Skip to content

Commit 35f2b88

Browse files
committed
ass-test-help: Use lib/bats/helper-function
This is in anticipation of migrating `lib/bats/assertions` to use `lib/bats/helper-function` as well.
1 parent af36aa4 commit 35f2b88

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

lib/bats/assertion-test-helpers

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# a Bats assertion function complies with the expectations outlined in
77
# `$_GO_CORE_DIR/lib/bats/assertions`, namely that it:
88
#
9-
# - begins with `set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"`
10-
# - calls `return_from_bats_assertion` directly via every return path
9+
# - begins with `set "$DISABLE_BATS_SHELL_OPTIONS"`
10+
# - calls `restore_bats_shell_options` directly via every return path
1111
#
1212
# They also enforce that the assertion produces no output when successful, and
1313
# that it writes only to standard error (`>&2`) when it fails.
@@ -36,7 +36,7 @@ fi
3636
export TEST_OUTPUT_FILE="$BATS_TEST_ROOTDIR/test-output.txt"
3737

3838
# Path to the script generated by `expect_assertion_*` to test whether
39-
# `return_from_bats_assertion` was called or not.
39+
# `restore_bats_shell_options` was called or not.
4040
readonly ASSERTION_TEST_SCRIPT="$BATS_TEST_ROOTDIR/assertion-test.bats"
4141

