-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·234 lines (198 loc) · 4.78 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/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)
# Placeholder for an empty URL.
readonly URL="https://www.youtube.com/watch?v=lmvUFhjZdFc"
readonly DEFAULT_OUTPUT="./test-sections"
log_succ() {
printf "${GREEN}%s${RESET}\n" "${*}"
}
log_warn() {
printf "${YELLOW}%s${RESET}\n" "${*}" 1>&2
}
log_err() {
printf "${RED}%s${RESET}\n" "${*}" 1>&2
}
usage() {
cat <<USAGE
Usage: ./test.sh URL
Run tests for yt-album.
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
before_test
set +e
test "$test_number" "$folder"
local ret="$?"
if [[ $ret != "0" && $exit_code == "0" ]]; then
exit_code="$ret"
fi
set -e
((test_number++))
done
after_tests
exit $exit_code
}
before_test() {
clean
setup
}
after_tests() {
clean
}
clean() {
rm -rf "$DEFAULT_OUTPUT"
}
setup() {
mkdir -p "$DEFAULT_OUTPUT"
}
test() {
local test_number="$1"
local folder="$2"
local out="$DEFAULT_OUTPUT"
local sections_file
local url
[[ -f "$folder/sections.txt" ]] && sections_file="$folder/sections.txt"
[[ -f "$folder/url.txt" ]] && url=$(cat "$folder/url.txt")
local expected
expected=$(cat "$folder/expected.txt")
local actual
actual="$(yt-album "${sections_file-}" "${url-}" "$out")"
local expected_ffprobe
local actual_ffprobe
if [[ -f "$folder/ffprobe.txt" ]]; then
expected_ffprobe="$(cat "$folder/ffprobe.txt")"
local title
title="$(cat "$folder/title.txt")"
actual_ffprobe="$(ffprobe_durations "$out/$title")"
fi
if ! is_ok "$expected" "$actual"; then
log_not_ok "$test_number" "$folder" "$expected" "$actual"
(exit 1)
elif ! is_ok "${expected_ffprobe-}" "${actual_ffprobe-}"; then
log_not_ok "$test_number" "$folder" "${expected_ffprobe-}" \
"${actual_ffprobe-}"
(exit 2)
else
log_ok "$test_number" "$folder"
(exit 0)
fi
}
yt-album() {
local sections_file="$1"
local url="$2"
local out="$3"
if [[ -n "${sections_file-}" && -n "${url-}" ]]; then
./yt-album.sh --no-color --no-progress --output "$out" \
--sections "$sections_file" -- "$url" 2>&1
elif [[ -n "${sections_file-}" ]]; then
./yt-album.sh --no-color --no-progress --output "$out" \
--sections "$sections_file" -- "$URL" 2>&1
else
./yt-album.sh --no-color --no-progress --output "$out" \
-- "$url" 2>&1
fi
}
ffprobe_durations() {
local out="$1"
local res=""
for file in "$1"/*; do
[[ ! -f "$file" ]] && continue
res+="$(basename "$file")"
res+=$'\n'
line="$(ffprobe -v error -show_entries format=duration \
-of default=noprint_wrappers=1:nokey=1 "$file")"
res+="$(LC_ALL=C printf "%.0f\n" "$line")"
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 1 -type d | wc -l)"
# See <https://testanything.org/>.
printf "%s\n" "TAP version 14"
printf "%s\n" "1..$count"
}
main