-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-args.sh
executable file
·71 lines (58 loc) · 1.72 KB
/
test-args.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
#!/bin/bash
# shellcheck disable=SC1091
source ./build-args.sh
source ./constants.sh
source ./parse-programs.sh
source ./parse-options.sh
function run-test {
local test_file snaphot_file snaphot USAGE test_result \
output error
local -i status
test_file=$1
USAGE="$(cat "$test_file")"
snaphot_file="$(dirname "$test_file")/__snapshots__/$(basename "$test_file")"
printf 'Running test: %s...' "$test_file"
if ! test -f "$snaphot_file"; then
printf '\nSnapshot file not found: %s\n' "$snaphot_file" >&2
exit 1
fi
snaphot="$(cat "$snaphot_file")"
# shellcheck disable=SC2034
local -a programs=() \
arguments=() \
options=()
printf -v test_result '%s\n\n' "$USAGE"
parse-options "$USAGE" 'error'
status=$?
if test $status -eq 0; then
parse-programs "$USAGE" 'error'
status=$?
else
printf -v test_result '%sERROR: %s' "$test_result" "$error"
fi
if test $status -eq 0; then
output="$(build-args 2>&1)"
printf -v test_result '%s%s' "$test_result" "$output"
else
printf -v test_result '%sERROR: %s' "$test_result" "$error"
fi
if [[ "$snaphot" != "$test_result" ]]; then
printf ' failed!\n\nTest: %s did not match snapshot\n\n' "$test_file" >&2
printf '%s\n\n' "$USAGE" >&2
diff -Naurd --label='snapshot' <(printf '%s' "$snaphot") --label='test-result' <(printf '%s' "$test_result") >&2
printf '\n'
else
printf ' pass.\n'
fi
}
function run-tests {
if ! test -z "$1"; then
run-test "$1"
else
local t
for t in tests/args/*.args; do
run-test "$t"
done
fi
}
run-tests "$1"