Skip to content

Commit

Permalink
Do not enable bash-preexec in non-interactive shells
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Oct 1, 2024
1 parent 8926de0 commit 6b7a9be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0]
return 1
fi

# We do not enable bash-preexc in non-interactive shells (except in tests).
# `__bp_inside_test` is set in test/*.bats in bash-preexec repository.
if [[ $- != *i* && ! ${__bp_inside_test-} ]]; then
return 0
fi

# Avoid duplicate inclusion
if [[ -n "${bash_preexec_imported:-}" || -n "${__bp_imported:-}" ]]; then
return 0
Expand Down
6 changes: 6 additions & 0 deletions test/bash-preexec.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env bats

# This variable tells bash-preexec.sh that it is loaded for testing purposes.
# bash-preexec.sh is intended to be used in interactive shell sessions, so it
# is disabled in non-interactive shells by default. However, it still needs to
# be loaded in non-interactive shells for the Bats tests,
__bp_inside_test=yes

setup() {
PROMPT_COMMAND='' # in case the invoking shell has set this
history -s fake command # preexec requires there be some history
Expand Down
6 changes: 6 additions & 0 deletions test/include-test.bats
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env bats

# This variable tells bash-preexec.sh that it is loaded for testing purposes.
# bash-preexec.sh is intended to be used in interactive shell sessions, so it
# is disabled in non-interactive shells by default. However, it still needs to
# be loaded in non-interactive shells for the Bats tests,
__bp_inside_test=yes

@test "should not import if it's already defined" {
bash_preexec_imported="defined"
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
Expand Down

0 comments on commit 6b7a9be

Please sign in to comment.