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 Jan 13, 2025
1 parent e8e9024 commit acc0aa5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
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
13 changes: 12 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Testing `bash-preexec`
======================

**Note on test conditions**
### Note on test conditions

When writing test conditions, use `[ ... ]` instead of `[[ ... ]]` since the
former are supported by Bats on Bash versions before 4.1. In particular, macOS
Expand All @@ -20,3 +20,14 @@ References:
* [Differences between `[` and `[[`](http://mywiki.wooledge.org/BashFAQ/031)
* [Problems with `[[` in Bats](https://github.com/sstephenson/bats/issues/49)
* [Using `|| return 1` instead of `|| false`](https://github.com/bats-core/bats-core/commit/e5695a673faad4d4d33446ed5c99d70dbfa6d8be)


### Set variable `__bp_inside_test` to test bash-preexec

By default, bash-preexec is disabled in a non-interactive shell. However, to
test bash-preexec in non-interactive shells, one needs to enable bash-preexec
by setting variable `__bp_inside_test` to a non-empty string.

```bash
__bp_inside_test=yes
```
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 acc0aa5

Please sign in to comment.