-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·182 lines (153 loc) · 3.47 KB
/
test.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
#!/usr/bin/env bash
set -euo pipefail
# Allows running in debug mode by setting the TRACE environment variable.
# e.g. <TRACE=1 ./yt-album.sh>
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
log_succ() {
printf "${GREEN}%s${RESET}\n" "${*}"
}
log_warn() {
printf "${YELLOW}%s${RESET}\n" "${*}"
}
log_err() {
printf "${RED}%s${RESET}\n" "${*}"
}
usage() {
cat <<USAGE
Usage: ./test.sh URL
Run tests for whatsapp-date.
Options:
-h, --help
Show this help message and exit.
--no-color
Disable colored output.
--verbose
Enable verbose output.
Examples:
./test.sh
# Debug mode
TRACE=1 ./test.sh
USAGE
}
die() {
log_err "${*}"
usage
exit 1
}
while :; do
case ${1-} in
# Two hyphens ends the options parsing
--)
shift
break
;;
-h | --help | help)
usage
exit
;;
--verbose)
VERBOSE=1
;;
--no-color)
GREEN=""
YELLOW=""
RED=""
RESET=""
;;
# Anything remaining that starts with a dash triggers a fatal error
-?*)
die "The command line option is unknown: $1"
;;
# Anything remaining is treated as content not a parseable option
*)
break
;;
esac
shift
done
main() {
log_header
local test_number=1
local exit_code=0
for folder in tests/*; do
[[ -f "$folder" ]] && continue
set +e
test "$test_number" "$folder"
local ret="$?"
if [[ $ret != "0" && $exit_code == "0" ]]; then
exit_code="$ret"
fi
set -e
((test_number++))
done
exit $exit_code
}
test() {
local test_number="$1"
local folder="$2"
local expected
expected="$(cat "$folder/expected.txt")"
local actual
actual="$(TZ=UTC ./whatsapp-date.sh --no-color "$folder/input" 2>&1)"
local expected_stat
local actual_stat
if [[ -f "$folder/stat.txt" ]]; then
expected_stat="$(cat "$folder/stat.txt")"
actual_stat="$(get_stats "$folder/input")"
fi
if ! is_ok "$expected" "$actual"; then
log_not_ok "$test_number" "$folder" "$expected" "$actual"
(exit 1)
elif ! is_ok "${expected_stat-}" "${actual_stat-}"; then
log_not_ok "$test_number" "$folder" "${expected_stat-}" \
"${actual_stat-}"
(exit 2)
else
log_ok "$test_number" "$folder"
(exit 0)
fi
}
get_stats() {
local image_folder="$1"
local res=""
for file in "$image_folder"/*; do
res+="$(basename "$file"):$(TZ=UTC stat --format="%y" "$file")"
res+=$'\n'
done
printf "%s" "$res"
}
is_ok() {
local expected="$1"
local actual="$2"
[[ "$actual" == "$expected" ]]
}
log_ok() {
local test_number="$1"
local test_name="$2"
printf "${GREEN}ok %s${RESET} - %s\n" "$test_number" "$test_name"
}
log_not_ok() {
local test_number="$1"
local test_name="$2"
local expected="$3"
local actual="$4"
printf "${RED}not ok %s${RESET} - %s\n" "$test_number" "$test_name"
if [[ -n "${VERBOSE-}" ]]; then
log_err "---"
log_warn "Expected:"
log_succ "$expected"
log_warn "Actual:"
log_err "$actual"
fi
}
log_header() {
count="$(find tests/* -maxdepth 0 -type d | wc -l)"
# See <https://testanything.org/>.
printf "%s\n" "TAP version 14"
printf "%s\n" "1..$count"
}
main