Skip to content

Commit fd99df5

Browse files
committed
tests: Use lib/bats/helper-function features
Part of #156. This replaces `BATS_ASSERTION_DISABLE_SHELL_OPTIONS` with `DISABLE_BATS_SHELL_OPTIONS` and replaces `return_from_bats_assertion` with `restore_bats_shell_options` in assertions defined in Bats test suite files. Performance isn't greatly affected, except that the `core/plugin-scope-variable-values` tests are twice as fast after extracting `__assert_scope_value_equal_impl`.
1 parent 6cc7c95 commit fd99df5

File tree

6 files changed

+29
-24
lines changed

6 files changed

+29
-24
lines changed

tests/commands/find.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ teardown() {
2525
}
2626

2727
assert_command_scripts_equal() {
28-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
28+
set "$DISABLE_BATS_SHELL_OPTIONS"
2929
unset 'lines[0]' 'lines[1]'
3030
lines=("${lines[@]}")
3131
assert_lines_equal "$@"
32-
return_from_bats_assertion "$?"
32+
restore_bats_shell_options "$?"
3333
}
3434

3535
@test "$SUITE: return only builtin commands" {

tests/complete/command-path.bats

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ teardown() {
2828
}
2929

3030
assert_completions_match() {
31-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
31+
set "$DISABLE_BATS_SHELL_OPTIONS"
32+
__assert_completions_match_impl "$@"
33+
restore_bats_shell_options "$?"
34+
}
35+
36+
__assert_completions_match_impl() {
3237
# Trim the last three lines of output from the script to compare separately
3338
# from the output of _@go.complete_command_path.
3439
local num_lines="${#lines[@]}"
@@ -55,11 +60,7 @@ assert_completions_match() {
5560
if ! assert_equal "${__expected_argv[*]}" "${var_lines[2]}" 'argv list'; then
5661
((++num_errors))
5762
fi
58-
if [[ "$num_errors" -ne '0' ]]; then
59-
return_from_bats_assertion '1'
60-
else
61-
return_from_bats_assertion
62-
fi
63+
[[ "$num_errors" -eq '0' ]]
6364
}
6465

6566
@test "$SUITE: error on empty arguments" {

tests/core/plugin-scope-variable-values.bats

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ teardown() {
5757
}
5858

5959
assert_scope_values_equal() {
60-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
60+
set "$DISABLE_BATS_SHELL_OPTIONS"
61+
__assert_scope_values_equal_impl "$@"
62+
restore_bats_shell_options "$?"
63+
}
64+
65+
__assert_scope_values_equal_impl() {
6166
local scope="$1"
6267
shift
6368
local orig_lines=("${lines[@]}")
@@ -71,15 +76,14 @@ assert_scope_values_equal() {
7176
result='1'
7277
fi
7378
lines=("${orig_lines[@]}")
74-
return_from_bats_assertion "$result"
75-
return
79+
return "$result"
7680
fi
7781
done
7882

7983
if [[ "$i" -eq "${#lines[@]}" ]]; then
8084
printf 'ERROR: could not find "%s" in output.\nOUTPUT:\n%s\n' \
8185
"$scope SCOPE:" "$output" >&2
82-
return_from_bats_assertion '1'
86+
return '1'
8387
fi
8488
}
8589

tests/log/add-output-file.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ teardown() {
88
}
99

1010
run_log_script_and_assert_status_and_output() {
11-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
11+
set "$DISABLE_BATS_SHELL_OPTIONS"
1212
local num_errors
1313
local expected
1414

@@ -28,9 +28,9 @@ run_log_script_and_assert_status_and_output() {
2828
"$(@go.stack_trace_item_from_offset "$TEST_GO_SCRIPT")")
2929

3030
@go.assert_log_equals "${expected[@]}"
31-
return_from_bats_assertion "$?"
31+
restore_bats_shell_options "$?"
3232
else
33-
return_from_bats_assertion 1
33+
restore_bats_shell_options 1
3434
fi
3535
}
3636

tests/log/filters.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ teardown() {
88
}
99

1010
run_log_script_and_assert_success() {
11-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
11+
set "$DISABLE_BATS_SHELL_OPTIONS"
1212
local result=0
1313

1414
@go.run_log_script "$@" \
@@ -23,7 +23,7 @@ run_log_script_and_assert_success() {
2323
'@go.log INFO Goodbye, World!'
2424

2525
assert_success
26-
return_from_bats_assertion "$?"
26+
restore_bats_shell_options "$?"
2727
}
2828

2929
@test "$SUITE: default _GO_LOG_LEVEL_FILTER is RUN" {

tests/validation.bats

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ teardown() {
77
}
88

99
assert_error_on_invalid_input() {
10-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
10+
set "$DISABLE_BATS_SHELL_OPTIONS"
1111
run "$TEST_GO_SCRIPT" "$1"
1212

1313
if [[ "$status" -eq '0' ]]; then
1414
echo "Expected input to fail validation: $1" >&2
15-
return_from_bats_assertion 1
15+
restore_bats_shell_options 1
1616
else
17-
return_from_bats_assertion
17+
restore_bats_shell_options
1818
fi
1919
}
2020

2121
assert_success_on_valid_input() {
22-
set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
22+
set "$DISABLE_BATS_SHELL_OPTIONS"
2323
run "$TEST_GO_SCRIPT" "$1"
2424

2525
if [[ "$status" -ne '0' ]]; then
2626
printf 'Expected input to pass validation: %s\n' "$1" >&2
27-
return_from_bats_assertion 1
27+
restore_bats_shell_options 1
2828
return
2929
fi
3030

3131
run eval "var=\"$1\""
3232

3333
if [[ -n "$output" ]]; then
3434
fail "Evaluating input still produced output: $1" >&2
35-
return_from_bats_assertion 1
35+
restore_bats_shell_options 1
3636
else
37-
return_from_bats_assertion
37+
restore_bats_shell_options
3838
fi
3939
}
4040

0 commit comments

Comments
 (0)