-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathassert.command.bash
More file actions
753 lines (667 loc) · 19.2 KB
/
Copy pathassert.command.bash
File metadata and controls
753 lines (667 loc) · 19.2 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#!/usr/bin/env bash
##
# @file
# Assertions for commands executed with 'run'.
#
##
# Asserts that the last command succeeded.
#
# Arguments:
# 1. output: Exact output to additionally assert on. Optional.
#
# Globals:
# status: Exit status of the last 'run' call.
# output: Output captured by the last 'run' call.
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_success() {
# shellcheck disable=SC2154
if [ "${status-}" -ne 0 ]; then
local -a rows=("status" "$(_command_describe_status "${status}")")
[ -z "${output-}" ] || rows+=("output" "${output}")
[ -z "${stderr-}" ] || rows+=("stderr" "${stderr}")
format_error "Command failed" "${rows[@]}" | flunk
elif [ "$#" -gt 0 ]; then
assert_output "${1}"
fi
}
##
# Asserts that the last command failed.
#
# Arguments:
# 1. --status expected: Exact exit status to assert on, as two arguments.
# Optional, recognised only in first position. Deprecated, use
# 'assert_failure_status' instead.
# 2. output: Exact output to additionally assert on. Optional.
#
# Globals:
# status: Exit status of the last 'run' call.
# output: Output captured by the last 'run' call.
# stderr: Standard error captured by the last 'run --separate-stderr' call.
# BATS_HELPERS_DEPRECATION_QUIET: Non-empty suppresses this library's
# deprecation notices.
##
assert_failure() {
local expected=""
if [ "${1-}" = "--status" ]; then
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: 'assert_failure --status' will be removed in v2.1. Use 'assert_failure_status' instead." >&3
if [ "$#" -lt 2 ]; then
flunk "Option '--status' requires an exit status."
return 1
fi
_command_validate_status "${2}" || return 1
if [ "${2}" -eq 0 ]; then
flunk "A failure cannot have exit status 0. Use 'assert_success' instead."
return 1
fi
expected="${2}"
shift 2
fi
# shellcheck disable=SC2154
if [ "${status-}" -eq 0 ]; then
local -a rows=()
[ -z "${output-}" ] || rows+=("output" "${output}")
[ -z "${stderr-}" ] || rows+=("stderr" "${stderr}")
format_error "Command succeeded, but should have failed" "${rows[@]}" | flunk
return 1
fi
if [ -n "${expected}" ]; then
assert_status "${expected}" || return 1
fi
if [ "$#" -gt 0 ]; then
assert_output "${1}"
fi
}
##
# Asserts that the last command failed with an exact exit status.
#
# Arguments:
# 1. expected: Exit status to assert on.
# 2. output: Exact output to additionally assert on. Optional.
#
# Globals:
# status: Exit status of the last 'run' call.
# output: Output captured by the last 'run' call.
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_failure_status() {
_command_validate_status "${1-}" || return 1
if [ "${1}" -eq 0 ]; then
flunk "A failure cannot have exit status 0. Use 'assert_success' instead."
return 1
fi
local expected="${1}"
shift
assert_failure "$@" || return 1
assert_status "${expected}"
}
##
## Exit status assertions.
##
##
# Asserts that the last command exited with an exact status.
#
# Arguments:
# 1. expected: Expected exit status.
#
# Globals:
# status: Exit status of the last 'run' call.
# output: Output captured by the last 'run' call.
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_status() {
if [ "$#" -eq 0 ]; then
flunk "An expected exit status is required."
return 1
fi
_command_validate_status "${1}" || return 1
# shellcheck disable=SC2154
if [ "${status-}" -eq "${1}" ]; then
return 0
fi
local -a rows=("expected" "${1}" "actual" "$(_command_describe_status "${status}")")
[ "${output-}" = "" ] || rows+=("output" "${output}")
[ "${stderr-}" = "" ] || rows+=("stderr" "${stderr}")
format_error "Command exited with an unexpected status" "${rows[@]}" | flunk
}
##
# Asserts that the last command exited with the general error status.
#
# Globals:
# status: Exit status of the last 'run' call.
##
assert_status_general_error() {
assert_status 1
}
##
# Asserts that the last command exited with the command not found status.
#
# Globals:
# status: Exit status of the last 'run' call.
##
assert_status_command_not_found() {
assert_status 127
}
##
# Validates a value given as an expected exit status.
#
# Arguments:
# 1. expected: Value to validate.
##
_command_validate_status() {
if ! [[ ${1-} =~ ^[0-9]+$ ]] || [ "${1}" -gt 255 ]; then
flunk "Exit status '${1-}' is not an integer between 0 and 255."
return 1
fi
}
##
# Describes an exit status, naming the ones that mean more than a failure.
#
# A status of 127 is what a shell returns for a command it could not find, and a
# status above 128 is how it reports a process a signal killed rather than one
# that chose its own status. Either reads as an ordinary failure of the code
# under test unless it is named.
#
# Arguments:
# 1. status: Exit status to describe.
#
# Outputs:
# STDOUT: The status, followed by what it means when it means something.
##
_command_describe_status() {
local code="${1}"
if [ "${code}" -eq 127 ]; then
printf '%s (command not found)\n' "${code}"
return 0
fi
local signal=$((code - 128))
if [ "${signal}" -ge 1 ]; then
local name
# A number the platform does not name is not one of its signals, so the
# status is left to stand for itself rather than named as one.
if name="$(kill -l "${signal}" 2>/dev/null)" && [ -n "${name}" ]; then
printf '%s (killed by SIG%s)\n' "${code}" "${name}"
return 0
fi
fi
printf '%s\n' "${code}"
}
##
# Asserts that the output of the last command equals a string.
#
# Arguments:
# 1. expected: Expected output. Optional, read from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output() {
# shellcheck disable=SC2154
_command_assert_equal "output" "${output-}" "$@"
}
##
# Asserts that a needle matches a captured stream.
#
# Arguments:
# 1. subject: Which stream the haystack is - 'output' or 'stderr'. Opens the
# title and keys the row the stream is reported under.
# 2. anchor: Where the needle must sit - 'anywhere', 'start' or 'end'.
# 3. negate: '1' to assert that the needle does not match.
# 4. mode: How the needle is read - 'literal', 'regex' or 'format'.
# 5. case_sensitive: '1' to match case-sensitively, '0' to ignore case.
# 6. haystack: Captured stream to search.
# 7. needle: String to search for. Optional, read from STDIN when omitted.
##
_command_assert_match() {
local needle
if [ "$#" -eq 6 ]; then
needle="$(cat -)"
elif [ "$#" -eq 7 ]; then
needle="${7}"
else
flunk "A needle is required."
return 1
fi
_string_assert_match "${1}" "${2}" "${3}" "${4}" "${5}" "${6}" "${needle}"
}
##
# Asserts that a captured stream equals a string.
#
# Arguments:
# 1. subject: Which stream the value is - 'output' or 'stderr'. Opens the
# title of the report.
# 2. actual: Captured stream to compare.
# 3. expected: Expected value. Optional, read from STDIN when omitted.
##
_command_assert_equal() {
local subject="${1}"
local actual="${2}"
local expected
if [ "$#" -eq 2 ]; then
expected="$(cat -)"
else
expected="${3}"
fi
if [ "${expected}" = "${actual}" ]; then
return 0
fi
format_error "$(_string_subject_noun "${subject}") does not equal string" "expected" "${expected}" "actual" "${actual}" | flunk
}
##
## Output containment assertions.
##
##
# Asserts that the output of the last command contains a substring,
# ignoring case.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_contains() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "literal" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command contains a substring,
# case-sensitively.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_contains_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "literal" 1 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not contain a substring,
# ignoring case.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_contains() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "literal" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not contain a substring,
# case-sensitively.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_contains_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "literal" 1 "${output-}" "$@"
}
##
## Output regular expression assertions.
##
##
# Asserts that the output of the last command matches a regular expression,
# ignoring case.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_matches() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "regex" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command matches a regular expression,
# case-sensitively.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_matches_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "regex" 1 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not match a regular
# expression, ignoring case.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_matches() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "regex" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not match a regular
# expression, case-sensitively.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_matches_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "regex" 1 "${output-}" "$@"
}
##
## Output format assertions.
##
##
# Asserts that the output of the last command matches a format string, ignoring
# case.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_matches_format() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "format" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command matches a format string,
# case-sensitively.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_matches_format_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 0 "format" 1 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not match a format string,
# ignoring case.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_matches_format() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "format" 0 "${output-}" "$@"
}
##
# Asserts that the output of the last command does not match a format string,
# case-sensitively.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# output: Output captured by the last 'run' call.
##
assert_output_not_matches_format_case() {
# shellcheck disable=SC2154
_command_assert_match "output" "anywhere" 1 "format" 1 "${output-}" "$@"
}
##
## Standard error assertions.
##
##
# Asserts that standard error was captured separately from the output.
#
# Only 'run --separate-stderr' populates 'stderr'. The check is that the
# variable is set rather than that it holds text, so that a command which wrote
# nothing to standard error is told apart from one whose standard error was
# never captured at all.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_captured() {
if [ -z "${stderr+set}" ]; then
flunk "Stderr was not captured. Run the command with 'run --separate-stderr'."
return 1
fi
}
##
# Asserts that the standard error of the last command equals a string.
#
# Arguments:
# 1. expected: Expected standard error. Optional, read from STDIN when
# omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr() {
assert_stderr_captured || return 1
_command_assert_equal "stderr" "${stderr}" "$@"
}
##
# Asserts that the last command wrote nothing to standard error.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_empty() {
assert_stderr_captured || return 1
if [ -z "${stderr}" ]; then
return 0
else
format_error "Stderr is not empty" "stderr" "${stderr}" | flunk
fi
}
##
## Standard error containment assertions.
##
##
# Asserts that the standard error of the last command contains a substring,
# ignoring case.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_contains() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "literal" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command contains a substring,
# case-sensitively.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_contains_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "literal" 1 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not contain
# a substring, ignoring case.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_contains() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "literal" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not contain
# a substring, case-sensitively.
#
# Arguments:
# 1. needle: Substring to search for. Optional, read from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_contains_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "literal" 1 "${stderr}" "$@"
}
##
## Standard error regular expression assertions.
##
##
# Asserts that the standard error of the last command matches a regular
# expression, ignoring case.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_matches() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "regex" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command matches a regular
# expression, case-sensitively.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_matches_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "regex" 1 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not match a regular
# expression, ignoring case.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_matches() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "regex" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not match a regular
# expression, case-sensitively.
#
# Arguments:
# 1. needle: Extended regular expression to match. Optional, read from STDIN
# when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_matches_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "regex" 1 "${stderr}" "$@"
}
##
## Standard error format assertions.
##
##
# Asserts that the standard error of the last command matches a format string,
# ignoring case.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_matches_format() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "format" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command matches a format string,
# case-sensitively.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_matches_format_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 0 "format" 1 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not match a format
# string, ignoring case.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_matches_format() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "format" 0 "${stderr}" "$@"
}
##
# Asserts that the standard error of the last command does not match a format
# string, case-sensitively.
#
# Arguments:
# 1. needle: Format string, see 'string_format_to_regex'. Optional, read
# from STDIN when omitted.
#
# Globals:
# stderr: Standard error captured by the last 'run --separate-stderr' call.
##
assert_stderr_not_matches_format_case() {
assert_stderr_captured || return 1
_command_assert_match "stderr" "anywhere" 1 "format" 1 "${stderr}" "$@"
}