@@ -74,54 +74,8 @@ END_OF_FAILURE_MESSAGE
7474# assertion: The full assertion to evaluate as a single string
7575expect_assertion_success () {
7676 set +eET
77- local run_cmd=" $1 "
78- local assertion=" $2 "
79- local test_script=" $ASSERTION_TEST_SCRIPT "
80- local __assertion_output
81- local __assertion_status
82- local expected_output=()
83-
84- if ! __run_command_and_assertion_in_subshell " $run_cmd " " $assertion " ; then
85- __return_from_expect_assertion ' 1'
86- elif [[ " $__assertion_status " -ne ' 0' ]]; then
87- printf " In subshell: expected passing status, actual %d\nOutput:\n%s\n" \
88- " $__assertion_status " " $__assertion_output " >&2
89- __return_from_expect_assertion ' 1'
90- elif ! __check_expected_output " $__assertion_output " ; then
91- printf " '%s' should not produce output when successful.\n" \
92- " ${assertion%% * } " >&2
93- __return_from_expect_assertion ' 1'
94- else
95- # Although we expect the assertion under test to pass, this script injects a
96- # failing assertion after it to check that the assertion under test directly
97- # calls `restore_bats_shell_options` upon returning. If it doesn't, `set
98- # -eET` will not be in effect, so the failing assertion will not trigger the
99- # ERR trap or fail the test case.
100- #
101- # In an earlier incarnation of `restore_bats_shell_options` that only
102- # restored `set -o functrace` (and when tests only started with `set +o
103- # functrace`, equivalent to `set +T`), the failing assertion would fire the
104- # ERR trap and exit the test case, but Bats would show the passing
105- # assertion's stack, per issue #48.
106- __run_assertion_test_script \
107- " run $run_cmd " \
108- " failing_assertion() { [ 0 -eq 1 ]; }" \
109- " $assertion " \
110- " failing_assertion"
111-
112- expected_output=(' 1..1'
113- " not ok 1 $BATS_TEST_DESCRIPTION "
114- " # (from function \` failing_assertion' in file $test_script , line 5,"
115- " # in test file $test_script , line 7)"
116- " # \` failing_assertion' failed" )
117-
118- if ! __check_expected_output " $output " " ${expected_output[@]} " ; then
119- printf " $ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE " " ${assertion%% * } " >&2
120- __return_from_expect_assertion ' 1'
121- else
122- __return_from_expect_assertion
123- fi
124- fi
77+ __expect_assertion_success " $@ "
78+ __return_from_expect_assertion " $? "
12579}
12680
12781# Validates that an assertion fails for the provided inputs
@@ -137,44 +91,8 @@ expect_assertion_success() {
13791# expected: The expected output of the assertion failure
13892expect_assertion_failure () {
13993 set +eET
140- local run_cmd=" $1 "
141- local assertion=" $2 "
142- shift 2
143- local __assertion_output
144- local __assertion_status
145- local with_status
146- local expected_output=()
147-
148- if ! __run_command_and_assertion_in_subshell " $run_cmd " " $assertion " ; then
149- __return_from_expect_assertion ' 1'
150- elif [[ " $__assertion_status " -eq ' 0' ]]; then
151- printf " In subshell: expected failure, but succeeded\nOutput:\n%s\n" \
152- " $__assertion_output " >&2
153- __return_from_expect_assertion ' 1'
154- else
155- if [[ " $__assertion_status " -ne ' 1' ]]; then
156- with_status=" with status $__assertion_status "
157- fi
158-
159- if ! __check_expected_output " $__assertion_output " " $@ " ; then
160- __return_from_expect_assertion ' 1'
161- else
162- __run_assertion_test_script " run $run_cmd " " $assertion "
163-
164- expected_output=(' 1..1'
165- " not ok 1 $BATS_TEST_DESCRIPTION "
166- " # (in test file $ASSERTION_TEST_SCRIPT , line 5)"
167- " # \` $assertion ' failed${with_status} "
168- " ${@/#/# } " )
169-
170- if ! __check_expected_output " $output " " ${expected_output[@]} " ; then
171- printf " $ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE " " ${assertion%% * } " >&2
172- __return_from_expect_assertion ' 1'
173- else
174- __return_from_expect_assertion
175- fi
176- fi
177- fi
94+ __expect_assertion_failure " $@ "
95+ __return_from_expect_assertion " $? "
17896}
17997
18098# Calls `printf` on its arguments and returns an error
@@ -220,6 +138,103 @@ export -f printf_to_test_output_file
220138# None of the functions below this line are part of the public interface.
221139# --------------------------------
222140
141+ # Implementation for `expect_assertion_success`
142+ #
143+ # Arguments:
144+ # run_cmd: The full command to pass to `run` as a single string
145+ # assertion: The full assertion to evaluate as a single string
146+ __expect_assertion_success () {
147+ local run_cmd=" $1 "
148+ local assertion=" $2 "
149+ local test_script=" $ASSERTION_TEST_SCRIPT "
150+ local __assertion_output
151+ local __assertion_status
152+ local expected_output=()
153+
154+ if ! __run_command_and_assertion_in_subshell " $run_cmd " " $assertion " ; then
155+ return ' 1'
156+ elif [[ " $__assertion_status " -ne ' 0' ]]; then
157+ printf " In subshell: expected passing status, actual %d\nOutput:\n%s\n" \
158+ " $__assertion_status " " $__assertion_output " >&2
159+ return ' 1'
160+ elif ! __check_expected_output " $__assertion_output " ; then
161+ printf " '%s' should not produce output when successful.\n" \
162+ " ${assertion%% * } " >&2
163+ return ' 1'
164+ fi
165+
166+ # Although we expect the assertion under test to pass, this script injects a
167+ # failing assertion after it to check that the assertion under test directly
168+ # calls `restore_bats_shell_options` upon returning. If it doesn't, `set
169+ # -eET` will not be in effect, so the failing assertion will not trigger the
170+ # ERR trap or fail the test case.
171+ #
172+ # In an earlier incarnation of `restore_bats_shell_options` that only
173+ # restored `set -o functrace` (and when tests only started with `set +o
174+ # functrace`, equivalent to `set +T`), the failing assertion would fire the
175+ # ERR trap and exit the test case, but Bats would show the passing
176+ # assertion's stack, per issue #48.
177+ __run_assertion_test_script \
178+ " run $run_cmd " \
179+ " failing_assertion() { [ 0 -eq 1 ]; }" \
180+ " $assertion " \
181+ " failing_assertion"
182+
183+ expected_output=(' 1..1'
184+ " not ok 1 $BATS_TEST_DESCRIPTION "
185+ " # (from function \` failing_assertion' in file $test_script , line 5,"
186+ " # in test file $test_script , line 7)"
187+ " # \` failing_assertion' failed" )
188+
189+ if ! __check_expected_output " $output " " ${expected_output[@]} " ; then
190+ printf " $ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE " " ${assertion%% * } " >&2
191+ return ' 1'
192+ fi
193+ }
194+
195+ # Implementation for `expect_assertion_failure`
196+ #
197+ # Arguments:
198+ # run_cmd: The full command to pass to `run` as a single string
199+ # assertion: The full assertion to evaluate as a single string
200+ # expected: The expected output of the assertion failure
201+ __expect_assertion_failure () {
202+ local run_cmd=" $1 "
203+ local assertion=" $2 "
204+ shift 2
205+ local __assertion_output
206+ local __assertion_status
207+ local with_status
208+ local expected_output=()
209+
210+ if ! __run_command_and_assertion_in_subshell " $run_cmd " " $assertion " ; then
211+ return ' 1'
212+ elif [[ " $__assertion_status " -eq ' 0' ]]; then
213+ printf " In subshell: expected failure, but succeeded\nOutput:\n%s\n" \
214+ " $__assertion_output " >&2
215+ return ' 1'
216+ fi
217+
218+ if [[ " $__assertion_status " -ne ' 1' ]]; then
219+ with_status=" with status $__assertion_status "
220+ elif ! __check_expected_output " $__assertion_output " " $@ " ; then
221+ return ' 1'
222+ fi
223+
224+ __run_assertion_test_script " run $run_cmd " " $assertion "
225+
226+ expected_output=(' 1..1'
227+ " not ok 1 $BATS_TEST_DESCRIPTION "
228+ " # (in test file $ASSERTION_TEST_SCRIPT , line 5)"
229+ " # \` $assertion ' failed${with_status} "
230+ " ${@/#/# } " )
231+
232+ if ! __check_expected_output " $output " " ${expected_output[@]} " ; then
233+ printf " $ASSERTION_TEST_SCRIPT_FAILURE_MESSAGE " " ${assertion%% * } " >&2
234+ return ' 1'
235+ fi
236+ }
237+
223238# Passes the target command to `run`, the executes the `assertion` in a subshell
224239#
225240# The `run_cmd` is executed in-process to set `output`, `status`, and `lines`.
@@ -262,7 +277,7 @@ __run_command_and_assertion_in_subshell() {
262277 elif [[ -z " $__assertion_status " ]]; then
263278 __assertion_output=" ${__assertion_output% exit:* } "
264279
265- if [[ " ${FUNCNAME[1]} " == ' expect_assertion_failure ' ]]; then
280+ if [[ " ${FUNCNAME[1]} " == ' __expect_assertion_failure ' ]]; then
266281 printf ' "%s" output does not end with a newline character:\n%s\n' \
267282 " ${assertion%% * } " " ${__assertion_output% exit:* } " >&2
268283 return 1
0 commit comments