-
Notifications
You must be signed in to change notification settings - Fork 1
/
shunit
executable file
·159 lines (138 loc) · 3.96 KB
/
shunit
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
#!/bin/bash
# GPL License
#
# Copyright (c) 2019 Meyers Tom
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Build and maintained by Tom Meyers
# Contact email- tom@odex.be
# Contact github - F0xedb
version="v0.1.1"
# color information
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;35m'
NC='\033[0m' # No Color
# location of the assert file
name=$(basename "$0")
directory=$(printf "%s" "$0" | sed "s:$name::")
source "$directory"assert.sh
# special settings
shopt -s globstar
trap "destroyTest && destory" SIGHUP
# Help info
function help() {
printf "${BLUE}%s${NC} options -> -h | -v | .sh\n" "$name"
printf "${BLUE}USAGE:${NC}\n"
printf "%s -v \t\t\t\tPrint the current version\n" "$name"
printf "%s (-h --help) \t\t\tPrint this help menu\n" "$name"
printf "%s <shell script> \t\t\tSource a shell script before and after all tests have ran. function start and stop will be called. \n" "$name"
}
# default behaviour of special functions
function setup() {
return 0
}
function destroy() {
return 0
}
function setupTest() {
return 0
}
function destroyTest() {
return 0
}
function start() {
return 0
}
function stop() {
return 0
}
# $1 is the unit filename
# $2 is the function name
function run-function() {
source "$1"
echo -e "$BLUE Running test: $2 $NC"
setupTest || echo -e "${RED}Error: running setupTest${NC}"
"$2"
destroyTest || echo -e "${RED}Error: running destroyTest${NC}"
}
# execute all test in a single unit $1 is the shUnit filename $2 is the units file name
function execute-test() {
func=$(grep "function Test.*{" "$2" | sed 's:function ::' | sed 's: {::')
for unitTest in $func; do
"$1" -f "$2" "$unitTest"
done
}
# $1 is the shUnit
# $2 is the unitfile to be ran
function run-unit() {
# get the file comment
desc=$(head -n1 "$2" | sed 's:^#::')
# print out the file comment
echo -e "$BLUE$desc$NC"
# load in the file
source "$2"
# perform initial setup
setup || echo -e "${RED}Error: running setup${NC}"
execute-test "$1" "$2"
# perform destruction
destroy || echo -e "${RED} Error: running destroy${NC}"
}
function reset() {
if [[ -d ".shunit" ]]; then
rm -rf .shunit
fi
mkdir .shunit
touch .shunit/pass
touch .shunit/error
}
# $1 is the path to shUnit
function iterate() {
reset
start || echo -e "${RED}Error: running startup${NC}"
for unit in **/*.shUnit; do
if [[ -f "$unit" ]]; then
echo -e "$BLUE Running unit: $NC$unit"
"$1" -r "$unit"
echo -e "$BLUE--------------------------------------------------------$NC"
fi
done
stop || echo -e "${RED}Error: running stop function${NC}"
pass=$(wc -l .shunit/pass | awk '{print $1}')
error=$(wc -l .shunit/error | awk '{print $1}')
echo "Pass: $pass, Errors: $error"
if [[ "$error" -gt "0" ]]; then
exit 1
fi
}
if [[ "$1" == "-r" ]]; then
run-unit "$0" "$2"
elif [[ "$1" == "-f" ]]; then
run-function "$2" "$3"
elif [[ "$1" == "-v" ]]; then
echo $version
exit 0
elif [[ "$1" == "-h" || "$1" == "--help" ]]; then
help
else
if [[ -f "$1" ]]; then
source "$1"
fi
iterate "$0"
fi