-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.sh
executable file
·1921 lines (1569 loc) · 44.7 KB
/
tests.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
#!/bin/bash
_base_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source $_base_dir/vendor/github.com/reconquest/coproc.bash/coproc.bash 2>/dev/null \
|| import:use "github.com/reconquest/coproc.bash"
# Public API Functions {{{
# @description Make all functions from tests.sh available without 'tests:'
# prefix. Prefix can be also user defined, like 't:'.
#
# @arg $1 string Custom prefix for namespace functions.
tests:import-namespace() {
local prefix="${1:-}"
if [ $_tests_verbose -gt 2 ]; then
tests:debug "! importing namespace 'tests:'" \
$(sed -re"s/.+/ into '&'/" <<< $prefix)
fi
builtin eval $(
declare -F |
grep -F -- '-f tests:' |
cut -d: -f2 |
sed -re's/.*/'$prefix'&() { tests:& "${@}"; };/'
)
}
# @description Returns temporary directory for current test session.
#
# It can be used as a workspace for the testcase.
#
# @example
# ls $(tests:get-tmp-dir)
#
# @stdout Path to temp dir, e.g., /tmp/tests.XXXX
tests:get-tmp-dir() {
if [ -z "$_tests_dir" ]; then
tests:debug "test session not initialized"
_tests_interrupt
fi
echo "$_tests_dir/root"
}
# @description Suspends testcase execution until file contents matches
# pattern or timeout is reached.
#
# Can be used to check if background-executed command output do not contains
# any error messages.
#
# @example
# stderr=$(tests:get-background-stderr $command_id)
# tests:wait-file-not-matches "$stderr" "ERROR" 1 2
#
# @arg $1 string Path to file.
# @arg $2 regexp Regexp, same as in `grep -E`.
# @arg $3 int Interval of time to check changes after.
# @arg $4 int Timeout in seconds.
tests:wait-file-matches() {
local file="$1"
local pattern="$2"
local sleep_interval="$3"
local sleep_max="$4"
shift 4
if [ ! "$_tests_run_exitcode" ]; then
_tests_prepare_eval_namespace wait-for-matches
fi
local sleep_iter=0
local sleep_iter_max=$(bc <<< "$sleep_max/$sleep_interval")
tests:debug "! waiting file matches pattern after command:" \
"pattern '$pattern', file '$file' (${sleep_max}sec max)"
while true; do
sleep_iter=$(($sleep_iter+1))
if grep -qE "$pattern" $file 2>/dev/null; then
tests:debug "! matches found for pattern '$(\
echo $pattern)' in file '$file' (after $(\
bc <<< "$sleep_iter * $sleep_interval" )sec)"
echo 0 > $_tests_run_exitcode
break
fi
if [[ $sleep_iter -le $sleep_iter_max ]]; then
sleep $sleep_interval
continue
fi
tests:debug "! no matches found for pattern '$(\
echo $pattern)' in file '$file' (after $(\
bc <<< "$sleep_iter * $sleep_interval" )sec)"
echo 1 > $_tests_run_exitcode
break
done
}
tests:wait-file-not-matches() {
tests:wait-file-matches "${@}"
if [ $(cat $_tests_run_exitcode) -eq 0 ]; then
echo 1 > $_tests_run_exitcode
else
echo 0 > $_tests_run_exitcode
fi
}
# @description Asserts, that first string arg is equals to second.
#
# @example
# tests:assert-equals 1 2 # fails
#
# @arg $1 string Expected string.
# @arg $2 string Actual value.
tests:assert-equals() {
local expected="$1"
local actual="$2"
_tests_make_assertion "$expected" "$actual" \
"two strings equals" \
">>> $expected$" \
"<<< $actual$\n"
_tests_inc_asserts_count
}
# @description Asserts, that last evaluated command's stdout contains given
# string.
#
# @example
# tests:eval echo 123
# tests:assert-stdout 123
#
# @arg $1 string Expected stdout.
tests:assert-stdout() {
local expected="$1"
shift
tests:assert-stdout-re "$(_tests_quote_re <<< "$expected")"
}
# @description Asserts, that last evaluated command's stderr contains given
# string.
#
# @example
# tests:eval echo 123 '1>&2' # note quoting
# tests:assert-stderr 123
#
# @arg $1 string Expected stderr.
tests:assert-stderr() {
local expected="$1"
shift
tests:assert-stderr-re "$(_tests_quote_re <<< "$expected")"
}
# @description Compares, that last evaluated command output (stdout, stderr) or
# file contents matches regexp.
#
# @example
# tests:eval echo aaa
# tests:match-re stdout a.a
# echo $? # 0
# tests:match-re stdout a.b
# echo $? # 1
#
# @arg $1 'stdout'|'stderr'|filename If 'stdout' or 'stderr' is used, use
# last command's stream as actual value. If filename is specified, then use
# contents of specified filename as actual contents.
#
# @arg $2 regexp Regexp to match, same as in grep.
tests:match-re() {
local target="$1"
local regexp="$2"
shift 2
if [ ! "$_tests_run_exitcode" ]; then
_tests_prepare_eval_namespace match
fi
if [ -f $target ]; then
file=$target
elif [ "$target" = "stdout" ]; then
file=$_tests_run_stdout
else
file=$_tests_run_stderr
fi
if [ -z "$regexp" ]; then
if [ -s $file ]; then
echo 1
else
echo 0
fi
elif grep -qP "$regexp" $file; then
echo 0
else
echo $?
fi > $_tests_run_exitcode
}
# @description Same as 'tests:match-re', but abort testing if comparison
# failed.
#
# @example
# tests:eval echo aaa
# tests:assert-re stdout a.a
# tests:assert-re stdout a.b # test fails there
#
# @see tests:match-re
tests:assert-re() {
local target="$1"
local regexp="$2"
shift 2
tests:match-re "$target" "$regexp"
if [ ! -f "$_tests_run_exitcode" ]; then
tests:debug "(assert-re) BUG: _tests_run_exitcode is empty"
_tests_interrupt
fi
local result=$(cat $_tests_run_exitcode)
_tests_make_assertion $result 0 \
"regexp matches" \
">>> ${regexp:-<empty regexp>}" \
"<<< contents of ${target}:\n\
$(_tests_pipe _tests_indent "$target" "<empty>" < $file)\n"
_tests_inc_asserts_count
}
# @description Asserts, that there are no diff on the last command output
# (stderr or stdout), or on string or on specified file with specified string
# or file.
#
# @example
# tests:eval echo -e '1\n2'
# tests:assert-no-diff stdout "$(echo -e '1\n2')" # note quotes
# tests:assert-no-diff stdout "$(echo -e '1\n3')" # test will fail
#
# @arg $1 string|filename Expected value.
# @arg $2 'stdout'|'stderr'|string|filename Actual value.
# @arg $@ any Additional arguments for diff.
tests:assert-no-diff() {
if [ -s /dev/stdin ]; then
local expected_target=/dev/stdin
else
local expected_target="$1"
shift
fi
local actual_target="$1"
shift
local options=(-u $@)
if [ -e "$expected_target" ]; then
expected_content="$(cat $expected_target)"
else
expected_content="$expected_target"
fi
if [ -e "$actual_target" ]; then
actual_content="$(cat $actual_target)"
elif [ "$actual_target" = "stdout" ]; then
actual_content="$(cat $_tests_run_stdout)"
elif [ "$actual_target" = "stderr" ]; then
actual_content="$(cat $_tests_run_stderr)"
else
actual_content="$actual_target"
fi
local diff
local result=0
if diff=$(diff ${options[@]} \
<(echo -e "$expected_content") \
<(echo -e "$actual_content")); then
result=0
else
result=$?
fi
_tests_make_assertion $result 0 \
"no diff" \
"\n$(_tests_pipe _tests_indent 'diff' <<< "$diff")\n"
_tests_inc_asserts_count
}
# @description Returns file containing stdout of last command.
#
# @example
# tests:eval echo 123
# cat $(tests:get-stdout-file) # will echo 123
#
# @stdout Filename containing stdout.
tests:get-stdout-file() {
echo $_tests_run_stdout
}
# @description Returns file containing stderr of last command.
#
# @example
# tests:eval echo 123 '1>&2' # note quotes
# cat $(tests:get-stderr) # will echo 123
#
# @stdout Filename containing stderr.
tests:get-stderr-file() {
echo $_tests_run_stderr
}
# @description Returns contents of the stdout of last command.
#
# @example
# tests:eval echo 123
# tests:get-stdout # will echo 123
#
# @stdout Stdout for last command.
tests:get-stdout() {
cat "$(tests:get-stdout-file)"
}
# @description Returns contents of the stderr of last command.
#
# @example
# tests:eval echo 123 '>&2'
# tests:get-stderr # will echo 123
#
# @stdout Stderr for last command.
tests:get-stderr() {
cat "$(tests:get-stderr-file)"
}
# @description Returns file containing exitcode of last command.
#
# @example
# tests:eval exit 220
# cat $(tests:get-exitcode-file) # will echo 220
#
# @stdout Filename containing exitcode.
tests:get-exitcode-file() {
echo $_tests_run_exitcode
}
# @description Returns exitcode of last command.
#
# @example
# tests:eval exit 220
# tests:get-exitcode # will echo 220
#
# @stdout Filename containing exitcode.
tests:get-exitcode() {
cat $_tests_run_exitcode
}
# @description Same as 'tests:assert-diff', but ignore changes whose lines are
# all blank.
#
# @example
# tests:eval echo -e '1\n2'
# tests:assert-no-diff-blank stdout "$(echo -e '1\n2')" # note quotes
# tests:assert-no-diff-blank stdout "$(echo -e '1\n\n2')" # test will pass
#
# @see tests:diff
tests:assert-no-diff-blank() {
tests:assert-no-diff "$@" "-B"
}
# @description Same as shell 'test' function, but asserts, that exit code is
# zero.
#
# @example
# tests:assert-test 1 -eq 1
# tests:assert-test 1 -eq 2 # test will fail
#
# @arg $@ Arguments for 'test' function.
tests:assert-test() {
local args="$@"
tests:debug "test $args"
local result
if test "$@"; then
result=0
else
result=$?
fi
if [ $result -ne 0 ]; then
touch "$_tests_dir/.failed"
tests:debug "test $args: failed"
_tests_interrupt
fi
_tests_inc_asserts_count
}
# @description Put specified contents into temporary file with given name.
#
# @example
# tests:put-string xxx "lala"
#
# tests:assert-equals xxx "lala" # test will pass
#
# @arg $1 filename Temporary file name.
# @arg $2 string Contents to put.
tests:put-string() {
local file="$1"
local content="$2"
tests:put "$file" <<< "$content"
}
# @description Put stdin into temporary file with given name.
#
# @example
# tests:put xxx <<EOF
# 1
# 2
# 3
# EOF
#
# tests:assert-no-diff xxx "$(echo -e '1\n2\n3')" # test will pass
#
# @arg $1 filename Temporary file name.
tests:put() {
local file="$_tests_dir_root/$1"
local stderr
if ! stderr=$(cat 2>&1 > $file); then
tests:debug "error writing file:"
_tests_indent 'error' <<< "$stderr"
_tests_interrupt
fi
if [ $_tests_verbose -gt 2 ]; then
tests:debug "wrote the file $file with content:"
tests:colorize fg 208 _tests_indent 'file' < $file
fi
}
# @description Asserts that stdout is empty.
#
# @example
# tests:eval echo ""
#
# tests:assert-stdout-empty
#
# @noargs
tests:assert-stdout-empty() {
tests:assert-empty stdout
}
# @description Asserts that stderr is empty.
#
# @example
# tests:eval echo "" '1>&2'
#
# tests:assert-stderr-empty
#
# @noargs
tests:assert-stderr-empty() {
tests:assert-empty stderr
}
# @description Asserts that target is empty.
#
# @example
# tests:eval echo ""
#
# tests:assert-empty stdout
#
# @noargs
tests:assert-empty() {
tests:assert-re "$1" ""
}
# @description Asserts that stdout of last evaluated command matches given
# regexp.
#
# @example
# tests:eval echo 123
#
# tests:assert-stdout-re 1.3 # will pass
#
# @arg $1 regexp Regexp, same as in grep.
tests:assert-stdout-re() {
tests:assert-re stdout "$@"
}
# @description Asserts as 'tests:assert-stdout-re', but stderr used instead
# of stdout.
#
# @example
# tests:eval echo 123 '1>&2' # note quotes
#
# tests:assert-stderr-re 1.3 # will pass
#
# @arg $1 regexp Regexp, same as in grep.
tests:assert-stderr-re() {
tests:assert-re stderr "$@"
}
# @description Asserts that last evaluated command exit status is zero.
#
# @example
# tests:eval true
# tests:assert-success
#
# @noargs
tests:assert-success() {
tests:assert-exitcode 0
}
# @description Asserts that last evaluated command exit status is not zero.
# Basically, alias for `test:not tests:assert-success`.
#
# @example
# tests:eval false
# tests:assert-fail
#
# @noargs
tests:assert-fail() {
tests:not tests:assert-success
}
# @description Asserts that exit code of last evaluated command equals to
# specified value.
#
# @example
# tests:eval false
# tests:assert-exitcode 1
#
# @arg $1 int Expected exit code.
tests:assert-exitcode() {
if [ ! -f "$_tests_run_exitcode" ]; then
tests:debug "(assert-exitcode) BUG: _tests_run_exitcode is empty"
_tests_interrupt
fi
local actual=$(cat $_tests_run_exitcode)
local expected=$1
shift
_tests_make_assertion "$expected" "$actual" \
"command exited with code" \
"actual exit code = $actual" \
"expected exit code $_tests_last_assert_operation $expected\n"
_tests_inc_asserts_count
}
# @description Negates passed assertion.
#
# @example
# tests:eval false
# tests:assert-fail
# tests:not tests:assert-success
#
# tests:eval true
# tests:assert-success
# tests:not tests:assert-fail
#
# @arg $@ any Command to evaluate.
tests:not() {
_tests_assert_operation="!="
_tests_last_assert_operation="!="
"${@}"
_tests_assert_operation="="
}
# @description Prevets eval command to print stdout/stderr.
#
# @example
# tests:silence tests:eval rm -r blah
#
# @arg $@ any Command to evaluate.
tests:silence() {
_tests_eval_silence="1"
"${@}"
_tests_eval_silence=""
}
# @description Output message and fail current testcase immideately.
#
# @arg $@ any String to output.
tests:fail() {
tests:describe "$@"
_tests_interrupt
}
# @description Same as tests:debug(), but colorize output
# for better vizibility.
#
# @arg $@ any String to output.
tests:describe() {
tests:debug "@@ \e[7;49;34m" ${@} "\e[0m"
}
# @description Print specified string in the debug log.
#
# @example
# tests:debug "hello from debug" # will shown only in verbose mode
#
# @arg $@ any String to echo.
tests:debug() {
local output=$(_tests_get_debug_fd)
if [ $_tests_verbose -lt 1 ]; then
return
fi
local prefix=$_tests_debug_prefix
if [ $_tests_verbose -ge 6 ]; then
prefix="<$BASHPID> $prefix"
fi
if [ "$_tests_dir" ]; then
echo -e "${prefix}# $@"
else
echo -e "### $@"
fi >&${output}
}
# @description Changes working directory to specified directory.
#
# @arg $1 directory Directory to change to.
tests:cd() {
local dir=${1:-$(tests:get-tmp-dir)}
tests:debug "\$ cd $dir"
if [[ ! -d "$dir" ]]; then
tests:debug "error changing working directory to $dir:"
_tests_indent 'error' <<< "$(readlink -fm $dir) not found"
_tests_interrupt
else
builtin cd $dir
fi
}
# @description Evaluates specified string via shell 'eval'.
#
# Redirection syntax differs from what can be found in bash.
#
# Redirection operators will be used as redirection only if they are
# passed as separate argumentm, like this: `tests:eval echo 1 '>' 2`.
#
# List of redirection operators:
# * `>`
# * `<`
# * `>&`
# * `<&`
# * `>&n`, where `n` is a number
# * `<&n`, where `n` is a number
# * `>>`
# * `<<<`
# * `<>`
# * `|`
#
# To redirect output to file use: `> filename` (note space).
#
# Also, if only one argument is passed to `tests:eval`, the it will
# be evaled as is. So, `tests:eval "echo 1 > 2"` will create file `2`,
# but `tests:eval echo "1 > 2"` will only output `1 > 2` to the stdout.
#
# *NOTE*: you will not get any stdout or stderr from evaluated command.
# To obtain stdout or stderr see `tests:pipe`.
#
# *NOTE*: output will be buffered! If you want unbuffered output, use
# `tests:runtime`.
#
# *NOTE*: use of that function will not produce any output to stdout
# nor stderr. If you want to pipe your result to something, use
# `tests:pipe`.
#
# @example
# tests:eval echo 123 "# i'm comment"
# tests:eval echo 123 \# i\'m comment
# tests:eval echo 567 '1>&2' # redirect to stderr
# tests:eval echo 567 1\>\&2' # same
#
# @arg $@ string String to evaluate.
# @see tests:pipe
# @see tests:runtime
tests:eval() {
local output
_tests_prepare_eval_namespace eval
exec {output}>$_tests_run_output
_tests_eval_and_output_to_fd ${output} ${output} "${@}"
exec {output}<&-
}
# @description Same, as `tests:pipe`, but produce unbuffered result.
#
# @example
# tests:runtime 'echo 1; sleep 10; echo 2' # see 1 immediately
#
# @arg $@ string String to evaluate.
# @see tests:eval
tests:runtime() {
local stdout
local stderr
_tests_prepare_eval_namespace eval
exec {stdout}>$_tests_run_output
exec {stderr}>$_tests_run_output
# because of pipe, environment variables will not bubble out of
# _tests_eval_and_output_to_fd call
_tests_prepare_eval_namespace eval
{ { _tests_eval_and_output_to_fd ${stdout} ${stderr} "${@}" \
{stdout}>&1 | _tests_indent 'runtime stdout' ; } \
{stderr}>&1 | _tests_indent 'runtime stderr' ; }
exec {stdout}<&-
exec {stderr}<&-
}
# @description Same, as `tests:eval`, but return stdout and stderr
# as expected.
#
# @example
# lines=$(tests:eval echo 123 | wc -l) # note not escaped pipe
# tests:assert-equals $lines 1
#
# @arg $@ string String to evaluate.
# @see tests:eval
tests:pipe() {
local stdout
local stderr
_tests_prepare_eval_namespace eval
exec {stdout}>&1
exec {stderr}>&2
_tests_eval_and_output_to_fd ${stdout} ${stderr} "${@}"
exec {stdout}<&-
exec {stderr}<&-
}
# @description Same, as `tests:eval`, but writes stdout into given variable and
# return stderr as expected.
#
# @example
# _x() {
# echo "y [$@]"
# }
# tests:value response _x a b c
# tests:assert-equals "$response" "y [a b c]"
#
# @arg $1 string Variable name.
# @arg $@ string String to evaluate.
# @see tests:eval
tests:value() {
local __variable__="$1"
local __value__=""
shift
tests:ensure "${@}"
__value__="$(cat "$(tests:get-stdout-file)")"
eval $__variable__=\"\${__value__}\"
}
# @description Eval specified command and assert, that it has zero exitcode.
#
# @example
# tests:esnure true # will pass
# tests:esnure false # will fail
#
# @arg $@ any Command to evaluate.
tests:ensure() {
tests:eval "$@"
tests:assert-success
}
# @description Creates temporary directory.
#
# @arg $@ any Same as for mkdir command.
tests:make-tmp-dir() {
# prepend to any non-flag argument $_tests_dir prefix
if [ $_tests_verbose -gt 2 ]; then
tests:debug "making directories in $_tests_dir_root: mkdir ${@}"
fi
local stderr
if ! stderr=$(
command mkdir -p \
$(sed -re "s#(^|\\s)([^-])#\\1$_tests_dir_root/\\2#g" <<< "${@}")); then
tests:debug "error making directories ${@}:"
_tests_indent 'error' <<< "$stderr"
_tests_interrupt
fi
}
# @description Changes working directory to the specified temporary directory,
# previously created by 'tests:make-tmp-dir'.
#
# @arg $1 string Directory name.
tests:cd-tmp-dir() {
tests:cd $_tests_dir_root/$1
}
# @description Runs any command in background, this is very useful if you test
# some running service.
#
# Processes which are ran by 'tests:background' will be killed on cleanup
# state, and if test failed, stderr and stdout of all background processes will
# be printed.
#
# @arg $1 variable Name of variable to store BG process ID.
# @arg $@ string Command to start.
tests:run-background() {
local _run_id_var="$1"
shift
local _run_debug_old_prefix=$_tests_debug_prefix
_tests_debug_prefix="[BG] <$BASHPID> "
local _run_coproc=""
local _run_pid=""
coproc:run-immediately _run_coproc "${@}"
_tests_debug_prefix=$_run_debug_old_prefix
coproc:get-pid "$_run_coproc" _run_pid
command mkdir $_tests_dir/.bg/$_run_pid
ln -s "$_run_coproc" $_tests_dir/.bg/$_run_pid/coproc
builtin eval $_run_id_var=\$_tests_dir/.bg/\$_run_pid
tests:debug "! running coprocess with pid <$_run_pid>:"
_tests_indent "coproc" <<< "${@}"
local _run_stdout=""
local _run_stderr=""
coproc:get-stdout-fd "$_run_coproc" _run_stdout
coproc:get-stderr-fd "$_run_coproc" _run_stderr
_tests_run_bg_reader \
$_run_stdout $_tests_dir/.bg/$_run_pid/stdout "<$_run_pid> stdout"
_tests_run_bg_reader \
$_run_stderr $_tests_dir/.bg/$_run_pid/stderr "<$_run_pid> stderr"
}
_tests_run_bg_reader() {
local input_fd=$1
local output=$2
local prefix=$3
( cat <&$input_fd | tee $output | _tests_indent "$prefix" ) &
}
# @description Returns pid of specified background process.
#
# @arg $1 string Process ID, returned from 'tests:run-background'.
#
# @stdout Pid of background process.
tests:get-background-pid() {
local _pid=""
coproc:get-pid "$(readlink -f "$1/coproc")" _pid
echo $_pid
}
# @description Returns stdout of specified background process.
#
# @arg $1 string Process ID, returned from 'tests:run-background'.
#
# @stdout Stdout from background process.
tests:get-background-stdout() {
echo "$1/stdout"
}
# @description Returns stderr of specified background process.
#
# @arg $1 string Process ID, returned from 'tests:run-background'.
#
# @stdout Stderr from background process.
tests:get-background-stderr() {
echo "$1/stderr"
}
# @description Stops background process with 'kill -TERM'.
#
# @arg $1 string Process ID, returned from 'tests:run-background'.
tests:stop-background() {
local bg_proc="$1"
tests:debug "! terminating coprocess <${bg_proc##*/}>"
coproc:stop $(readlink -f "$bg_proc/coproc")
tests:debug "! coprocess <${bg_proc##*/}> has been terminated"
}
# @description Waits, until specified file will be changed or timeout passed
# after executing specified command.
#
# @arg $1 string Command to evaluate.
# @arg $2 filename Filename to wait changes in.
# @arg $3 int Interval of time to check changes after.
# @arg $4 int Timeout in seconds.
tests:wait-file-changes() {
local file="$1"
local sleep_interval="$2"
local sleep_max="$3"
shift 3
local stat_initial=$(stat $file)
local sleep_iter=0
local sleep_iter_max=$(bc <<< "$sleep_max/$sleep_interval")
tests:debug "! waiting file changes after command:" \
"file '$file' (${sleep_max}sec max)"
if [ $# -gt 0 ]; then
if [ $_tests_verbose -gt 3 ]; then
_tests_escape_cmd "$@" \
| _tests_pipe tests:colorize fg 5 _tests_indent 'eval' \
>&$_tests_debug_fd
fi
_tests_pipe "$@"
fi
while true; do
sleep_iter=$(($sleep_iter+1))
local stat_actual=$(stat $file)
if [[ "$stat_initial" == "$stat_actual" ]]; then
if [[ $sleep_iter -ne $sleep_iter_max ]]; then
sleep $sleep_interval
continue
fi
tests:debug "! file left unchanged: file '$file' (after $( \
bc <<< "$sleep_iter * $sleep_interval" )sec)"
return 1
fi
tests:debug "! file changed: file '$file' (after $( \
bc <<< "$sleep_iter * $sleep_interval" )sec)"
return 0
done
}
# @description Sets verbosity of testcase output.
#
# @arg $1 int Verbosity.
tests:set-verbose() {
_tests_verbose=$1
}
# @description Gets current verbosity level.
#
# @noargs
# @stdout Current verbosity.
tests:get-verbose() {
printf "%s" $_tests_verbose
}
# @description Copy specified file or directory from the testcases
# dir to the temporary test directory.
#
# @arg $@ any Same args, as for cp commmand.
tests:clone() {
local args=()
local last_arg=""
while [ $# -gt 0 ]; do
if [ "$last_arg" ]; then
args+=($_tests_base_dir/$last_arg)
fi
last_arg=""
if grep -q '^-' <<< "$1"; then