This repository has been archived by the owner on May 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testthat.sh
executable file
·1750 lines (1369 loc) · 45.4 KB
/
testthat.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
PROGNAME=$(basename $0)
VERSION=1.3.1
YES=yes
ON_THE_SPOT=on.the.spot
AT_THE_END=at.the.end
INCLUDE_FCTS=${TESTTHAT_INCLUDE_FCTS:-}
INCLUDE_FILES=${TESTTHAT_INCLUDE_FILES:-}
DEBUG=0
TOTEST=
NB_TEST_CONTEXT=0
ERR_NUMBER=0
PRINT=
FILE_PATTERN='[Tt][Ee][Ss][Tt][-._].*\.sh'
FCT_PREFIX='[Tt][Ee][Ss][Tt]_?'
AUTORUN=$YES
REPORT=$AT_THE_END
QUIT_ON_FIRST_ERROR=
declare -a g_err_msgs=()
declare -a g_err_stderr_files=()
declare -a g_fcts_run_in_test_file=()
function print_help {
cat <<END_HELP
A bash script for running tests on command line scripts.
Usage: $PROGNAME [options] <folders or files>
The folders are searched for files matching 'test-*.sh' pattern.
You can use the environment variables TEST_THAT_FCT and TEST_THAT_NO_FCT to restrict the test functions that are run. Just set this variable to the list of functions you want to run or not run (separated by commas).
OPTIONS:
--dryrun List what tests would be run but do not execute anything.
-f, --file-pattern Redefine the regular expression for filtering test files
in folders. Default is "$FILE_PATTERN".
-g, --debug Debug mode.
-h, --help Print this help message.
--no-autorun Do not detect and run automatically the test functions.
This means you will have to call explicitly the
test_that function.
-i, --include-fcts <fct1,fct2,...>
Set a selection of test functions to run. Only those test
functions will be run, if they exist. The value is a
comma separated list of functions names. Can be set also
through TESTTHAT_INCLUDE_FCTS environment variable.
-j, --include-files <file1, file2, ...>
Set a selection of test files to run. Only those test
files will be run, if they exist. The value is a
comma separated list of files names. Can be set also
through TESTTHAT_INCLUDE_FILES environment variable.
-p, --print Print live output of test functions.
-q, --quit-first Quit on first error, and stop all tests.
Useful with $ON_THE_SPOT report (see -r option).
-r, --report <NAME> Set the name of the reporter to use. Possible
values are: $ON_THE_SPOT (report each error as it
occurs), $AT_THE_END (report at the end of all
tests).
Default is $AT_THE_END.
-v, --version Print version.
-x, --fct-prefix Set the prefix to use when auto-detecting test
functions. Default is "$FCT_PREFIX".
WRITING A TEST SCRIPT:
When inside a test script, you have first to define context:
test_context "My context"
The text of the context will be printed on the screen.
Then you call test_that for each test function you have written:
test_that "myFct is working correctly" test_myFct
Inside your test_myFct function, you call assertions:
function test_myFct {
expect_num_eq 1 2 || return 1
}
Do not forget to append " || return 1" to the assertion call, otherwise no
error will be reported in case of failure.
ASSERTIONS:
Assertions start all with the prefix "expect_" and need to be followed by
" || return 1" in order to report a failure.
Some assertions take a custom message to be displayed in case of failure.
Success/failure assertions:
expect_success Test the success of a command.
Arguments: command.
Example:
expect_success my_command || return 1
expect_success my_command arg1 arg2 || return 1
expect_success_in_n_tries
Test that a command succeeds before n tries.
Arg. 1: Number of tries.
Remaining arguments: command.
Example:
expect_success_in_n_tries 3 my_command || return 1
expect_success_in_n_tries 3 my_command arg1 || return 1
expect_failure Test the failure of a command.
Arguments: command.
Example:
expect_failure my_command || return 1
expect_failure my_command arg1 arg2 || return 1
expect_status Test that a command fails and return a precise status value.
Arg. 1: Expected status number.
Remaining arguments: command.
Example:
expect_status 0 my_command || return 1
expect_status 4 my_command || return 1
expect_status 4 my_command arg1 arg2 || return 1
expect_exit Test the failure of a command by running the command inside
a subshell. Thus you can test a call to a function that
call the \`exit\` command.
Arguments: command.
Example:
expect_exit my_command || return 1
expect_exit my_command arg1 arg2 || return 1
expect_exit_status
Test that a command fails and return a precise status value
by running the command inside a subshell. Thus you can test
a call to a function that call the \`exit\` command.
Arg. 1: Expected status number.
Remaining arguments: command.
Example:
expect_exit_status 2 my_command || return 1
expect_exit_status 0 my_command arg1 arg2 || return 1
Output assertions:
expect_empty_output
Test if a command output nothing on stdout.
Arguments: command.
Example:
expect_empty_output my_command arg1 arg2 || return 1
expect_non_empty_output
Test if a command output something on stdout.
Arguments: command.
Example:
expect_non_empty_output my_command arg1 arg2 || return 1
expect_output_eq Test if the output of a command is equals to a value. The
output is stripped from carriage returns before comparison.
Arg. 1: Expected output as a string.
Remaining arguments: command.
Example:
expect_output_eq "Expected Output" my_command arg1 arg2 || return 1
expect_output_ne Test if the output of a command is equals to a value. The
output is stripped from carriage returns before comparison.
Arg. 1: Expected output as a string.
Remaining arguments: command.
Example:
expect_output_ne "Expected Output" my_command arg1 arg2 || return 1
expect_output_esc_eq
Test if the output of a command is equals to a value.
Carriage returns are preserved.
Arg. 1: Expected output as a string for echo command with
trailing newline disabled and backslash escapes
enabled.
Remaining arguments: command.
Example:
expect_output_esc_eq "Expected Output" my_command arg1 arg2 || return 1
expect_output_esc_ne
Test if the output of a command is different from a value.
Carriage returns are preserved.
Arg. 1: Expected output as a string for echo command with
trailing newline disabled and backslash escapes
enabled.
Remaining arguments: command.
Example:
expect_output_esc_ne "Expected Output" my_command arg1 arg2 || return 1
expect_output_nlines_eq
Test if a command output exactly n lines of text on stdout.
Arg. 1: Expected number of lines.
Remaining arguments: command.
Example:
expect_output_nlines_eq 3 my_command arg1 arg2 || return 1
expect_output_nlines_ge
Test if a command output n lines or more of text on stdout.
Arg. 1: Expected minimum number of lines.
Remaining arguments: command.
Example:
expect_output_nlines_ge 3 my_command arg1 arg2 || return 1
expect_output_re Test if the output of a command matches a regular
expression. The output is stripped from carriage returns
before comparison.
Arg. 1: Regular expression.
Remaining arguments: command.
Example:
expect_output_re "A.*B" my_command arg1 arg2 || return 1
String assertions:
expect_str_null Test if a string is empty.
Arg. 1: String.
Arg. 2: Message (optional).
Example:
expect_str_null $$s || return 1
expect_str_null $$s "My Msg." || return 1
expect_str_not_null
Test if a string is not empty.
Arg. 1: String.
Arg. 2: Message (optional).
Example:
expect_str_not_null $$s || return 1
expect_str_not_null $$s "My Msg." || return 1
expect_str_eq Test if two strings are equal.
Arg. 1: First string.
Arg. 2: Second string.
Arg. 3: Message (optional).
Example:
expect_str_eq $$s "abc" || return 1
expect_str_eq $$s "abc" "My Msg." || return 1
expect_str_ne Test if two strings are different.
Arg. 1: First string.
Arg. 2: Second string.
Arg. 3: Message (optional).
Example:
expect_str_ne $$s "abc" || return 1
expect_str_ne $$s "abc" "My Msg." || return 1
expect_str_re Test if a string matches an ERE.
Arg. 1: String.
Arg. 2: Pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_str_re $$s "^[a-zA-Z]+-[0-9]+$" || return 1
expect_str_re $$s "^[a-zA-Z]+-[0-9]+$" "My Msg" || return 1
Numeric assertions:
expect_num_eq Test the equality of two integers.
Arg. 1: First integer.
Arg. 2: Second integer.
Arg. 3: Message (optional).
Example:
expect_num_eq $$n 2 || return 1
expect_num_eq $$n 2 "My Msg." || return 1
expect_num_ne Test that two integers are different.
Arg. 1: First integer.
Arg. 2: Second integer.
Arg. 3: Message (optional).
Example:
expect_num_ne $$n 2 || return 1
expect_num_ne $$n 2 "My Msg." || return 1
expect_num_le Test that an integer is lower or equal than another.
Arg. 1: First integer.
Arg. 2: Second integer.
Arg. 3: Message (optional).
Example:
expect_num_le $$n 5 || return 1
expect_num_le $$n 5 "My Msg" || return 1
expect_num_gt Test that an integer is strictly greater than another.
Arg. 1: First integer.
Arg. 2: Second integer.
Arg. 3: Message (optional).
Example:
expect_num_gt $$n 5 || return 1
expect_num_gt $$n 5 "My Msg" || return 1
Environment assertions:
expect_def_env_var
Test if an environment variable is defined and not empty.
Arg. 1: Name of the environement variable.
Arg. 2: Message (optional).
Example:
expect_def_env_var MY_VAR || return 1
expect_def_env_var MY_VAR "My Msg" || return 1
File system assertions:
expect_file Test if file exists.
Arg. 1: File.
Arg. 2: Message (optional).
Example:
expect_folder "myFile" || return 1
expect_folder "myFile" "My Msg" || return 1
expect_folder Test if folder exists.
Arg. 1: Folder.
Arg. 2: Message (optional).
Example:
expect_folder "myFolder" || return 1
expect_folder "myFolder" "My Msg" || return 1
expect_symlink Test if a symbolic link exists and points to a certain
location.
Arg. 1: Symbolic link path.
Arg. 2: The path to which the link points.
Arg. 3: Message (optional).
Example:
expect_symlink "/my/sym/link" "/the/file/to/which/it/points"
expect_no_path Test if nothing exists (no file, no folder) at the
specified path.
Arg. 1: Path.
Arg. 2: Message (optional).
Example:
expect_no_path "myFolder" || return 1
expect_no_path "myFolder" "My Msg" || return 1
expect_same_folders
Test if two folders have the same content, using "diff"
command.
Arg. 1: First folder.
Arg. 2: Second folder.
Example:
expect_same_folders "folderA" "folderB" || return 1
expect_files_in_folder
Test if files matching a pattern exist inside a folder.
Arg. 1: Folder.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_files_in_folder "myFolder" "^.*\.txt$" || return 1
expect_files_in_folder "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_other_files_in_folder
Test if a folder contains files not matching a pattern.
Arg. 1: Folder.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_other_files_in_folder "myFolder" "^.*\.txt$" || return 1
expect_other_files_in_folder "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_no_other_files_in_folder
Test if a folder contains files matching a pattern, and no
other files.
Arg. 1: Folder.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_no_other_files_in_folder "myFolder" "^.*\.txt$" || return 1
expect_no_other_files_in_folder "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_files_in_tree
Test if files matching a pattern exist inside a tree structure.
Arg. 1: Folder in which to search recursively.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_files_in_tree "myFolder" "^.*\.txt$" || return 1
expect_files_in_tree "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_other_files_in_tree
Test if files not matching a pattern exist inside a tree
structure, and no other files.
Arg. 1: Folder in which to search recursively.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_other_files_in_tree "myFolder" "^.*\.txt$" || return 1
expect_other_files_in_tree "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_no_other_files_in_tree
Test if files matching a pattern exist inside a tree
structure, and no other files.
Arg. 1: Folder in which to search recursively.
Arg. 2: Files pattern as an ERE.
Arg. 3: Message (optional).
Example:
expect_no_other_files_in_tree "myFolder" "^.*\.txt$" || return 1
expect_no_other_files_in_tree "myFolder" "^.*\.txt$" "My Msg" || return 1
expect_folder_is_writable
Test files can be created or modified inside a folder.
Arg. 1: Path to the folder.
Arg. 3: Message (optional).
Example:
expect_folder_is_writable "myFolder" "My Msg" || return 1
File assertions:
expect_same_files
Test if two files are identical.
Arg. 1: File 1.
Arg. 2: File 2.
Example:
expect_same_files "myFile1" "myFile2" || return 1
expect_empty_file
Test if a file exists and is empty.
Arg. 1: File.
Arg. 2: Message (optional).
Example:
expect_empty_file "myFile" || return 1
expect_non_empty_file
Test if a file exists and is not empty.
Arg. 1: File.
Arg. 2: Message (optional).
Example:
expect_non_empty_file "myFile" || return 1
expect_no_duplicated_row
Test if a file contains no duplicated rows.
Arg. 1: File.
Example:
expect_no_duplicated_row "myFile" || return 1
expect_same_number_of_rows
Test if two files contain the same number of lines.
Arg. 1: File 1.
Arg. 2: File 2.
Example:
expect_same_number_of_rows "myFile1" "myFile2" || return 1
CSV assertions:
expect_csv_has_columns
Test if a CSV file contains a set of columns. Second
argument is the separator character used in the CSV.
Arg. 1: File.
Arg. 2: CSV separator character.
Arg. 3: Expected column names separated by spaces.
Example:
expect_csv_has_columns "myfile.csv" "," "col1 col2 col3" || return 1
expect_csv_not_has_columns
Test if a CSV file does not contain a set of columns.
Arg. 1: File.
Arg. 2: CSV separator character.
Arg. 3: Column names separated by spaces.
Example:
expect_csv_not_has_columns "myfile.csv" "," "col1 col2 col3" || return 1
expect_csv_identical_col_values
Test if two CSV files contain the same column with the same
values.
Arg. 1: Column name.
Arg. 2: File 1.
Arg. 3: File 2.
Arg. 4: CSV separator character.
Example:
expect_csv_identical_col_values "myCol" "myFile1" "myFile2" ";" || return 1
expect_csv_float_col_equals
Test if all the values of a CSV file column are close to a float value.
Arg. 1: File.
Arg. 2: CSV separator.
Arg. 3: Column name.
Arg. 4: Float value.
Arg. 5: Tolerance.
Example:
expect_csv_float_col_equals "myFile" "," "myCol" 10.01 0.01 || return 1
expect_csv_same_col_names
Test if two CSV files contain the same column names.
Arg. 1: File 1.
Arg. 2: File 2.
Arg. 3: CSV separator.
Arg. 4: The number of columns on which to make the
comparison. If unset all columns will be used
(optional).
Arg. 5: If set to 1, then double quotes will be removed
from column names before comparison (optional).
Example:
expect_csv_same_col_names "myFile1" "myFile2" ";" || return 1
expect_csv_same_col_names "myFile1" "myFile2" ";" 8 || return 1
expect_csv_same_col_names "myFile1" "myFile2" ";" 8 1 || return 1
DEPRECATED ASSERTIONS:
expect_failure_status
Replace by "expect_status".
expect_file_exists Replaced by "expect_file".
expect_success_after_n_tries
Replaced by "expect_success_in_n_tries".
csv_expect_has_columns
Replaced by "expect_csv_has_columns".
csv_expect_not_has_columns
Replaced by "expect_csv_not_has_columns".
csv_expect_identical_col_values
Replaced by "expect_csv_identical_col_values".
csv_expect_float_col_equals
Replaced by "expect_csv_float_col_equals".
csv_expect_same_col_names
Replaced by "expect_csv_same_col_names".
GLOSSARY
ERE Extended Regular Expression.
END_HELP
}
function error {
local msg=$1
echo "ERROR: $msg" >&2
exit 1
}
function debug {
local dbglvl=$1
local dbgmsg=$2
[ $DEBUG -ge $dbglvl ] && echo "[DEBUG] $dbgmsg" >&2
}
function deprecated {
local new_fct="$1"
debug 1 "Deprecated function. Use $new_fct() instead."
}
function read_args {
local args="$*" # save arguments for debugging purpose
# Read options
while true ; do
case $1 in
--dryrun) DRYRUN=1 ;;
-f|--file-pattern) FILE_PATTERN="$2" ; shift ;;
-g|--debug) DEBUG=$((DEBUG + 1)) ;;
-h|--help) print_help ; exit 0 ;;
--no-autorun) AUTORUN= ;;
-i|--include-fcts) INCLUDE_FCTS="$2" ; shift ;;
-j|--include-files) INCLUDE_FILES="$2" ; shift ;;
-p|--print) PRINT=$YES ;;
-q|--quit-first) QUIT_ON_FIRST_ERROR=$YES ;;
-r|--report) REPORT="$2" ; shift ;;
-v|--version) echo $VERSION ; exit 0 ;;
-x|--fct-prefix) FCT_PREFIX="$2" ; shift ;;
-) error "Illegal option $1." ;;
--) error "Illegal option $1." ;;
--*) error "Illegal option $1." ;;
-?) error "Unknown option $1." ;;
-[^-]*) split_opt=$(echo $1 | sed 's/^-//' | sed 's/\([a-zA-Z]\)/ -\1/g') ; set -- $1$split_opt "${@:2}" ;;
*) break
esac
shift
done
# Read remaining arguments as a list of folders and/or files
if [ -n "$*" ] ; then
TOTEST=("$@")
else
TOTEST=()
fi
# Check reporter
[[ $REPORT == $AT_THE_END || $REPORT == $ON_THE_SPOT ]] || error "Unknown reporter $REPORT."
# Debug
debug 1 "Arguments are : $args"
debug 1 "Folders and files to test are : $TOTEST"
debug 1 "AUTORUN=$AUTORUN"
debug 1 "DEBUG=$DEBUG"
debug 1 "FCT_PREFIX=$FCT_PREFIX"
debug 1 "FILE_PATTERN=$FILE_PATTERN"
debug 1 "INCLUDE_FCTS=$INCLUDE_FCTS"
debug 1 "INCLUDE_FILES=$INCLUDE_FILES"
debug 1 "REPORT=$REPORT"
}
function join_by {
local IFS="$1"
shift
echo "$*"
}
function test_context {
local msg=$1
if [[ -z $DRYRUN ]] ; then
[[ $NB_TEST_CONTEXT -gt 0 ]] && echo
echo -n "$msg "
((++NB_TEST_CONTEXT))
fi
}
print_error() {
n=$1
msg="$2"
output_file="$3"
echo
echo '----------------------------------------------------------------'
printf "%x. " $n
echo "Failure while asserting that \"$msg\"."
echo '---'
if [[ -f $output_file ]] ; then
cat "$output_file"
rm "$output_file"
fi
echo '----------------------------------------------------------------'
}
finalize_tests() {
# Print new line
[[ $NB_TEST_CONTEXT -eq 0 ]] || echo
# Print end report
[[ $REPORT == $AT_THE_END ]] && print_end_report
# Exit
exit $ERR_NUMBER
}
function test_that {
local msg="$1"
local test_fct="$2"
shift 2
local params="$*"
local tmp_stderr_file=$(mktemp -t testthat-stderr.XXXXXX)
# Filtering
if [[ -n $TEST_THAT_FCT && ",$TEST_THAT_FCT," != *",$test_fct,"* ]] ; then
return 0
fi
if [[ -n $TEST_THAT_NO_FCT && ",$TEST_THAT_NO_FCT," == *",$test_fct,"* ]] ; then
return 0
fi
# Run test
g_fcts_run_in_test_file+=("$test_fct")
( $test_fct $params 2>"$tmp_stderr_file" ) # Run in a subshell to catch exit
# interruptions.
exit_code=$?
# Set message
[[ -n $msg ]] || msg="Tests pass in function $test_fct"
# Print stderr now
[[ $PRINT == $YES && -f $tmp_stderr_file ]] && cat $tmp_stderr_file
# Failure
if [ $exit_code -gt 0 ] ; then
# Increment error number
((++ERR_NUMBER))
# Print error number
if [[ ERR_NUMBER -lt 16 ]] ; then
printf %x $ERR_NUMBER
else
echo -n E
fi
# Print error now
if [[ $REPORT == $ON_THE_SPOT ]] ; then
print_error $ERR_NUMBER "$msg" "$tmp_stderr_file"
# Store error message for later
else
g_err_msgs+=("$msg")
g_err_stderr_files+=("$tmp_stderr_file")
fi
# Quit on first error
[[ $QUIT_ON_FIRST_ERROR == $YES ]] && finalize_tests
# Success
else
rm $tmp_stderr_file
fi
}
function run_test_file {
local file="$1"
[[ -z $DRYRUN ]] || echo "Test functions found in file \"$file\":"
g_fcts_run_in_test_file=()
source "$file"
# Run all test_.* functions not run explicitly by test_that
if [[ $AUTORUN == $YES ]] ; then
for fct in $(grep -E '^ *function +'$FCT_PREFIX'[^ ]+|'$FCT_PREFIX'[^ ]+\(\) *\{' "$file" | sed -E 's/^ *(function +)?('$FCT_PREFIX'[^ {(]+).*$/\2/') ; do
# Ignore some reserved names
[[ $fct == test_context || $fct == test_that ]] && continue
# Filtering
[[ -z $INCLUDE_FCTS || ",$INCLUDE_FCTS," == *",$fct,"* ]] || continue
# Run function
[[ " ${g_fcts_run_in_test_file[*]} " == *" $fct "* ]] && continue
if [[ -n $DRYRUN ]] ; then
echo " $fct"
else
test_that "" $fct
fi
done
fi
}
function print_end_report {
if [[ $ERR_NUMBER -gt 0 ]] ; then
echo '================================================================'
echo "$ERR_NUMBER error(s) encountered."
# Loop on all errors
for ((i = 0 ; i < ERR_NUMBER ; ++i)) ; do
print_error $((i+1)) "${g_err_msgs[$i]}" "${g_err_stderr_files[$i]}"
done
fi
}
output_progress() {
# Output the progress of a command, by taking both stdout and stderr of the
# command and replace each line by a dot character.
# This function is useful while some part of the test code takes much time
# and use does not get any feedback.
# It is also particularly essential with Travis-CI, which aborts the test
"$@" 2>&1 | while read line ; do echo -n . ; done
}
function print_call_stack {
local frame=0
while caller $frame >&2 ; do
((frame++));
done
}
function expect_success_in_n_tries {
local n=$1
shift
local cmd="$*"
# Try to run the command
for ((i = 0 ; i < n ; ++i)) ; do
( "$@" >&2 )
err=$?
[[ $err == 0 ]] && break
done
# Failure
if [[ $err -gt 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed $n times." >&2
return 1
fi
echo -n .
}
function expect_success {
local cmd="$*"
( "$@" >&2 )
local status=$?
if [[ $status -gt 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $status." >&2
return 1
fi
echo -n .
}
function expect_failure {
local cmd="$*"
( "$@" >&2 )
if [[ $? -eq 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" was successful while expecting failure." >&2
return 1
fi
echo -n .
}
function expect_status {
local expected_status="$1"
shift
local cmd="$*"
( "$@" >&2 )
local actual_status=$?
if [[ $actual_status -ne $expected_status ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $actual_status, but " \
"expected status $expected_status." >&2
return 2
fi
echo -n .
}
function expect_empty_output {
local cmd="$*"
local output=
local tmpfile=$(mktemp -t $PROGNAME.XXXXXX)
( "$@" >"$tmpfile" )
local status=$?
output=$(cat "$tmpfile")
unlink "$tmpfile"
if [[ $status -ne 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $status." >&2
return 1
elif [[ -n $output ]] ; then
print_call_stack >&2
echo "Output of \"$cmd\" is not empty. Output: \"$output\"." >&2
return 2
fi
echo -n .
}
function expect_non_empty_output {
local cmd="$*"
local empty=
local tmpfile=$(mktemp -t $PROGNAME.XXXXXX)
( "$@" >"$tmpfile" )
local status=$?
[[ -s "$tmpfile" ]] || empty=$YES
unlink "$tmpfile"
if [[ $status -ne 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $status." >&2
return 1
elif [[ $empty == $YES ]] ; then
print_call_stack >&2
echo "Output of \"$cmd\" is empty." >&2
return 2
fi
echo -n .
}
function _expect_output_op {
local op="$1"
local expected_output="$2"
shift 2
local cmd="$*"
local tmpfile=$(mktemp -t $PROGNAME.XXXXXX)
( "$@" >"$tmpfile" )
local status=$?
local output=$(cat "$tmpfile")
rm "$tmpfile"
if [[ $status -ne 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $status." >&2
return 1
elif [[ $op == eq && "$expected_output" != "$output" ]] ; then
print_call_stack >&2
echo "Output of \"$cmd\" is wrong. Expected \"$expected_output\". Got \"$output\"." >&2
return 2
elif [[ $op == ne && "$expected_output" == "$output" ]] ; then
print_call_stack >&2
echo "Output of \"$cmd\" is wrong. Expected something different from \"$expected_output\"." >&2
return 3
elif [[ $op == re ]] && ! egrep "$expected_output" >/dev/null <<<"$output" ; then
print_call_stack >&2
echo "Output of \"$cmd\" is wrong. Expected \"$expected_output\". Got \"$output\"." >&2
return 4
fi
echo -n .
}
function expect_output_eq {
_expect_output_op 'eq' "$@"
return $?
}
function expect_output_re {
_expect_output_op 're' "$@"
return $?
}
function expect_output_ne {
_expect_output_op 'ne' "$@"
return $?
}
function _expect_output_esc_op {
local op="$1"
local expected_output="$2"
shift 2
local cmd="$*"
local tmpfile=$(mktemp -t $PROGNAME.XXXXXX)
local tmpfile2=$(mktemp -t $PROGNAME.XXXXXX)
( "$@" >"$tmpfile" )
local status=$?
echo -ne "$expected_output" >"$tmpfile2"
if [[ $status -ne 0 ]] ; then
print_call_stack >&2
echo "Command \"$cmd\" failed with status $status." >&2
rm "$tmpfile" "$tmpfile2"
return 1
elif [[ $op == eq ]] && ! diff -q "$tmpfile" "$tmpfile2" ; then
print_call_stack >&2
echo -n "Output of \"$cmd\" is wrong. Expected \"$expected_output\". Got \"" >&2
cat $tmpfile >&2
echo "\"." >&2
rm "$tmpfile" "$tmpfile2"
return 2
elif [[ $op == ne ]] && diff -q "$tmpfile" "$tmpfile2" ; then
print_call_stack >&2
echo -n "Output of \"$cmd\" is wrong. Expected something different from \"$expected_output\"." >&2
rm "$tmpfile" "$tmpfile2"
return 3
fi
rm "$tmpfile" "$tmpfile2"
echo -n .
}
function expect_output_esc_ne {
_expect_output_esc_op 'ne' "$@"
return $?
}
function expect_output_esc_eq {
_expect_output_esc_op 'eq' "$@"
return $?
}
function expect_output_nlines_eq {
local n="$1"
shift
local cmd="$*"
local tmpfile=$(mktemp -t $PROGNAME.XXXXXX)
( "$@" >"$tmpfile" )
local status=$?
local nlines=$(awk 'END { print NR }' "$tmpfile")
unlink "$tmpfile"
if [[ $status -ne 0 ]] ; then