-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_bash_testsuite.sh
executable file
·109 lines (96 loc) · 2.31 KB
/
run_bash_testsuite.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
#!/bin/bash
set -u
# path to tests is the root directory to
# start a recursive search for test files
if [[ $# != 1 ]]; then
echo "usage: $0 <path-to-tests>"
exit 1
fi
# search for convention
# test files should have suffix '_test'
TEST_DIR=$1
TESTFILES=$(find "${TEST_DIR}" -name "*_test.sh")
EXITCODE=0
SUCCESS=0
FAILURE=1
Should="ToBe"
ShouldNot="ToNotBe"
temp_file=$(mktemp)
function setred {
printf '\033[1;31m'
}
function setgreen {
printf '\033[40;38;5;82m'
}
function setdefault {
printf '\033[0m'
}
function Expect () {
if [[ $# != 3 ]]; then
echo "you are not calling Expect properly"
echo "it requires 3 args you gave $#"
return ${FAILURE}
fi
local positivePath=$2
local testCase=$1
local expectedValue=$3
if [[ ${positivePath} == ${Should} ]]; then
if [[ ${testCase} != ${expectedValue} ]]; then
echo "${testCase} should match ${expectedValue}"
return ${FAILURE}
fi
else
if [[ ${testCase} == ${expectedValue} ]]; then
echo "${testCase} should not match ${expectedValue}"
return ${FAILURE}
fi
fi
return ${SUCCESS}
} >> ${temp_file}
echo
echo "-------------------------------------------"
echo "running bash test suite"
# using the convention of each test file should have a
# function file excluding '_test' suffix
for test in ${TESTFILES}; do
(
source ${test//_test.sh/.sh}
source ${test}
EXITCODE=0
# convention for function names is
# functions with prefix 'Test' will be executed
# and the output will pass/fail the pipeline
for testFunc in $(typeset -f | grep '^Test.*()' | awk '{print $1}'); do
if eval "${testFunc} >> ${temp_file}"; then
setgreen
printf "."
setdefault
else
EXITCODE=1
setred
echo "(test failed !!!!! )"
echo ${testFunc}
cat ${temp_file}
echo
echo "----------------------------------------------------"
echo
setdefault
fi
rm ${temp_file}
done
exit $EXITCODE
)
EXITCODE=$?
done
if [[ ${EXITCODE} == 0 ]];then
setgreen
echo
echo "Test Suite Passed"
setdefault
else
setred
echo
echo "Test Suite Failed"
setdefault
fi
exit ${EXITCODE}