-
Notifications
You must be signed in to change notification settings - Fork 412
/
bats.bats
executable file
·485 lines (411 loc) · 16.7 KB
/
bats.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env bats
load test_helper
fixtures bats
@test "no arguments prints message and usage instructions" {
run bats
[ $status -eq 1 ]
[ "${lines[0]}" == 'Error: Must specify at least one <test>' ]
[ "${lines[1]%% *}" == 'Usage:' ]
}
@test "invalid option prints message and usage instructions" {
run bats --invalid-option
[ $status -eq 1 ]
[ "${lines[0]}" == "Error: Bad command line option '--invalid-option'" ]
[ "${lines[1]%% *}" == 'Usage:' ]
}
@test "-v and --version print version number" {
run bats -v
[ $status -eq 0 ]
[ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ]
}
@test "-h and --help print help" {
run bats -h
[ $status -eq 0 ]
[ "${#lines[@]}" -gt 3 ]
}
@test "invalid filename prints an error" {
run bats nonexistent
[ $status -eq 1 ]
[ $(expr "$output" : ".*does not exist") -ne 0 ]
}
@test "empty test file runs zero tests" {
run bats "$FIXTURE_ROOT/empty.bats"
[ $status -eq 0 ]
[ "$output" = "1..0" ]
}
@test "one passing test" {
run bats "$FIXTURE_ROOT/passing.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
}
@test "summary passing tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "1 test, 0 failures" ]
}
@test "summary passing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[3]}" = "3 tests, 0 failures, 2 skipped" ]
}
@test "tap passing and skipping tests" {
run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
[ "${lines[2]}" = "ok 2 a skipped test with no reason # skip" ]
[ "${lines[3]}" = "ok 3 a skipped test with a reason # skip for a really good reason" ]
}
@test "summary passing and failing tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 0 ]
[ "${lines[4]}" = "2 tests, 1 failure" ]
}
@test "summary passing, failing and skipping tests" {
run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_failing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[5]}" = "3 tests, 1 failure, 1 skipped" ]
}
@test "tap passing, failing and skipping tests" {
run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_failing_and_skipping.bats"
[ $status -eq 0 ]
[ "${lines[0]}" = "1..3" ]
[ "${lines[1]}" = "ok 1 a passing test" ]
[ "${lines[2]}" = "ok 2 a skipping test # skip" ]
[ "${lines[3]}" = "not ok 3 a failing test" ]
}
@test "BATS_CWD is correctly set to PWD as validated by bats_trim_filename" {
local trimmed
bats_trim_filename "$PWD/foo/bar" 'trimmed'
printf 'ACTUAL: %s\n' "$trimmed" >&2
[ "$trimmed" = 'foo/bar' ]
}
@test "one failing test" {
run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
[ "${lines[0]}" = '1..1' ]
[ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing.bats, line 4)" ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
}
@test "one failing and one passing test" {
run bats "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ]
[ "${lines[0]}" = '1..2' ]
[ "${lines[1]}" = 'not ok 1 a failing test' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_and_passing.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
[ "${lines[4]}" = 'ok 2 a passing test' ]
}
@test "failing test with significant status" {
STATUS=2 run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "failing helper function logs the test case's line number" {
run bats "$FIXTURE_ROOT/failing_helper.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 failing helper function' ]
[ "${lines[2]}" = "# (from function \`failing_helper' in file $RELATIVE_FIXTURE_ROOT/test_helper.bash, line 6," ]
[ "${lines[3]}" = "# in test file $RELATIVE_FIXTURE_ROOT/failing_helper.bats, line 5)" ]
[ "${lines[4]}" = "# \`failing_helper' failed" ]
}
@test "test environments are isolated" {
run bats "$FIXTURE_ROOT/environment.bats"
[ $status -eq 0 ]
}
@test "setup is run once before each test" {
make_bats_test_suite_tmpdir
run bats "$FIXTURE_ROOT/setup.bats"
[ $status -eq 0 ]
run cat "$BATS_TEST_SUITE_TMPDIR/setup.log"
[ ${#lines[@]} -eq 3 ]
}
@test "teardown is run once after each test, even if it fails" {
make_bats_test_suite_tmpdir
run bats "$FIXTURE_ROOT/teardown.bats"
[ $status -eq 1 ]
run cat "$BATS_TEST_SUITE_TMPDIR/teardown.log"
[ ${#lines[@]} -eq 3 ]
}
@test "setup failure" {
run bats "$FIXTURE_ROOT/failing_setup.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/failing_setup.bats, line 2)" ]
[ "${lines[3]}" = "# \`false' failed" ]
}
@test "passing test with teardown failure" {
PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 2)" ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ]
}
@test "failing test with teardown failure" {
PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 truth' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 6)" ]
[ "${lines[3]}" = $'# `[ "$PASS" = 1 ]\' failed' ]
}
@test "teardown failure with significant status" {
PASS=1 STATUS=2 run bats "$FIXTURE_ROOT/failing_teardown.bats"
[ $status -eq 1 ]
[ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ]
}
@test "failing test file outside of BATS_CWD" {
make_bats_test_suite_tmpdir
cd "$BATS_TEST_SUITE_TMPDIR"
run bats "$FIXTURE_ROOT/failing.bats"
[ $status -eq 1 ]
[ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ]
}
@test "load sources scripts relative to the current test file" {
run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}
@test "load aborts if the specified script does not exist" {
HELPER_NAME="nonexistent" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 1 ]
}
@test "load sources scripts by absolute path" {
HELPER_NAME="${FIXTURE_ROOT}/test_helper.bash" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 0 ]
}
@test "load aborts if the script, specified by an absolute path, does not exist" {
HELPER_NAME="${FIXTURE_ROOT}/nonexistent" run bats "$FIXTURE_ROOT/load.bats"
[ $status -eq 1 ]
}
@test "output is discarded for passing tests and printed for failing tests" {
run bats "$FIXTURE_ROOT/output.bats"
[ $status -eq 1 ]
[ "${lines[6]}" = '# failure stdout 1' ]
[ "${lines[7]}" = '# failure stdout 2' ]
[ "${lines[11]}" = '# failure stderr' ]
}
@test "-c prints the number of tests" {
run bats -c "$FIXTURE_ROOT/empty.bats"
[ $status -eq 0 ]
[ "$output" = 0 ]
run bats -c "$FIXTURE_ROOT/output.bats"
[ $status -eq 0 ]
[ "$output" = 4 ]
}
@test "dash-e is not mangled on beginning of line" {
run bats "$FIXTURE_ROOT/intact.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 dash-e on beginning of line" ]
}
@test "dos line endings are stripped before testing" {
run bats "$FIXTURE_ROOT/dos_line.bats"
[ $status -eq 0 ]
}
@test "test file without trailing newline" {
run bats "$FIXTURE_ROOT/without_trailing_newline.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 truth" ]
}
@test "skipped tests" {
run bats "$FIXTURE_ROOT/skipped.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 a skipped test # skip" ]
[ "${lines[2]}" = "ok 2 a skipped test with a reason # skip a reason" ]
}
@test "skipped test with parens (pretty formatter)" {
run bats --pretty "$FIXTURE_ROOT/skipped_with_parens.bats"
[ $status -eq 0 ]
# Some systems (Alpine, for example) seem to emit an extra whitespace into
# entries in the 'lines' array when a carriage return is present from the
# pretty formatter. This is why a '+' is used after the 'skipped' note.
[[ "${lines[@]}" =~ "- a skipped test with parentheses in the reason (skipped: "+"a reason (with parentheses))" ]]
}
@test "extended syntax" {
emulate_bats_env
run bats-exec-suite -x "$FIXTURE_ROOT/failing_and_passing.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'begin 1 a failing test' ]
[ "${lines[2]}" = 'not ok 1 a failing test' ]
[ "${lines[5]}" = 'begin 2 a passing test' ]
[ "${lines[6]}" = 'ok 2 a passing test' ]
}
@test "pretty and tap formats" {
run bats --tap "$FIXTURE_ROOT/passing.bats"
tap_output="$output"
[ $status -eq 0 ]
run bats --pretty "$FIXTURE_ROOT/passing.bats"
pretty_output="$output"
[ $status -eq 0 ]
[ "$tap_output" != "$pretty_output" ]
}
@test "pretty formatter bails on invalid tap" {
run bats-format-tap-stream < <(printf "This isn't TAP.\nGood day to you.\n")
[ "$status" -eq 0 ]
[ "${lines[0]}" = "This isn't TAP." ]
[ "${lines[1]}" = "Good day to you." ]
}
@test "single-line tests" {
run bats "$FIXTURE_ROOT/single_line.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'ok 1 empty' ]
[ "${lines[2]}" = 'ok 2 passing' ]
[ "${lines[3]}" = 'ok 3 input redirection' ]
[ "${lines[4]}" = 'not ok 4 failing' ]
[ "${lines[5]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/single_line.bats, line 9)" ]
[ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ]
}
@test "testing IFS not modified by run" {
run bats "$FIXTURE_ROOT/loop_keep_IFS.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 loop_func" ]
}
@test "expand variables in test name" {
SUITE='test/suite' run bats "$FIXTURE_ROOT/expand_var_in_test_name.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 test/suite: test with variable in name" ]
}
@test "handle quoted and unquoted test names" {
run bats "$FIXTURE_ROOT/quoted_and_unquoted_test_names.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = "ok 1 single-quoted name" ]
[ "${lines[2]}" = "ok 2 double-quoted name" ]
[ "${lines[3]}" = "ok 3 unquoted name" ]
}
@test "ensure compatibility with unofficial Bash strict mode" {
local expected='ok 1 unofficial Bash strict mode conditions met'
# Run Bats under `set -u` to catch as many unset variable accesses as
# possible.
run bash -u "$BATS_ROOT/bin/bats" \
"$FIXTURE_ROOT/unofficial_bash_strict_mode.bats"
if [[ "$status" -ne 0 || "${lines[1]}" != "$expected" ]]; then
cat <<END_OF_ERR_MSG
This test failed because the Bats internals are violating one of the
constraints imposed by:
--------
$(< "$FIXTURE_ROOT/unofficial_bash_strict_mode.bash")
--------
See:
- https://github.com/sstephenson/bats/issues/171
- http://redsymbol.net/articles/unofficial-bash-strict-mode/
If there is no error output from the test fixture, run the following to
debug the problem:
$ bash -u bats $RELATIVE_FIXTURE_ROOT/unofficial_bash_strict_mode.bats
If there's no error output even with this command, make sure you're using the
latest version of Bash, as versions before 4.1-alpha may not produce any error
output for unset variable accesses.
If there's no output even when running the latest Bash, the problem may reside
in the DEBUG trap handler. A particularly sneaky issue is that in Bash before
4.1-alpha, an expansion of an empty array, e.g. "\${FOO[@]}", is considered
an unset variable access. The solution is to add a size check before the
expansion, e.g. [[ "\${#FOO[@]}" -ne 0 ]].
END_OF_ERR_MSG
emit_debug_output && return 1
fi
}
@test "parse @test lines with various whitespace combinations" {
run bats "$FIXTURE_ROOT/whitespace.bats"
[ $status -eq 0 ]
[ "${lines[1]}" = 'ok 1 no extra whitespace' ]
[ "${lines[2]}" = 'ok 2 tab at beginning of line' ]
[ "${lines[3]}" = 'ok 3 tab before description' ]
[ "${lines[4]}" = 'ok 4 tab before opening brace' ]
[ "${lines[5]}" = 'ok 5 tabs at beginning of line and before description' ]
[ "${lines[6]}" = 'ok 6 tabs at beginning, before description, before brace' ]
[ "${lines[7]}" = 'ok 7 extra whitespace around single-line test' ]
[ "${lines[8]}" = 'ok 8 no extra whitespace around single-line test' ]
[ "${lines[9]}" = 'ok 9 parse unquoted name between extra whitespace' ]
[ "${lines[10]}" = 'ok 10 {' ] # unquoted single brace is a valid description
[ "${lines[11]}" = 'ok 11 ' ] # empty name from single quote
}
@test "duplicate tests cause a warning on stderr" {
run bats "$FIXTURE_ROOT/duplicate-tests.bats"
[ $status -eq 1 ]
local expected='bats warning: duplicate test name(s) in '
expected+="$FIXTURE_ROOT/duplicate-tests.bats: test_gizmo_test"
printf 'expected: "%s"\n' "$expected" >&2
printf 'actual: "%s"\n' "${lines[0]}" >&2
[ "${lines[0]}" = "$expected" ]
printf 'num lines: %d\n' "${#lines[*]}" >&2
[ "${#lines[*]}" = "7" ]
}
@test "sourcing a nonexistent file in setup produces error output" {
run bats "$FIXTURE_ROOT/source_nonexistent_file_in_setup.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails in setup' ]
[ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file_in_setup.bats, line 2)" ]
[ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ]
}
@test "referencing unset parameter in setup produces error output" {
run bats "$FIXTURE_ROOT/reference_unset_parameter_in_setup.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 referencing unset parameter fails in setup' ]
[ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter_in_setup.bats, line 3)" ]
[ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ]
}
@test "sourcing a nonexistent file in test produces error output" {
run bats "$FIXTURE_ROOT/source_nonexistent_file.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file.bats, line 2)" ]
[ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ]
}
@test "referencing unset parameter in test produces error output" {
run bats "$FIXTURE_ROOT/reference_unset_parameter.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 referencing unset parameter fails' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter.bats, line 3)" ]
[ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ]
}
@test "sourcing a nonexistent file in teardown produces error output" {
run bats "$FIXTURE_ROOT/source_nonexistent_file_in_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails in teardown' ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file_in_teardown.bats, line 2)" ]
[ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ]
}
@test "referencing unset parameter in teardown produces error output" {
run bats "$FIXTURE_ROOT/reference_unset_parameter_in_teardown.bats"
[ $status -eq 1 ]
[ "${lines[1]}" = 'not ok 1 referencing unset parameter fails in teardown' ]
[ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter_in_teardown.bats, line 3)" ]
[ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ]
}
@test "execute exported function without breaking failing test output" {
exported_function() { return 0; }
export -f exported_function
run bats "$FIXTURE_ROOT/exported_function.bats"
[ $status -eq 1 ]
[ "${lines[0]}" = "1..1" ]
[ "${lines[1]}" = "not ok 1 failing test" ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/exported_function.bats, line 7)" ]
[ "${lines[3]}" = "# \`false' failed" ]
[ "${lines[4]}" = "# a='exported_function'" ]
}
@test "output printed even when no final newline" {
run bats "$FIXTURE_ROOT/no-final-newline.bats"
printf 'num lines: %d\n' "${#lines[@]}" >&2
printf 'LINE: %s\n' "${lines[@]}" >&2
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 7 ]
[ "${lines[1]}" = 'not ok 1 no final newline' ]
[ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/no-final-newline.bats, line 2)" ]
[ "${lines[3]}" = "# \`printf 'foo\nbar\nbaz' >&2 && return 1' failed" ]
[ "${lines[4]}" = '# foo' ]
[ "${lines[5]}" = '# bar' ]
[ "${lines[6]}" = '# baz' ]
}
@test "parallel test execution with --jobs" {
type -p parallel &>/dev/null || skip "--jobs requires GNU parallel"
SECONDS=0
run bats --jobs 10 "$FIXTURE_ROOT/parallel.bats"
duration="$SECONDS"
[ "$status" -eq 0 ]
# Make sure the lines are in-order.
[[ "${lines[0]}" == "1..10" ]]
for t in {1..10}; do
[[ "${lines[$t]}" == "ok $t slow test $t" ]]
done
# In theory it should take 3s, but let's give it bit of extra time instead.
[[ "$duration" -lt 20 ]]
}