/
file_setup_teardown.bats
198 lines (176 loc) · 8.02 KB
/
file_setup_teardown.bats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
bats_require_minimum_version 1.5.0
load 'test_helper'
fixtures file_setup_teardown
setup_file() {
export SETUP_FILE_EXPORT_TEST=true
}
@test "setup_file is run once per file" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/setup_file_once.log"
bats "$FIXTURE_ROOT/setup_file.bats"
}
@test "teardown_file is run once per file" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/teardown_file_once.log"
run bats "$FIXTURE_ROOT/teardown_file.bats"
[[ $status -eq 0 ]]
# output the log for faster debugging
cat "$LOG"
# expect to find an entry for the tested file
grep 'teardown_file.bats' "$LOG"
# it should be the only entry
run wc -l < "$LOG"
[[ $output -eq 1 ]]
}
@test "setup_file is called correctly in multi file suite" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/setup_file_multi_file_suite.log"
run bats "$FIXTURE_ROOT/setup_file.bats" "$FIXTURE_ROOT/no_setup_file.bats" "$FIXTURE_ROOT/setup_file2.bats"
[[ $status -eq 0 ]]
run wc -l < "$LOG"
# each setup_file[2].bats is in the log exactly once!
[[ $output -eq 2 ]]
grep setup_file.bats "$LOG"
grep setup_file2.bats "$LOG"
}
@test "teardown_file is called correctly in multi file suite" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/teardown_file_multi_file_suite.log"
run bats "$FIXTURE_ROOT/teardown_file.bats" "$FIXTURE_ROOT/no_teardown_file.bats" "$FIXTURE_ROOT/teardown_file2.bats"
[[ $status -eq 0 ]]
run wc -l < "$LOG"
# each teardown_file[2].bats is in the log exactly once!
[[ $output -eq 2 ]]
grep teardown_file.bats "$LOG"
grep teardown_file2.bats "$LOG"
}
@test "setup_file failure aborts tests for this file" {
# this might need to mark them as skipped as the test count is already determined at this point
run bats "$FIXTURE_ROOT/setup_file_failed.bats"
echo "$output"
[[ "${lines[0]}" == "1..2" ]]
[[ "${lines[1]}" == "not ok 1 setup_file failed" ]]
[[ "${lines[2]}" == "# (from function \`setup_file' in test file $RELATIVE_FIXTURE_ROOT/setup_file_failed.bats, line 2)" ]]
[[ "${lines[3]}" == "# \`false' failed" ]]
[[ "${lines[4]}" == "# bats warning: Executed 1 instead of expected 2 tests" ]] # this warning is expected
# to appease the count validator, we would have to reduce the expected number of tests (retroactively?) or
# output even those tests that should be skipped due to a failed setup_file.
# Since we are already in a failure mode, the additional error does not hurt and is less verbose than
# printing all the failed/skipped tests due to the setup failure.
}
@test "teardown_file failure fails at least one test from the file" {
run bats "$FIXTURE_ROOT/teardown_file_failed.bats"
[[ $status -ne 0 ]]
echo "$output"
[[ "${lines[0]}" == "1..1" ]]
[[ "${lines[1]}" == "ok 1 test" ]]
[[ "${lines[2]}" == "not ok 2 teardown_file failed" ]]
[[ "${lines[3]}" == "# (from function \`teardown_file' in test file $RELATIVE_FIXTURE_ROOT/teardown_file_failed.bats, line 3)" ]]
[[ "${lines[4]}" == "# \`false' failed" ]]
[[ "${lines[5]}" == "# bats warning: Executed 2 instead of expected 1 tests" ]] # for now this warning is expected
# for a failed teardown_file not to change the number of tests being reported, we would have to alter at least one previous test result report
# this would require arbitrary amounts of buffering so we simply add our own line with a fake test number
# tripping the count validator won't change the overall result, as we already are in a failure mode
}
@test "teardown_file runs even if any test in the file failed" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/teardown_file_failed.log"
run bats "$FIXTURE_ROOT/teardown_file_after_failing_test.bats"
[[ $status -ne 0 ]]
grep teardown_file_after_failing_test.bats "$LOG"
echo "$output"
[[ $output == "1..1
not ok 1 failing test
# (in test file $RELATIVE_FIXTURE_ROOT/teardown_file_after_failing_test.bats, line 6)
# \`false' failed" ]]
}
@test "teardown_file should run even after user abort via CTRL-C" {
if [[ "$BATS_NUMBER_OF_PARALLEL_JOBS" -gt 1 ]]; then
skip "Aborts don't work in parallel mode"
fi
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/teardown_file_abort.log"
# guarantee that background processes get their own process group -> pid=pgid
set -m
SECONDS=0
# run testsubprocess in background to not avoid blocking this test
bats "$FIXTURE_ROOT/teardown_file_after_long_test.bats"&
SUBPROCESS_PID=$!
# wait until we enter the test
sleep 2
# fake sending SIGINT (CTRL-C) to the process group of the background subprocess
kill -SIGINT -- -$SUBPROCESS_PID
wait # for the test to finish either way (SIGINT or normal execution)
echo "Waited: $SECONDS seconds"
[[ $SECONDS -lt 10 ]] # make sure we really cut it short with SIGINT
# check that teardown_file ran and created the log file
[[ -f "$LOG" ]]
grep teardown_file_after_long_test.bats "$LOG"
# but the test must not have run to the end!
run ! grep "test finished successfully" "$LOG"
}
@test "setup_file runs even if all tests in the file are skipped" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/setup_file_skipped.log"
run bats "$FIXTURE_ROOT/setup_file_even_if_all_tests_are_skipped.bats"
[[ -f "$LOG" ]]
grep setup_file_even_if_all_tests_are_skipped.bats "$LOG"
}
@test "teardown_file runs even if all tests in the file are skipped" {
# shellcheck disable=SC2031,SC2030
export LOG="$BATS_TEST_TMPDIR/teardown_file_skipped.log"
run bats "$FIXTURE_ROOT/teardown_file_even_if_all_tests_are_skipped.bats"
[[ $status -eq 0 ]]
[[ -f "$LOG" ]]
grep teardown_file_even_if_all_tests_are_skipped.bats "$LOG"
}
@test "setup_file must not leak context between tests in the same suite" {
# example: BATS_ROOT was unset in one test but used in others, therefore, the suite failed
# Simulate leaking env var from first to second test by: export SETUP_FILE_VAR="LEAK!"
run bats "$FIXTURE_ROOT/setup_file_does_not_leak_env.bats" "$FIXTURE_ROOT/setup_file_does_not_leak_env2.bats"
echo "$output"
[[ $status -eq 0 ]]
}
@test "teardown_file must not leak context between tests in the same suite" {
# example: BATS_ROOT was unset in one test but used in others, therefore, the suite failed
run bats "$FIXTURE_ROOT/teardown_file_does_not_leak.bats" "$FIXTURE_ROOT/teardown_file_does_not_leak2.bats"
echo "$output"
[[ $status -eq 0 ]]
[[ $output == "1..2
ok 1 test
ok 2 must not see variable from first run" ]]
}
@test "halfway setup_file errors are caught and reported" {
run bats "$FIXTURE_ROOT/setup_file_halfway_error.bats"
[ $status -ne 0 ]
echo "$output"
[ "${lines[0]}" == "1..1" ]
[ "${lines[1]}" == "not ok 1 setup_file failed" ]
[ "${lines[2]}" == "# (from function \`setup_file' in test file $RELATIVE_FIXTURE_ROOT/setup_file_halfway_error.bats, line 3)" ]
[ "${lines[3]}" == "# \`false' failed" ]
}
@test "halfway teardown_file errors are ignored" {
run -0 bats "$FIXTURE_ROOT/teardown_file_halfway_error.bats"
[ "${lines[0]}" == "1..1" ]
[ "${lines[1]}" == "ok 1 empty" ]
[ "${#lines[@]}" -eq 2 ]
}
@test "variables exported in setup_file are visible in tests" {
[[ $SETUP_FILE_EXPORT_TEST == "true" ]]
}
@test "Don't run setup_file for files without tests" {
# shellcheck disable=SC2031
export LOG="$BATS_TEST_TMPDIR/setup_file.log"
# only select the test from no_setup_file
run bats -f test "$FIXTURE_ROOT/setup_file.bats" "$FIXTURE_ROOT/no_setup_file.bats"
[ ! -f "$LOG" ] # setup_file must not have been executed!
[ "${lines[0]}" == '1..1' ] # but at least one test should have been run
}
@test "Failure in setup_file and teardown_file still prints error message" {
run ! bats "$FIXTURE_ROOT/error_in_setup_and_teardown_file.bats"
[ "${lines[0]}" == '1..1' ]
[ "${lines[1]}" == 'not ok 1 setup_file failed' ]
[ "${lines[2]}" == "# (from function \`setup_file' in test file test/fixtures/file_setup_teardown/error_in_setup_and_teardown_file.bats, line 2)" ]
[ "${lines[3]}" == "# \`false' failed" ]
[ "${#lines[@]}" -eq 4 ]
}