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