-
Notifications
You must be signed in to change notification settings - Fork 4
/
doctest.sh
executable file
·95 lines (77 loc) · 2.41 KB
/
doctest.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
#!/usr/bin/env bash
#
# This file runs code snippets in doc/*.md files.
set -e -o pipefail
for arg in "$@"; do
if [[ "$arg" =~ ^- ]]; then
echo "Usage: $0 [doc/file1.md doc/file2.md ...]" >&2
exit 2
fi
done
if [ $# == 0 ]; then
files=(doc/*.md)
else
files=("$@")
fi
if [[ "${OS:=$(uname)}" =~ Windows ]]; then
source activate
mingw32-make
jou="$PWD/jou.exe"
elif [[ "$OS" =~ NetBSD ]]; then
gmake
jou="$PWD/jou"
else
make
jou="$PWD/jou"
fi
DIFF=$(which gdiff || which diff)
if $DIFF --help | grep -q -- --color; then
diff_color="--color=always"
fi
function generate_expected_output()
{
local joufile="$1"
(grep -onH '# Warning: .*' "$joufile" || true) | sed -E s/'(.*):([0-9]*):# Warning: '/'compiler warning for file "test.jou", line \2: '/
(grep -onH '# Error: .*' "$joufile" || true) | sed -E s/'(.*):([0-9]*):# Error: '/'compiler error in file "\1", line \2: '/
(grep -oE '# Output:.*' "$joufile" || true) | sed -E s/'^# Output: ?'//
}
rm -rf tmp/doctest
mkdir -p tmp/doctest
for file in "${files[@]}"; do
echo "Extracting doctests from $file..."
temp_dir="tmp/doctest/$(echo -n "$file" | base64)" # make it possible to display file path later
mkdir "$temp_dir"
for start_marker_lineno in $(grep -n '^```python$' "$file" | cut -d: -f1); do
outfile="$temp_dir/$((start_marker_lineno + 1)).jou"
awk -v n=$start_marker_lineno '(/^```$/ && line > n) { stop=1 } (++line > n && !stop) { print }' "$file" > "$outfile"
# Do not test if there is no expected output/errors
if [ -z "$(generate_expected_output "$outfile")" ]; then
rm "$outfile"
fi
done
done
ntotal=0
nfail=0
cd tmp/doctest
for file in */*.jou; do
# Print file and line number, as in "doc/foo.md:123: "
# Newline is deleted to avoid warning on NetBSD 9.3, see issue #500
echo -n "$(basename "$(dirname "$file")" | tr -d '\n' | base64 -d):$(basename "$file" | cut -d'.' -f1): "
cp "$file" test.jou
if $DIFF --text -u $diff_color <(generate_expected_output test.jou | tr -d '\r') <( ("$jou" test.jou 2>&1 || true) | tr -d '\r'); then
echo "ok"
else
((nfail++)) || true
fi
((ntotal++)) || true
done
if [ $ntotal == 0 ]; then
echo "*** Error: no doctests found ***" >&2
exit 1
fi
echo ""
echo ""
echo "$((ntotal-nfail)) succeeded, $nfail failed"
if [ $nfail != 0 ]; then
exit 1
fi