Skip to content

Commit 7154098

Browse files
jordemortrcaloras
andauthored
Return original value from __bp_precmd_invoke_cmd (#131)
* Return original value from `__bp_precmd_invoke_cmd` WIthout this change, `__bp_precmd_invoke_cmd` may not restore `$?` to its original value after being run. Since `__bp_precmd_invoke_cmd` get installed as the first thing in `PROMPT_COMMAND`, this will cause problems if there is an existing `PROMPT_COMMAND` (which is perhaps not aware of `bash-preexec`) that wants to do something with the exit status of the command that was just run. * Updating tests to allow preserving original exist status for other commands in PROMPT_COMMAND Co-authored-by: Ryan Caloras <[email protected]>
1 parent fd2ffa8 commit 7154098

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

bash-preexec.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ __bp_precmd_invoke_cmd() {
156156
"$precmd_function"
157157
fi
158158
done
159+
160+
__bp_set_ret_value "$__bp_last_ret_value"
159161
}
160162

161163
# Sets a return value in $?. We may want to get access to the $? variable in our

test/bash-preexec.bats

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ test_preexec_echo() {
2121
printf "%s\n" "$1"
2222
}
2323

24+
# Helper functions necessary because Bats' run doesn't preserve $?
25+
return_exit_code() {
26+
return $1
27+
}
28+
29+
set_exit_code_and_run_precmd() {
30+
return_exit_code ${1:-0}
31+
__bp_precmd_invoke_cmd
32+
}
33+
34+
2435
@test "sourcing bash-preexec should exit with 1 if we're not using bash" {
2536
unset BASH_VERSION
2637
run source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"
@@ -155,7 +166,7 @@ test_preexec_echo() {
155166

156167
@test "precmd should execute a function once" {
157168
precmd_functions+=(test_echo)
158-
run '__bp_precmd_invoke_cmd'
169+
run set_exit_code_and_run_precmd
159170
[ $status -eq 0 ]
160171
[ "$output" == "test echo" ]
161172
}
@@ -164,18 +175,10 @@ test_preexec_echo() {
164175
echo_exit_code() {
165176
echo "$?"
166177
}
167-
return_exit_code() {
168-
return $1
169-
}
170-
# Helper function is necessary because Bats' run doesn't preserve $?
171-
set_exit_code_and_run_precmd() {
172-
return_exit_code 251
173-
__bp_precmd_invoke_cmd
174-
}
175178

176179
precmd_functions+=(echo_exit_code)
177-
run 'set_exit_code_and_run_precmd'
178-
[ $status -eq 0 ]
180+
run set_exit_code_and_run_precmd 251
181+
[ $status -eq 251 ]
179182
[ "$output" == "251" ]
180183
}
181184

@@ -206,7 +209,7 @@ test_preexec_echo() {
206209
: "last-arg"
207210
__bp_preexec_invoke_exec "$_"
208211
eval "$bats_trap" # Restore trap
209-
run '__bp_precmd_invoke_cmd'
212+
run set_exit_code_and_run_precmd
210213
[ $status -eq 0 ]
211214
[ "$output" == "last-arg" ]
212215
}
@@ -242,7 +245,7 @@ test_preexec_echo() {
242245
precmd_functions+=(fun_1)
243246
precmd_functions+=(fun_2)
244247

245-
run '__bp_precmd_invoke_cmd'
248+
run set_exit_code_and_run_precmd
246249
[ $status -eq 0 ]
247250
[ "${#lines[@]}" == '2' ]
248251
[ "${lines[0]}" == "one" ]

0 commit comments

Comments
 (0)