Remove the underscore prefix of __bp_imported (#123)

* Remove the underscore prefix of "__bp_imported"

This formally exposes it as part of the public API.

* Fix Bats test after renaming the variable

* Rename the variable upon reviewer request

* Fix inconsistency after renaming variables

Variables were reanmed in 7fbc07fb4e, but
it didn’t cover the test or ReadMe. This commit fixes that.
This commit is contained in:
Franklin Yu 2022-01-27 02:25:47 -08:00 committed by GitHub
parent 3e563be4f1
commit fd2ffa8876
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -91,6 +91,15 @@ export __bp_enable_subshells="true"
```
This is disabled by default due to buggy situations related to to `functrace` and Bash's `DEBUG trap`. See [Issue #25](https://github.com/rcaloras/bash-preexec/issues/25)
## Library authors
If you want to detect bash-preexec in your library (for example, to add hooks to `preexec_functions` when available), use the Bash variable `bash_preexec_imported`:
```bash
if [[ -n "${bash_preexec_imported:-}" ]]; then
echo "Bash-preexec is loaded."
fi
```
## Tests
You can run tests using [Bats](https://github.com/bats-core/bats-core).
```bash

View file

@ -38,10 +38,14 @@ if [[ -z "${BASH_VERSION:-}" ]]; then
fi
# Avoid duplicate inclusion
if [[ "${__bp_imported:-}" == "defined" ]]; then
if [[ -n "${bash_preexec_imported:-}" ]]; then
return 0
fi
__bp_imported="defined"
bash_preexec_imported="defined"
# WARNING: This variable is no longer used and should not be relied upon.
# Use ${bash_preexec_imported} instead.
__bp_imported="${bash_preexec_imported}"
# Should be available to each precmd and preexec
# functions, should they want it. $? and $_ are available as $? and $_, but

View file

@ -1,13 +1,13 @@
#!/usr/bin/env bats
@test "should not import if it's already defined" {
__bp_imported="defined"
bash_preexec_imported="defined"
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
[ -z $(type -t __bp_install) ]
}
@test "should import if not defined" {
unset __bp_imported
unset bash_preexec_imported
source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
[ -n $(type -t __bp_install) ]
}