Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve prior_trap processing #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,8 @@ __bp_install() {
trap '__bp_preexec_invoke_exec "$_"' DEBUG

# Preserve any prior DEBUG trap as a preexec function
local prior_trap
# we can't easily do this with variable expansion. Leaving as sed command.
# shellcheck disable=SC2001
prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
eval "local trap_argv=(${__bp_trap_string:-})"
local prior_trap=${trap_argv[2]:-}
unset __bp_trap_string
if [[ -n "$prior_trap" ]]; then
eval '__bp_original_debug_trap() {
Expand Down
20 changes: 20 additions & 0 deletions test/bash-preexec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ set_exit_code_and_run_precmd() {
(( trap_count_snapshot < trap_invoked_count ))
}

@test "__bp_install should preserve an existing DEBUG trap containing quotes" {
trap_invoked_count=0
foo() { (( trap_invoked_count += 1 )); }

# note setting this causes BATS to mis-report the failure line when this test fails
trap "foo && echo 'hello' >/dev/null" debug
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous sed-based pattern would fail with more complex cases like this.

[ "$(trap -p DEBUG | cut -d' ' -f3-7)" == "'foo && echo '\''hello'\'' >/dev/null'" ]

bp_install
trap_count_snapshot=$trap_invoked_count

[ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ]
[[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1

__bp_interactive_mode # triggers the DEBUG trap

# ensure the trap count is still being incremented after the trap's been overwritten
(( trap_count_snapshot < trap_invoked_count ))
}

@test "__bp_sanitize_string should remove semicolons and trim space" {

__bp_sanitize_string output " true1; "$'\n'
Expand Down