-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_tests.test.bash
93 lines (78 loc) · 2.11 KB
/
run_tests.test.bash
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
#!/usr/bin/env bash
set -eu -o pipefail
exec {stdout}>&1 1>&2
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "$script_dir"
echo "case: show help message" >&${stdout}
./run_tests.bash --help | grep -qE "^Usage"
echo "case: test success" >&${stdout}
# given:
rm -rf ./tmp && mkdir ./tmp
cat > ./tmp/success.test.bash << 'SCRIPT'
echo "case: success"
test 1 -eq 1 >&2
SCRIPT
# when:
out=$(./run_tests.bash --logfile ./tmp/success-test.log ./tmp/*.test.bash)
# then:
expected=$(cat << EXPECTED
$(tput setaf 4)test: ./tmp/success.test.bash$(tput sgr0)
case: success
$(tput setaf 2)all case passed$(tput sgr0)
EXPECTED
)
diff -u <(echo "$expected") <(echo "$out")
# then:
grep -q "+ echo 'case: success'" ./tmp/success-test.log
echo "case: test fail" >&${stdout}
# given:
rm -rf ./tmp && mkdir ./tmp
cat > ./tmp/fail.test.bash << 'SCRIPT'
echo "case: fail"
test 1 -eq 2 >&2
SCRIPT
# when:
out=$(./run_tests.bash --logfile ./tmp/fail-test.log ./tmp/*.test.bash) || exit_status=$?
# then:
test "$exit_status" -ne 0
expected=$(cat << EXPECTED
$(tput setaf 4)test: ./tmp/fail.test.bash$(tput sgr0)
case: fail
$(tput setaf 1)failed$(tput sgr0)
+ echo 'case: fail'
+ test 1 -eq 2
EXPECTED
)
diff -u <(echo "$expected") <(echo "$out")
# then:
grep -q "+ echo 'case: fail'" ./tmp/fail-test.log
echo "case: run all scripts but fail if any test fail" >&${stdout}
# given:
rm -rf ./tmp && mkdir ./tmp
cat > ./tmp/success.test.bash << 'SCRIPT'
echo "case: success"
test 1 -eq 1 >&2
SCRIPT
cat > ./tmp/fail.test.bash << 'SCRIPT'
echo "case: fail"
test 1 -eq 2 >&2
SCRIPT
# when:
out=$(./run_tests.bash --logfile ./tmp/fail-test.log ./tmp/{fail,success}.test.bash) || exit_status=$?
# then:
test "$exit_status" -ne 0
expected=$(cat << EXPECTED
$(tput setaf 4)test: ./tmp/fail.test.bash$(tput sgr0)
case: fail
$(tput setaf 1)failed$(tput sgr0)
+ echo 'case: fail'
+ test 1 -eq 2
$(tput setaf 4)test: ./tmp/success.test.bash$(tput sgr0)
case: success
$(tput setaf 2)all case passed$(tput sgr0)
EXPECTED
)
diff -u <(echo "$expected") <(echo "$out")
# then:
grep -q "+ echo 'case: success'" ./tmp/fail-test.log
grep -q "+ echo 'case: fail'" ./tmp/fail-test.log