4242
# Format for the error message emitted when `ASSERTION_TEST_SCRIPT` fails.
@@ -48,11 +48,11 @@ export ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE=
4848
! read -rd '' ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE <<END_OF_FAILURE_MESSAGE
4949
The very first line of '%s' must be
5050
51-
set "\$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"
51+
set "\$DISABLE_BATS_SHELL_OPTIONS"
5252
53-
and it must call 'return_from_bats_assertion' directly from every return path.
53+
and it must call 'restore_bats_shell_options' directly from every return path.
5454
55-
For details, see the comments for 'return_from_bats_assertion' from:
55+
For details, see the comments for 'restore_bats_shell_options' from:
5656
5757
${BASH_SOURCE%/*}/assertions
5858
END_OF_FAILURE_MESSAGE
@@ -62,8 +62,7 @@ END_OF_FAILURE_MESSAGE
6262
# Will execute the command and assertion directly using the `run` command to
6363
# make sure the condition is satisfied as expected, then it will execute them
6464
# in a test script to make sure the assertion calls `set
65-
# "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS` and `return_from_bats_assertion`
66-
# appropriately.
65+
# "$DISABLE_BATS_SHELL_OPTIONS` and `restore_bats_shell_options` appropriately.
6766
#
6867
# Arguments:
6968
# run_cmd: The full command to pass to `run` as a single string
@@ -90,11 +89,11 @@ expect_assertion_success() {
9089
else
9190
# Although we expect the assertion under test to pass, this script injects a
9291
# failing assertion after it to check that the assertion under test directly
93-
# calls `return_from_bats_assertion` upon returning. If it doesn't, `set
92+
# calls `restore_bats_shell_options` upon returning. If it doesn't, `set
9493
# -eET` will not be in effect, so the failing assertion will not trigger the
9594
# ERR trap or fail the test case.
9695
#
97-
# In an earlier incarnation of `return_from_bats_assertion` that only
96+
# In an earlier incarnation of `restore_bats_shell_options` that only
9897
# restored `set -o functrace` (and when tests only started with `set +o
9998
# functrace`, equivalent to `set +T`), the failing assertion would fire the
10099
# ERR trap and exit the test case, but Bats would show the passing
@@ -125,8 +124,7 @@ expect_assertion_success() {
125124
# Will execute the command and assertion directly using the `run` command to
126125
# make sure the condition fails and the output is as expected, then it will
127126
# execute them in a test script to make sure the assertion calls `set
128-
# "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS` and `return_from_bats_assertion`
129-
# appropriately.
127+
# "$DISABLE_BATS_SHELL_OPTIONS` and `restore_bats_shell_options` appropriately.
130128
#
131129
# Arguments:
132130
# run_cmd: The full command to pass to `run` as a single string
@@ -300,7 +298,7 @@ __check_expected_output() {
300298
fi
301299
}
302300

303-
# Basically the same as `return_from_bats_assertion`, but specific to this file.
301+
# Basically the same as `restore_bats_shell_options`, but specific to this file.
304302
#
305303
# Arguments:
306304
# result: Return value of the calling assertion; defaults to 0

tests/assertion-test-helpers.bash

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ __test_assertion_impl() {
1616
fi
1717

1818
if [[ -n "$DELEGATE_RETURN_FROM_BATS_ASSERTION" ]]; then
19-
return_from_bats_assertion "$assertion_status"
19+
restore_bats_shell_options "$assertion_status"
2020
else
2121
return "$assertion_status"
2222
fi
2323
}
2424

2525
test_assertion() {
26-
# If an assertion fails to call `set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"`,
27-
# then when it fails, the stack trace will show the implementation details of
28-
# the assertion, rather than just the line at which it was called.
29-
set "${TEST_ASSERTION_SHELL_OPTIONS:-$BATS_ASSERTION_DISABLE_SHELL_OPTIONS}"
26+
# If an assertion fails to call `set "$DISABLE_BATS_SHELL_OPTIONS"`, then when
27+
# it fails, the stack trace will show the implementation details of the
28+
# assertion, rather than just the line at which it was called.
29+
set "${TEST_ASSERTION_SHELL_OPTIONS:-$DISABLE_BATS_SHELL_OPTIONS}"
3030

3131
__test_assertion_impl "$@"
3232
local result="$?"
3333

34-
# If an assertion calls `set "$BATS_ASSERTION_DISABLE_SHELL_OPTIONS"`, but not
35-
# `return_from_bats_assertion`, it will fail to scrub the stack and restore
34+
# If an assertion calls `set "$DISABLE_BATS_SHELL_OPTIONS"`, but not
35+
# `restore_bats_shell_options`, it will fail to scrub the stack and restore
3636
# `set -eET`.
3737
if [[ -z "$SKIP_RETURN_FROM_BATS_ASSERTION" &&
3838
-z "$DELEGATE_RETURN_FROM_BATS_ASSERTION" ]]; then
39-
return_from_bats_assertion "$result"
39+
restore_bats_shell_options "$result"
4040
else
4141
return "$result"
4242
fi

tests/assertion-test-helpers.bats

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ __check_failure_output() {
154154
"# 'test_assertion' should not produce output when successful."
155155
}
156156

157-
@test "$SUITE: successful assertion must call return_from_bats_assertion" {
157+
@test "$SUITE: successful assertion must call restore_bats_shell_options" {
158158
SKIP_RETURN_FROM_BATS_ASSERTION='true' run_assertion_test 'success'
159159
[ "$status" -eq '1' ]
160160

@@ -175,7 +175,7 @@ __check_failure_output() {
175175
"# $EXPECTED_TEST_SCRIPT_FAILURE_MESSAGE"
176176
}
177177

178-
@test "$SUITE: successful assertion return_from_bats_assertion must be direct" {
178+
@test "$SUITE: successful assertion restore_bats_shell_options must be direct" {
179179
DELEGATE_RETURN_FROM_BATS_ASSERTION='true' run_assertion_test 'success'
180180
[ "$status" -eq '1' ]
181181

@@ -278,7 +278,7 @@ __check_failure_output() {
278278
"# $EXPECTED_TEST_SCRIPT_FAILURE_MESSAGE"
279279
}
280280

281-
@test "$SUITE: failing assertion must call return_from_bats_assertion" {
281+
@test "$SUITE: failing assertion must call restore_bats_shell_options" {
282282
ASSERTION_STATUS='1' SKIP_RETURN_FROM_BATS_ASSERTION='true' \
283283
run_assertion_test 'failure' 'foo bar baz'
284284
[ "$status" -eq '1' ]
@@ -300,7 +300,7 @@ __check_failure_output() {
300300
"# $EXPECTED_TEST_SCRIPT_FAILURE_MESSAGE"
301301
}
302302

303-
@test "$SUITE: failing assertion return_from_bats_assertion must be direct" {
303+
@test "$SUITE: failing assertion restore_bats_shell_options must be direct" {
304304
ASSERTION_STATUS='1' DELEGATE_RETURN_FROM_BATS_ASSERTION='true' \
305305
run_assertion_test 'failure' 'foo bar baz'
306306
[ "$status" -eq '1' ]

0 commit comments

Comments
 (0)