Skip to content

Commit

Permalink
ensure that bash v3 tests run with bash v3
Browse files Browse the repository at this point in the history
this fixes our ability to catch the error from 3b85eb1

this is necessary as the movement from sourcing to commands causes `env` to be used, which will reset to the preferred bash rather than the active bash.

other changes:

- fix `confirm` tests hangining indefinitely on macos sonoma
  • Loading branch information
balupton committed Oct 25, 2023
1 parent 3b85eb1 commit 38a8ea8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
6 changes: 4 additions & 2 deletions commands/confirm
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ function confirm_() (
print_string "$prompt " >"$tty_target"
# send an ansi query to fetch the cursor row and column, returns [^[[24;80R] where 24 is row, 80 is column
# use _ to discard, the first read var is garbage, the second read var is the column, the final read var is the column
# don't use a timeout, as we already in a TTY so can guarantee an answer, and the read will complete immediately upon a response thanks to [-d R] which completes reading when the R is read, which is the final character of the response query
# use a 2 second timeout, as otherwise [confirm --test] on macos sonoma will wait forever
# shorter timeouts aren't suitable as slower machines take a while for the response
# we are already in a TTY, so can usually guarantee an answer, and the read will complete immediately upon a response thanks to [-d R] which completes reading when the R is read, which is the final character of the response query
local _
IFS='[;' read -srd R -p $'\e[6n' _ _ CURSOR_COLUMN <"$tty_target" || :
IFS='[;' read -t 2 -srd R -p $'\e[6n' _ _ CURSOR_COLUMN <"$tty_target" || :

# output the body if it exists
if test -n "$body"; then
Expand Down
13 changes: 11 additions & 2 deletions commands/debug-bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ function debug_bash() (
debug-bash [...options] -- <command> [...args]
OPTIONS:
--bash=<bash path>
If you want to invoke the command through a custom bash binary, then provide it here.
-v
Pass the -v flag to bash: Print shell input lines as they are read.
EOF
return 22 # EINVAL 22 Invalid argument
}

# process
local item cmd=() args=('-x')
local item option_bash='' cmd=() args=('-x')
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--bash='*) option_bash="${item#*--bash=}" ;;
'-v') args+=('-v') ;;
'--')
cmd+=("$@")
Expand All @@ -46,10 +50,15 @@ function debug_bash() (
help 'No <command> was provided.'
fi

# fallback
if test -z "$option_bash"; then
option_bash="$(type -P bash)"
fi

# =====================================
# Act

bash "${args[@]}" "$(type -P "${cmd[0]}")" "${cmd[@]:1}"
"$option_bash" "${args[@]}" "$(type -P "${cmd[0]}")" "${cmd[@]:1}"
return
)

Expand Down
10 changes: 5 additions & 5 deletions commands/dorothy
Original file line number Diff line number Diff line change
Expand Up @@ -1725,10 +1725,10 @@ function dorothy() (
source "$DOROTHY/sources/ripgrep.bash"

# able to test on bash v3?
local bash bashv3=''
local bash has_macos_bashv3='no'
bash="$(type -P bash)"
if is_mac && test "$bash" != '/bin/bash' && [[ "$(/bin/bash --version || :)" == 'GNU bash, version 3.'* ]]; then
bashv3='/bin/bash'
has_macos_bashv3=yes
fi

# prepre scan paths
Expand Down Expand Up @@ -1759,9 +1759,9 @@ function dorothy() (
fi

# run the bash v3 test
if test -n "$bashv3"; then
eval-helper --no-quiet --wrap -- "$bashv3" "$filepath" --test || {
failures+=("$bashv3 $filepath --test")
if test "$has_macos_bashv3" = 'yes'; then
eval-helper --no-quiet --wrap -- env EVAL_TESTER_BASH=/bin/bash PATH="/bin:$PATH" /bin/bash "$filepath" --test || {
failures+=("env EVAL_TESTER_BASH=/bin/bash PATH='/bin:$PATH' /bin/bash '$filepath' --test")
}
fi
done
Expand Down
32 changes: 24 additions & 8 deletions commands/eval-tester
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function eval_tester() (
--stderr=<expected stderr> (defaults to empty)
--ignore-stderr
--debug (implies --ignore-stderr)
--bash=<bash path>
Used to ensure that the command is invoked through a specific bash binary. Defaults to the env var EVAL_TESTER_BASH.
EOF
if test "$#" -ne 0; then
echo-error "$@"
Expand All @@ -29,13 +31,14 @@ function eval_tester() (
}

# process
local item cmd=() ignore_option=$'\e' option_name='' option_status='0' option_stdout='' option_stderr='' option_debug='no'
local item cmd=() ignore_option=$'\e' option_name='' option_status='0' option_stdout='' option_stderr='' option_debug='no' option_bash="${EVAL_TESTER_BASH-}"
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--name='*) option_name="${item#*--name=}" ;;
'--bash='*) option_bash="${item#*--bash=}" ;;
'--ignore-status') option_status="$ignore_option" ;;
'--ignore-stdout') option_stdout="$ignore_option" ;;
'--ignore-stderr') option_stderr="$ignore_option" ;;
Expand All @@ -55,13 +58,26 @@ function eval_tester() (
esac
done

# overrides
if test "$option_debug" = 'yes'; then
cmd=(
'debug-bash'
'--'
"${cmd[@]}"
)
# if invoking a command (not a function), then support debug and custom bash
# otherwise, custom bash not necessary as already inherited
# and debug-bash not possible
local cmd_path
cmd_path="$(type -P "${cmd[0]}" 2>/dev/null || :)"
if test -n "$cmd_path"; then
cmd[0]="$cmd_path"
if test "$option_debug" = 'yes'; then
cmd=(
'debug-bash'
"--bash=$option_bash"
'--'
"${cmd[@]}"
)
elif test -n "$option_bash"; then
cmd=(
"$option_bash"
"${cmd[@]}"
)
fi
fi

# =====================================
Expand Down

0 comments on commit 38a8ea8

Please sign in to comment.