-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest
executable file
·80 lines (71 loc) · 2.13 KB
/
test
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
#!/bin/bash
function run {
valgrindlog=$(mktemp --tmpdir valgrind.log.XXXXXXXXXX)
valgrind --quiet --log-file="$valgrindlog" --error-exitcode=124 --leak-check=full --track-fds=yes ./ja2l "$@"
status=$?
sed -i '/FILE DESCRIPTORS: 1 open at exit./,+4 d' -- "$valgrindlog" # 1 is expected, the valgrind logfile fd
cat >&2 -- "$valgrindlog"
rm -f -- "$valgrindlog"
if ((status != 0)); then
exit 1
fi
}
function assertEquals {
if [[ "$1" != "$2" ]]; then
printf >&2 '%s: assertion failure\n' "${3:-(unknown function)}"
diff >&2 -u --label "expected" --label "actual" <(printf '%s' "$1") <(printf '%s' "$2")
exit 1
fi
}
function assertMatches {
if ! [[ "$2" =~ $1 ]]; then
printf >&2 '%s: assertion failure\nPattern: %s\nActual:\n%s\n' "${3:-(unknown function)}" "$1" "$2"
exit 1
fi
}
function closeExtraFds {
for fd in /dev/fd/*; do
fd=${fd#/dev/fd/}
if ((fd > 2)); then
exec {fd}<&-
fi
done
}
function testSingleLine {
expected='{}'
actual=$(printf '[\n{}\n]\n' | run)
if (($?)); then
exit 1
fi
assertEquals "$expected" "$actual" "${FUNCNAME[0]}"
}
function testMultiLine {
expected=$'{}\n{}'
actual=$(printf '[\n{},\n{}\n]\n' | run)
if (($?)); then
exit 1
fi
assertEquals "$expected" "$actual" "${FUNCNAME[0]}"
}
function testHelp {
outputShort=$(run -h)
assertEquals 'exit code 0' "exit code $?" "${FUNCNAME[0]}"
outputLong=$(run --help)
assertEquals 'exit code 0' "exit code $?" "${FUNCNAME[0]}"
assertEquals "$outputShort" "$outputLong"
assertMatches '^Usage: ' "$outputShort" "${FUNCNAME[0]}"
}
function testVersion {
outputShort=$(run -v)
assertEquals 'exit code 0' "exit code $?" "${FUNCNAME[0]}"
outputLong=$(run --version)
assertEquals 'exit code 0' "exit code $?" "${FUNCNAME[0]}"
assertEquals "$outputShort" "$outputLong"
assertMatches '[[:digit:]]\.[[:digit:]]' "$outputShort" "${FUNCNAME[0]}"
assertMatches 'AGPL' "$outputShort" "${FUNCNAME[0]}"
}
closeExtraFds
testSingleLine
testMultiLine
testHelp
testVersion