Skip to content

Commit 84d10bf

Browse files
committed
testing/env: Make @go.test_compgen fail loudly
The previous commit rectified errors in assertions from `tests/new.bats` whereby assertions that should have failed as written did not. Though the code under test was sound, these latent test errors were due to the fact that `@go.test_compgen` would produce an empty result array and return success if the underlying `@go.compgen` returned empty results. When this empty array was passed to `assert_success` or `assert_failure` using "${array[@]}" notation, it expanded to an empty argument list, in which case these assertions did not check the value of `$output`. This left the impression that the output matched a nonempty list of values when the assertions were actually not comparing the output at all. Now `@go.test_compgen` will fail loudly with `@go.compgen` results are empty, and any case in which the underlying `@go.compgen` returns an error.
1 parent 4b9b137 commit 84d10bf

File tree

3 files changed

+68
-31
lines changed

3 files changed

+68
-31
lines changed

lib/testing/environment

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,19 @@ test-go() {
120120
# ...: Arguments passed directly to `@go.compgen`
121121
@go.test_compgen() {
122122
set "$DISABLE_BATS_SHELL_OPTIONS"
123+
local completions
124+
local err_args
125+
123126
. "$_GO_USE_MODULES" 'complete' 'strings'
124-
@go.split $'\n' "$(@go.compgen "${@:2}")" "$1"
127+
completions="$(@go.compgen "${@:2}")"
128+
129+
if [[ "$?" -ne '0' || -z "$completions" ]]; then
130+
printf -v 'err_args' ' "%s"' "${@:2}"
131+
printf 'compgen failed or results were empty:%s\n' "$err_args" >&2
132+
restore_bats_shell_options '1'
133+
return
134+
fi
135+
@go.split $'\n' "$completions" "$1"
125136
restore_bats_shell_options "$?"
126137
}
127138

tests/testing/environment.bats

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,3 @@ teardown() {
9393
run test-go
9494
assert_success '_GO_CMD: test-go'
9595
}
96-
97-
setup_go_test_compgen() {
98-
local item
99-
100-
mkdir -p "$TEST_GO_ROOTDIR/lib"
101-
printf 'foo' >"$TEST_GO_ROOTDIR/lib/foo"
102-
printf 'bar' >"$TEST_GO_ROOTDIR/lib/bar"
103-
printf 'baz' >"$TEST_GO_ROOTDIR/lib/baz"
104-
105-
. "$_GO_USE_MODULES" 'complete'
106-
while IFS= read -r item; do
107-
__expected+=("${item#$TEST_GO_ROOTDIR/}")
108-
done < <(@go.compgen -f -- "$TEST_GO_ROOTDIR/lib/")
109-
}
110-
111-
@test "$SUITE: @go.test_compgen" {
112-
set "$DISABLE_BATS_SHELL_OPTIONS"
113-
local __expected=()
114-
setup_go_test_compgen
115-
restore_bats_shell_options "$?"
116-
117-
export -f @go.test_compgen
118-
@go.create_test_go_script \
119-
'declare results=()' \
120-
'@go.test_compgen "results" -f -- lib/' \
121-
'printf "%s\n" "${results[@]}"'
122-
123-
run "$TEST_GO_SCRIPT"
124-
assert_success "${__expected[@]}"
125-
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#! /usr/bin/env bats
2+
3+
load ../../environment
4+
5+
EXPECTED=()
6+
7+
setup() {
8+
test_filter
9+
@go.create_test_go_script
10+
export -f @go.test_compgen
11+
12+
set "$DISABLE_BATS_SHELL_OPTIONS"
13+
setup_go_test_compgen
14+
restore_bats_shell_options "$?"
15+
16+
@go.create_test_go_script \
17+
'declare results=()' \
18+
'if ! @go.test_compgen "results" "$@"; then' \
19+
' exit 1' \
20+
'fi' \
21+
'printf "%s\n" "${results[@]}"'
22+
}
23+
24+
teardown() {
25+
@go.remove_test_go_rootdir
26+
}
27+
28+
setup_go_test_compgen() {
29+
local item
30+
31+
mkdir -p "$TEST_GO_ROOTDIR/lib"
32+
printf 'foo' >"$TEST_GO_ROOTDIR/lib/foo"
33+
printf 'bar' >"$TEST_GO_ROOTDIR/lib/bar"
34+
printf 'baz' >"$TEST_GO_ROOTDIR/lib/baz"
35+
36+
. "$_GO_USE_MODULES" 'complete'
37+
while IFS= read -r item; do
38+
EXPECTED+=("${item#$TEST_GO_ROOTDIR/}")
39+
done < <(@go.compgen -f -- "$TEST_GO_ROOTDIR/lib/")
40+
}
41+
42+
@test "$SUITE: completion succeeds" {
43+
run "$TEST_GO_SCRIPT" -f -- lib/
44+
assert_success "${EXPECTED[@]}"
45+
}
46+
47+
@test "$SUITE: completion fails" {
48+
run "$TEST_GO_SCRIPT" -f -- lib/nonexistent
49+
assert_failure \
50+
'compgen failed or results were empty: "-f" "--" "lib/nonexistent"'
51+
}
52+
53+
@test "$SUITE: fails if completions are empty" {
54+
run "$TEST_GO_SCRIPT" lib/nonexistent
55+
assert_failure 'compgen failed or results were empty: "lib/nonexistent"'
56+
}

0 commit comments

Comments
 (0)