-
Notifications
You must be signed in to change notification settings - Fork 0
/
shelter.sh
1372 lines (1165 loc) · 38.9 KB
/
shelter.sh
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# MIT license
# Copyright 2018 Sergej Alikov <[email protected]>
## @file
## @author Sergej Alikov <[email protected]>
## @copyright MIT License
## @brief Shell-based testing framework
set -euo pipefail
declare -g SHELTER_SED_CMD
declare -g SHELTER_MKTEMP_CMD
declare -g SHELTER_OS_IMPLEMENTATION
declare -g SHELTER_SAVED_EXIT_TRAP
declare -g SHELTER_DEBUG="${SHELTER_DEBUG:-FALSE}"
SHELTER_OS_IMPLEMENTATION=$(uname -s)
# We only support GNU sed
case "$SHELTER_OS_IMPLEMENTATION" in
FreeBSD|OpenBSD|Darwin)
SHELTER_SED_CMD='gsed'
SHELTER_MKTEMP_CMD='gmktemp'
;;
*)
SHELTER_SED_CMD='sed'
SHELTER_MKTEMP_CMD='mktemp'
;;
esac
if ! command -v "$SHELTER_SED_CMD" &>/dev/null; then
printf 'Please install %s\n' "$SHELTER_SED_CMD"
fi
if ! command -v "$SHELTER_MKTEMP_CMD" &>/dev/null; then
printf '%s is missing. Please install coreutils\n' "$SHELTER_SED_CMD"
fi
declare -g SHELTER_PROG_DIR
SHELTER_PROG_DIR=$(dirname "${BASH_SOURCE[0]:-}")
# shellcheck source=shelter-config.sh
source "${SHELTER_PROG_DIR%/}/shelter-config.sh"
declare -ri SHELTER_BLOCK_ROOT=0
declare -ri SHELTER_BLOCK_SUITES=1
declare -ri SHELTER_BLOCK_SUITE=2
declare -ri SHELTER_BLOCK_TESTCASE=3
declare -Ag SHELTER_PATCHED_COMMANDS=()
declare -Ag SHELTER_PATCHED_COMMAND_STRATEGIES=()
## @var SHELTER_FORMATTER_ERREXIT_ON
## @brief Set the error exit condition for the built-in formatters
## @details The following values are supported:
## - none Exit with 0 even when there are failing tests
## - failures-present Run all tests, exit with non-zero code if at least
## one test has failed
## - first-failing Exit with non-zero code immediately after the first
## failed test
declare -g SHELTER_FORMATTER_ERREXIT_ON='failures-present'
## @var SHELTER_SKIP_TEST_CASES
## @brief A list of test case commands to skip
## @details When set before executing test suites allows to skip
## certain test cases
declare -ag SHELTER_SKIP_TEST_CASES=()
# shellcheck disable=SC2034
# This variable is used internally by the assert_ functions
# It provides a side channel for assertion messages
[[ -n "${SHELTER_ASSERT_FD:-}" ]] || exec {SHELTER_ASSERT_FD}>&2
SHELTER_TEMP_DIR=$("$SHELTER_MKTEMP_CMD" -d)
declare -rg SHELTER_TEMP_DIR
mkdir "${SHELTER_TEMP_DIR}/bin"
PATH="${SHELTER_TEMP_DIR}/bin:${PATH}"
_shelter_cleanup_temp_dir () {
declare name
if [[ "${#SHELTER_PATCHED_COMMANDS[@]}" -gt 0 ]]; then
for name in "${!SHELTER_PATCHED_COMMANDS[@]}"; do
case "${SHELTER_PATCHED_COMMAND_STRATEGIES["$name"]:-}" in
mount)
>&2 printf 'Removing %s patch_command mount\n' "$name"
umount "$name"
rm -f -- "${SHELTER_PATCHED_COMMANDS["$name"]}"
;;
path)
>&2 printf 'Removing %s patch_command path override\n' "${SHELTER_PATCHED_COMMANDS["$name"]}"
rm -f -- "${SHELTER_PATCHED_COMMANDS["$name"]}"
;;
esac
done
fi
}
_shelter_cleanup () {
rmdir "${SHELTER_TEMP_DIR}/bin"
rmdir "$SHELTER_TEMP_DIR"
declare -a original_trap
eval "original_trap=(${SHELTER_SAVED_EXIT_TRAP})"
if [[ "${#original_trap[@]}" -gt 0 ]]; then
eval "${original_trap[2]:-}"
fi
}
SHELTER_SAVED_EXIT_TRAP=$(trap -p EXIT)
trap _shelter_cleanup EXIT
_keep_fds () {
declare -a keep_fds=()
declare -i fd_nr
for fd_nr in "$@"; do
keep_fds[fd_nr]=yes
done
declare fd
for fd in /proc/"$BASHPID"/fd/*; do
fd_nr=$(basename "$fd")
if [[ -n "${keep_fds[$fd_nr]:+notempty}" ]]; then
continue
fi
eval "exec ${fd_nr}>&-"
done
}
_annotated_eval () {
eval "$1" 2> >("$SHELTER_SED_CMD" -u "s/^/STDERR /") > >("$SHELTER_SED_CMD" -u "s/^/STDOUT /")
}
# This function is used internally to emit assertion messages
# to SHELTER_ASSERT_FD while retaining the exit code of a
# previous command.
# It may only be used inside another function!
# Exmaple:
# $ exec {SHELTER_ASSERT_FD}>&1
# $ test_fn () { bash -c 'exit 5' || _assert_msg 'FAILED!'; }
# $ test_fn || rc="$?"
# test_fn FAILED!
# $ echo "$rc"
# 5
_assert_msg () {
local rc="$?"
local msg="$1"
local assert_cmd="${FUNCNAME[1]}"
printf '%s %s\n' "$assert_cmd" "$msg" >&"${SHELTER_ASSERT_FD}"
return "$rc"
}
## @fn supported_shelter_versions ()
## @brief Return success if the current Shelter version matches at least one
## of the supplied versions
## @details Use this command to assert you are using a compatible version
## of Shelter framework by declaring a list of supported versions.
## This allows you to fail early when an unsupported version of Shelter is
## installed
## @param version version to match. Format is
## MAJOR or MAJOR.MINOR or MAJOR.MINOR.PATCH. May be specified multiple
## times
##
## Example:
##
## @code{.sh}
## supported_shelter_versions 0.5.0 0.6.1 1 2 4.9
## @endcode
supported_shelter_versions () {
declare -r SEMVER_RE='^([0-9]+).([0-9]+).([0-9]+)(-([0-9A-Za-z.-]+))?(\+([0-9A-Za-z.-]))?$'
declare -r VER_RE='^([0-9]+)(.([0-9]+))?(.([0-9]+))?$'
if [[ "$SHELTER_VERSION" =~ $SEMVER_RE ]]; then
declare major="${BASH_REMATCH[1]}"
declare minor="${BASH_REMATCH[2]}"
declare patch="${BASH_REMATCH[3]}"
declare version
for version in "$@"; do
[[ "$version" =~ $VER_RE ]] || continue
[[ "$major" -eq "${BASH_REMATCH[1]}" ]] || continue
if [[ -n "${BASH_REMATCH[3]:+x}" ]]; then
[[ "$minor" -eq "${BASH_REMATCH[3]}" ]] || continue
fi
if [[ -n "${BASH_REMATCH[5]:+x}" ]]; then
[[ "$patch" -eq "${BASH_REMATCH[5]}" ]] || continue
fi
return 0
done
fi
printf >&2 'Unsupported version %s of Shelter detected. Supported versions are: %s\n' \
"$SHELTER_VERSION" \
"$*"
return 1
}
## @fn assert_stdout ()
## @brief Assert the STDOUT output of the supplied command matches the expected
## @details In case the STDOUT output does not match the expected one -
## a diff will be printed to STDOUT, an assertion name and message
## will be output to SHELTER_ASSERT_FD, and the function will exit
## with a non-zero exit code
## @param cmd command. Will be passed to 'eval'
## @param expected_file OPTIONAL. File containing the expected output.
## Use dash (the default) for STDIN. Process substitution will also work
## @param msg assertion message
##
## Examples
##
## Using process substitution
## @code{.sh}
## assert_stdout 'bc <<< 1+1' <(echo '2')
## @endcode
##
## Using STDIN to pass the expected output
## @code{.sh}
## assert_stdout 'bc << 1+1' <<< 2
## @endcode
assert_stdout () {
declare cmd="$1"
declare expected_file="${2:--}"
declare msg="${3:-STDOUT of \"${cmd}\" does not match the contents of \"${expected_file}\"}"
diff -du <(_keep_fds 1 2 255; eval "$cmd") "$expected_file" || _assert_msg "$msg"
}
## @fn assert_success ()
## @brief Assert the command executes with a zero exit code
## @details If the supplied command fails - an assertion name and message
## will be output to SHELTER_ASSERT_FD and the function will exit
## with the same error code as the supplied command did
## @param cmd command. Will be passed to 'eval'
## @param msg OPTIONAL assertion message
##
## Example
##
## @code{.sh}
## assert_success 'systemctl -q is-active sshd' 'SSH daemon is not running!'
## @endcode
assert_success () {
declare cmd="$1"
declare msg="${2:-\"${cmd}\" failed}"
declare -i rc
set +e
(
set -e
_keep_fds 1 2 255
eval "$cmd"
)
rc="$?"
set -e
# shellcheck disable=SC2181
[[ "$rc" -eq 0 ]] || _assert_msg "$msg"
}
## @fn assert_fail ()
## @brief Assert command executes with a non-zero exit code
## @details If the supplied command succeeds - an assertion name and message
## will be output to SHELTER_ASSERT_FD and the function will exit
## with a non-zero exit code
## @param cmd command. Will be passed to 'eval'
## @param exit_code OPTIONAL expected exit code.
## Must be greater than zero or an empty string (the default) which
## will match any non-zero exit code
## @param msg OPTIONAL assertion message
##
## Examples
##
## Catching specific exit code
## @code{.sh}
## assert_fail 'ls --invalid-arg' 2
## @endcode
##
## Catching any non-zero exit code
## @code{.sh}
## assert_fail 'systemctl -q is-enabled httpd' '' 'httpd service should be disabled'
## @endcode
assert_fail () {
declare cmd="$1"
declare exit_code="${2:-}"
declare msg="${3:-\"${cmd}\" did not fail}"
if [[ "$exit_code" = '0' ]]; then
printf 'Invalid value for exit_code (%s)\n' "$exit_code" >&2
return 1
fi
declare -i rc=0
set +e
(
set -e
_keep_fds 1 2 255
eval "$cmd"
)
rc="$?"
set -e
if [[ -z "$exit_code" ]]; then
[[ "$rc" -gt 0 ]] || _assert_msg "$msg"
else
[[ "$rc" -eq "$exit_code" ]] || _assert_msg "$msg"
fi
}
## @fn assert_stdout_contains ()
## @brief Assert a line in STDOUT of the supplied command will match the regex
## @details In case none of the STDOUT lines match the regex -
## an assertion name and message will be output to SHELTER_ASSERT_FD,
## and the function will exit with a non-zero exit code
## @param cmd command. Will be passed to 'eval'
## @param regex. Regex to match (ERE)
##
## Example
##
## @code{.sh}
## assert_stdout_contains 'echo Hello World' 'World$'
## @endcode
assert_stdout_contains () {
declare cmd="$1"
declare regex="${2}"
declare msg="${3:-STDOUT of \"${cmd}\" does not contain \"${regex}\"}"
grep -E "$regex" <(_keep_fds 1 2 255; eval "$cmd") &>/dev/null || _assert_msg "$msg"
}
## @fn assert_stdout_not_contains ()
## @brief Assert none of the STDOUT lines will match the regex
## @details In case of a match - an assertion name and message
## will be output to SHELTER_ASSERT_FD, and the function will
## exit with a non-zero exit code
## @param cmd command. Will be passed to 'eval'
## @param regex. Regex to match (ERE)
##
## Example
##
## @code{.sh}
## assert_stdout_not_contains 'echo Hello World' 'foo'
## @endcode
assert_stdout_not_contains () {
declare cmd="$1"
declare regex="${2}"
declare msg="${3:-STDOUT of \"${cmd}\" contains \"${regex}\"}"
! grep -E "$regex" <(_keep_fds 1 2 255; eval "$cmd") &>/dev/null || _assert_msg "$msg"
}
## @fn shelter_run_test_case ()
## @brief Run a command in an isolated environment and return an annotated output
## @details The command is executed with errexit and nounset enabled.
## STDOUT and STDERR are processed by separate threads, therefore might
## be slightly out of order in relation to each other. Ordering within a
## single stream (STDOUT or STDERR) is guaranteed to be correct.
## The output is machine-readable.
## @param cmd command. Will be passed to 'eval'
##
## Example (number of variables reduced for clarity)
##
## @code{.sh}
## $ shelter_run_test_case 'echo Hi; echo Bye >&2; echo Hi again'
## CMD echo Hi; echo Bye >&2; echo Hi again
## ENV BASH declare\ --\ BASH=\"/usr/bin/bash\"
## ENV TERM declare\ --\ TERM=\"dumb\"
## ENV TIMEFORMAT declare\ --\ TIMEFORMAT=\"%R\"
## ENV UID declare\ -ir\ UID=\"1000\"
## ENV _ declare\ --\ _=\"var\"
## EXIT 0
## TIME 0.009
## STDOUT 1 Hi
## STDERR 2 Bye
## STDOUT 3 Hi again
## @endcode
shelter_run_test_case () {
if [[ "${#SHELTER_SKIP_TEST_CASES[@]}" -gt 0 ]]; then
declare cmd
for cmd in "${SHELTER_SKIP_TEST_CASES[@]}"; do
if [[ "$cmd" = "$1" ]]; then
printf 'SKIPPED %s\n' "$1"
return 0
fi
done
fi
if [[ "$SHELTER_DEBUG" == TRUE ]]; then
printf -- '- Executing %s\n' "$1" >&2
fi
printf 'CMD %s\n' "$1"
declare var
while read -r var; do
printf 'ENV %s %q\n' "$var" "$(declare -p "$var")"
done < <(compgen -A variable)
unset var
# Isolate subshells for wait.
(
{
TIMEFORMAT=%R
# Backup shell options. errexit is propagated to process
# substitutions, therefore no special handling is needed
declare -a shelter_shopt_backup
readarray -t shelter_shopt_backup < <(shopt -po)
set +e
# sequence numbers added by the last component allow
# user to perform sorting (sort -V) to split STDOUT and STDERR into
# separate blocks (preserving the correct order within the block)
# and reassemble back into a single block later (sort -V -k 2).
time eval '(set -eu; _keep_fds 1 2 255 "$SHELTER_ASSERT_FD"; unset TIMEFORMAT shelter_shopt_backup; trap "_annotated_eval _shelter_cleanup_temp_dir" EXIT; _annotated_eval "$1")' \
| { grep -n '' || true; } \
| "$SHELTER_SED_CMD" -u 's/^\([0-9]\+\):\(STDOUT\|STDERR\) /\2 \1 /'
declare rc="$?"
# Restore shell options
declare cmd
for cmd in "${shelter_shopt_backup[@]}"; do
eval "$cmd"
done
# Close the file descriptor, otherwise the below "assert" process
# substitution will keep running in background causing the later wait
# to get stuck.
exec {SHELTER_ASSERT_FD}>&-
} 2> >("$SHELTER_SED_CMD" -u "s/^/TIME /") {SHELTER_ASSERT_FD}> >("$SHELTER_SED_CMD" -u "s/^/ASSERT /")
# Wait for all process substitution subshells to finish.
# I think this should catch _all_ of them, including those in _annotated_eval.
wait
printf 'EXIT %s\n' "$rc"
)
}
## @fn shelter_run_test_class ()
## @brief Run a pattern-based list of functions as test cases
## @details Pass every function name starting with a specified prefix to
## the shelter_run_test_case command. A line containing "CLASS $class_name"
## will be added to the end of every test case output block
## @param class_name class name
## @param fn_prefix function prefix. All functions starting with
## this prefix (in the current scope) will be executed.
##
## Example (assumes there are "test_1" and "test_2" functions.
## Outut reduced for clarity)
##
## @code{.sh}
## $ shelter_run_test_class testclass test_
## CMD test_1
## ...
## CLASS testclass
## CMD test_2
## ...
## CLASS testclass
## @endcode
shelter_run_test_class () {
declare fn
while read -r fn; do
shelter_run_test_case "$fn"
printf 'CLASS %s\n' "$1"
done < <(compgen -A function "$2")
}
## @fn shelter_run_test_suite ()
## @brief Run a command which runs multiple tests cases as a test suite
## @details Suite data starting with a SUITE-* set of
## keywords will be added at the top
## @param cmd command. Will be passed to 'eval'
##
## Example (assumes there is a "suite_1" command which executes
## "shelter_run_test_case test_1" and "shelter_run_test_case test_2" commands.
## Outut reduced for clarity)
##
## @code{.sh}
## $ shelter_run_test_suite suite_1
## SUITE ERRORS 1
## SUITE FAILURES 0
## SUITE NAME suite_1
## SUITE SKIPPED 0
## SUITE TESTS 2
## SUITE TIME 1.51
## CMD test_1
## EXIT 0
## TIME 0.01
## ...
## CMD test_2
## EXIT 1
## TIME 1.5
## ...
## @endcode
shelter_run_test_suite () {
declare -i shelter_suite_tests=0
declare -i shelter_suite_errors=0
declare -i shelter_suite_failures=0
declare -i shelter_suite_skipped=0
declare -i shelter_suite_line=1
declare shelter_suite_time='0.0'
declare key
declare value
{
printf '0 SUITE_NAME %s\n' "$1"
while read -r key value; do
case "$key" in
CMD)
shelter_suite_tests+=1
;;
SKIPPED)
shelter_suite_tests+=1
shelter_suite_skipped+=1
;;
EXIT)
[[ "$value" = '0' ]] || shelter_suite_errors+=1
;;
ASSERT)
shelter_suite_failures+=1
;;
TIME)
shelter_suite_time=$(bc <<< "$shelter_suite_time + $value" | "$SHELTER_SED_CMD" 's/^\./0./')
;;
esac
printf '%d %s %s\n' "$shelter_suite_line" "$key" "$value"
shelter_suite_line+=1
done < <(unset shelter_suite_tests shelter_suite_errors shelter_suite_failures shelter_suite_skipped shelter_suite_line shelter_suite_time; eval "$1")
printf '0 SUITE_TESTS %s\n' "$shelter_suite_tests"
printf '0 SUITE_ERRORS %s\n' "$((shelter_suite_errors - shelter_suite_failures))"
printf '0 SUITE_FAILURES %s\n' "$shelter_suite_failures"
printf '0 SUITE_SKIPPED %s\n' "$shelter_suite_skipped"
printf '0 SUITE_TIME %s\n' "$shelter_suite_time"
} | sort -n | "$SHELTER_SED_CMD" -u 's/^[0-9]\+ //'
}
## @fn shelter_run_test_suites ()
## @brief Run a pattern-based list of functions as test suites
## @details Pass every function name starting with a specified prefix to
## the shelter_run_test_suite command. Aggregated suite data starting
## with a SUITES-* set of keywords will be added at the top
## @param name a name of the collection
## @param fn_prefix function prefix. All functions starting with
## this prefix (in the current scope) will be executed.
##
## Example (assumes there is a "suite_1" command which executes
## "shelter_run_test_case test_1" and "shelter_run_test_case test_2" commands,
## and a "suite_2" command which executes "shelter_run_test_case test_3"
## command. Outut reduced for clarity)
##
## @code{.sh}
## $ shelter_run_test_suites 'A collection' suite_
## SUITES-ERRORS 1
## SUITES-FAILURES 0
## SUITES-NAME A collection
## SUITES-SKIPPED 0
## SUITES-TESTS 3
## SUITES-TIME 1.5
## SUITE-ERRORS 1
## SUITE-FAILURES 0
## SUITE-NAME suite_1
## SUITE-SKIPPED 0
## SUITE-TESTS 2
## SUITE-TIME 1
## CMD cmd_1
## EXIT 0
## TIME 0.4
## ...
## CMD cmd_2
## EXIT 1
## TIME 0.6
## ...
## SUITE-ERRORS 0
## SUITE-FAILURES 0
## SUITE-NAME suite_2
## SUITE-SKIPPED 0
## SUITE-TESTS 2
## SUITE-TIME 0.5
## CMD cmd_3
## EXIT 0
## TIME 0.5
## ...
## @endcode
shelter_run_test_suites () {
declare -i shelter_suites_tests=0
declare -i shelter_suites_errors=0
declare -i shelter_suites_failures=0
declare -i shelter_suites_line=1
declare shelter_suites_time='0.0'
declare fn
declare key
declare value
{
printf '0 SUITES_NAME %s\n' "$1"
while read -r fn; do
while read -r key value; do
case "$key" in
SUITE_ERRORS)
shelter_suites_errors+="$value"
;;
SUITE_FAILURES)
shelter_suites_failures+="$value"
;;
SUITE_TESTS)
shelter_suites_tests+="$value"
;;
SUITE_TIME)
shelter_suites_time=$(bc <<< "$shelter_suites_time + $value" | "$SHELTER_SED_CMD" 's/^\./0./')
;;
esac
printf '%d %s %s\n' "$shelter_suites_line" "$key" "$value"
shelter_suites_line+=1
done < <(unset shelter_suites_tests shelter_suites_errors shelter_suites_failures shelter_suites_line shelter_suites_time; shelter_run_test_suite "$fn")
done < <(compgen -A function "$2")
printf '0 SUITES_TESTS %s\n' "$shelter_suites_tests"
printf '0 SUITES_ERRORS %s\n' "$shelter_suites_errors"
printf '0 SUITES_FAILURES %s\n' "$shelter_suites_failures"
printf '0 SUITES_TIME %s\n' "$shelter_suites_time"
} | sort -n | "$SHELTER_SED_CMD" -u 's/^[0-9]\+ //'
}
# This function is used internally to handle
# transitions between blocks while parsing
# It may only be used in an environment which defines the
# following functions:
# - output_suites_open
# - output_suites_close
# - output_suite_open
# - output_suite_close
# - output_testcase_open
# - output_testcase_body
# - output_testcase_close
# - output_body_add_error
_shelter_formatter_block_transition () {
declare next_block="$1"
case "$block" in
"$SHELTER_BLOCK_ROOT")
case "$next_block" in
"$SHELTER_BLOCK_SUITES")
# shellcheck disable=SC2154
flags[suites]=1
;;
"$SHELTER_BLOCK_SUITE")
# shellcheck disable=SC2154
flags[suite]=1
;;
esac
;;
"$SHELTER_BLOCK_SUITES")
output_suites_open
case "$next_block" in
"$SHELTER_BLOCK_ROOT")
output_suites_close
flags[suites]=0
;;
"$SHELTER_BLOCK_SUITES")
output_suites_close
flags[suites]=1
;;
"$SHELTER_BLOCK_SUITE")
flags[suite]=1
;;
esac
;;
"$SHELTER_BLOCK_SUITE")
output_suite_open
case "$next_block" in
"$SHELTER_BLOCK_ROOT")
output_suite_close
flags[suite]=0
if [[ "${flags[suites]:-0}" -eq 1 ]]; then
output_suites_close
flags[suites]=0
fi
;;
"$SHELTER_BLOCK_SUITES")
output_suite_close
flags[suite]=0
if [[ "${flags[suites]:-0}" -eq 1 ]]; then
output_suites_close
fi
;;
"$SHELTER_BLOCK_SUITE")
output_suite_close
flags[suite]=1
;;
esac
;;
"$SHELTER_BLOCK_TESTCASE")
output_testcase_open
if [[ "${flags[status]:-}" = error ]]; then
output_body_add_error
fi
output_testcase_body
output_testcase_close
case "$next_block" in
"$SHELTER_BLOCK_ROOT")
if [[ "${flags[suite]:-0}" -eq 1 ]]; then
output_suite_close
flags[suite]=0
fi
if [[ "${flags[suites]:-0}" -eq 1 ]]; then
output_suites_close
flags[suites]=0
fi
;;
"$SHELTER_BLOCK_SUITES")
if [[ "${flags[suite]:-0}" -eq 1 ]]; then
output_suite_close
flags[suite]=0
fi
flags[suite]=0
if [[ "${flags[suites]:-0}" -eq 1 ]]; then
output_suites_close
fi
flags[suites]=1
;;
"$SHELTER_BLOCK_SUITE")
if [[ "${flags[suite]:-0}" -eq 1 ]]; then
output_suite_close
fi
flags[suite]=1
;;
esac
;;
esac
block="$next_block"
attributes=()
body=()
stdout=()
stderr=()
unset 'flags[status]'
}
# This function is used internally as a
# generic formatter
# It may only be used in an environment which defines the
# following functions:
# - output_header
# - output_body_add_skipped
# - output_body_add_failure
# - output_stdout_add_line
# - output_stdout_add_line
_shelter_formatter () {
# shellcheck disable=SC2034
declare block="$SHELTER_BLOCK_ROOT"
declare -A attributes=()
# shellcheck disable=SC2034
declare -a body=()
declare -a stdout=()
declare -a stderr=()
declare -A flags=()
declare -i error_counter=0
declare -i failure_counter=0
declare transition_to
declare lineno line
output_header
while read -r key value; do
unset transition_to
# Keys which are allowed to change the block
case "$key" in
SUITES_NAME)
transition_to="$SHELTER_BLOCK_SUITES"
;;
SUITE_NAME)
transition_to="$SHELTER_BLOCK_SUITE"
;;
CMD|SKIPPED)
transition_to="$SHELTER_BLOCK_TESTCASE"
;;
esac
if [[ -n "${transition_to:-}" ]]; then
if ! [[ "$error_counter" -eq 0 && "$failure_counter" -eq 0 ]] && [[ "$SHELTER_FORMATTER_ERREXIT_ON" = 'first-failing' ]]; then
break
fi
_shelter_formatter_block_transition "$transition_to"
fi
case "$key" in
SKIPPED)
flags[status]=skipped
output_body_add_skipped
;&
SUITES_*|SUITE_*|CMD|CLASS|TIME)
attributes["${ATTRIBUTE_MAP["$key"]}"]="$value"
;;
EXIT)
attributes[status]="$value"
if [[ "$value" -eq 0 ]]; then
[[ "${flags[status]:-}" = failure ]] || flags[status]=success
else
if ! [[ "${flags[status]:-}" = failure ]]; then
flags[status]=error
error_counter=$((error_counter+1))
fi
fi
;;
ASSERT)
flags[status]=failure
failure_counter=$((failure_counter+1))
output_body_add_failure "${value%% *}" "${value#* }"
;;
STDOUT)
read -r lineno line <<< "$value"
stdout["$lineno"]="$line"
;;
STDERR)
read -r lineno line <<< "$value"
stderr["$lineno"]="$line"
;;
esac
done
_shelter_formatter_block_transition "$SHELTER_BLOCK_ROOT"
output_footer
if ! [[ "$SHELTER_FORMATTER_ERREXIT_ON" = 'none' ]]; then
[[ "$failure_counter" -eq 0 ]] || return 1
[[ "$error_counter" -eq 0 ]] || return 2
fi
}
# shellcheck disable=SC2030,SC2031
## @fn shelter_junit_formatter ()
## @brief Format output of the test runner as JUnit XML
##
## Examples
##
## @code{.sh}
## {
## shelter_run_test_case foo
## shelter_run_test_case bar
## shelter_run_test_class SuccessfulTests test_good_
## shelter_run_test_class FailingTests test_bad_
## } | shelter_junit_formatter
## @endcode
##
## @code{.sh}
## shelter_run_test_suite suite_1 | shelter_unit_formatter
## @endcode
shelter_junit_formatter () {
(
declare -rA ATTRIBUTE_MAP=(
[SUITES_ERRORS]=errors
[SUITES_FAILURES]=failures
[SUITES_NAME]=name
[SUITES_TESTS]=tests
[SUITES_TIME]=time
[SUITE_ERRORS]=errors
[SUITE_FAILURES]=failures
[SUITE_NAME]=name
[SUITE_SKIPPED]=skipped
[SUITE_TESTS]=tests
[SUITE_TIME]=time
[CMD]=name
[CLASS]=classname
[SKIPPED]=name
[TIME]=time
)
output_header () {
printf '<?xml version="1.0" encoding="UTF-8"?>\n'
}
xml_escaped () {
"$SHELTER_SED_CMD" -e 's/\&/\&/g' \
-e 's/</\</g' \
-e 's/>/\>/g' \
-e 's/"/\"/g' \
-e "s/'/\\'/g"
}
xml_attributes () {
declare item
declare -i first_item=1
[[ "${#attributes[@]}" -gt 0 ]] || return 0
while read -r item; do
if [[ "$first_item" -eq 1 ]]; then
first_item=0
else
printf ' '
fi
printf '%s="%s"' "$item" "$(xml_escaped <<< "${attributes["$item"]}")"
done < <(for index in "${!attributes[@]}"; do printf '%s\n' "$index"; done | sort)
printf '\n'
}
output_suites_open () {
printf '<testsuites %s>\n' "$(xml_attributes)"
}
output_suite_open () {
printf '<testsuite %s>\n' "$(xml_attributes)"
}
output_testcase_open () {
printf '<testcase %s>\n' "$(xml_attributes)"
}
output_testcase_body () {
declare item
if [[ "${#body[@]}" -gt 0 ]]; then
for item in "${body[@]}"; do
printf '%s\n' "$item"
done
fi
output_testcase_stdout
output_testcase_stderr
}
output_testcase_stdout () {
declare -i i
[[ "${#stdout[@]}" -gt 0 ]] || return 0
printf '<system-out>\n'
for i in "${!stdout[@]}"; do
printf '%s %s\n' "$i" "$(xml_escaped <<< "${stdout["$i"]}")"
done
printf '</system-out>\n'
}
output_testcase_stderr () {
declare -i i
[[ "${#stderr[@]}" -gt 0 ]] || return 0
printf '<system-err>\n'