-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesht.sh
executable file
·155 lines (131 loc) · 2.78 KB
/
tesht.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
#!/usr/bin/env bash
# Tesht is a microframework to help you to test your bash scripts!
# by Alexandre Prates Oct/2017
# set -x
echo -e "Loading Tesht\n"
function __uuid() {
if [ -f /proc/sys/kernel/random/uuid ]; then
cat /proc/sys/kernel/random/uuid
elif [ -x "$(command -v uuidgen)" ]; then
uuidgen
else
echo Cannot generate UUID!
exit 1
fi
}
BROKEN=
function setup() {
pushd . > /dev/null
TESTFAILED=
FAILLOG=/tmp/$(__uuid)
STDOUT=/tmp/$(__uuid)
STDERR=/tmp/$(__uuid)
RETURNCODE=
COMMAND=
}
function __success() {
printf "."
}
function __fail() {
TESTFAILED=true
BROKEN=true
MESSAGE="IN ${FUNCNAME[*]: -3:1}:${BASH_LINENO[*]: -3:1}\n"
if [ $# -eq 0 ]; then
MESSAGE="$MESSAGE Command: \"$COMMAND\"\n"
MESSAGE="$MESSAGE \"$(cat $STDERR | cut -d: -f 3-)\""
else
MESSAGE="$MESSAGE $1"
fi
echo "$MESSAGE" >> $FAILLOG
printf "f"
}
function test() {
$@ 1>$STDOUT 2>$STDERR
RETURNCODE=$?
COMMAND=$@
}
function assert_equal() {
if [[ $1 == $2 ]]; then
__success
else
local MESSAGE=$"EXPECTED: \"$1\" \n GOT: \"$2\""
__fail "$MESSAGE"
fi
}
function assert_match() {
if [[ $2 =~ $1 ]]; then
__success
else
local MESSAGE=$"Not match: \"$1\"\n IN: \"$2\""
__fail "$MESSAGE"
fi
}
function assert_not_equal() {
if [[ ! $1 == $2 ]]; then
__success
else
local MESSAGE=$"EXPECTED: \"$1\"\n GOT: \"$2\""
__fail "$MESSAGE"
fi
}
function assert_not_match() {
if [[ ! $2 =~ $1 ]]; then
__success
else
local MESSAGE=$"Not match: \"$1\"\n IN: \"$2\""
__fail "$MESSAGE"
fi
}
function assert_stdout() {
local MESSAGE="`cat $STDOUT`"
assert_match "$@" "$MESSAGE"
}
function assert_stderr() {
local MESSAGE=$(cat $STDERR)
assert_match "$@" "$MESSAGE"
}
function assert_not_stdout() {
local MESSAGE=$(cat $STDOUT)
assert_not_match "$@" "$MESSAGE"
}
function assert_not_stderr() {
local MESSAGE=$(cat $STDERR)
assert_not_match "$@" "$MESSAGE"
}
function assert_success() {
[ 0 -eq $RETURNCODE ] && __success || __fail
}
function assert_fail() {
if [ $# -eq 0 ]; then
[ $RETURNCODE -ne 0 ] && __success || __fail "Received code $RETURNCODE"
else
[ $RETURNCODE -eq $1 ] && __success || __fail "Expect code $1\n Received code $RETURNCODE"
fi
}
function assert_dir() {
[[ -d $1 ]] && __success || __fail "Dir '$1' does not exists."
}
function assert_file() {
[[ -e $1 ]] && __success || __fail "File '$1' does not exists."
}
function report() {
popd > /dev/null
echo ""
if [ $TESTFAILED ]; then
echo -e "$(cat $FAILLOG)"
fi
echo ""
}
for TESTFILE in $@ ; do
setup
echo "Running $TESTFILE"
source $TESTFILE
report
done
if [ $BROKEN ]; then
echo "Sorry something is broken"
exit 1
else
echo "Congratulations everthing is right!"
exit 0
fi