-
Notifications
You must be signed in to change notification settings - Fork 9
/
lazytest
executable file
·111 lines (98 loc) · 2.82 KB
/
lazytest
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
#!/usr/bin/env perl
# Usage:
# $ lazytest [markdown-files...] > test.sh
# $ bash test.sh || echo 'uh oh, something failed'
#
# Looks specifically for ```bash, not ```sh. So you have the option to fence
# with just ```sh if you want syntax highlighting but no test coverage.
use strict;
use warnings;
print <<'EOF';
#!/bin/bash
lazytest_n=0
lazytest_fail=0
lazytest_case() {
echo -ne '\r\033[J'
echo -n "$1" | tr '\n' ' ' | head -c79
echo -ne '\r'
# local actual="$(eval "$1"; echo "[exit code $?]")"
# local expected="$(cat <&3; echo "[exit code $?]")"
local actual="$(eval "$1")"
local expected="$(cat <&3)"
lazytest_n=$((lazytest_n + 1))
if [[ "$actual" = "$expected" ]]; then
return 0
else
lazytest_fail=$((lazytest_fail + 1))
echo -e "\033[J\033[1;31mFAIL\033[0;0m $*"
echo -e "\033[1;31m$lazytest_file:$lazytest_line\033[0;0m"
echo -e "EXPECTED\033[1;34m"
printf %s "$expected"
echo
echo -e "\033[0;0mACTUAL\033[1;34m"
printf %s "$actual"
echo -e "\033[0;0m"
return 1
fi
}
lazytest_end() {
if ((lazytest_fail)); then
echo -e "\r\033[J\033[1;31m$lazytest_n tests, $lazytest_fail failed\033[0;0m"
exit 1
else
echo -e "\r\033[J\033[1;32m$lazytest_n tests run, all passed\033[0;0m"
exit 0
fi
}
cat <<'LAZYTEST_EOF'
EOF
sub line_continues {
local $_ = $_[0];
die "shell command appears to continue beyond closing markdown code fence"
if /^\`\`\`/;
return 1 if /\\$/;
s/\\.//g;
1 while s/"[^"]*"//g || s/'[^']*'//g || s/\`[^\`]*\`//g || s/\([^()]*\)//g;
1 & length s/[^"]//gr or 1 & length s/[^']//gr or 1 & length s/[^\`]//gr
or length(s/\(//gr) - length(s/\)//gr);
}
my $argv = @ARGV;
while (<>) {
$. = 1, $argv = @ARGV unless $argv == @ARGV;
if (/^\`\`\`lazytest$/) {
print "LAZYTEST_EOF\n";
print until ($_ = <>) =~ /^\`\`\`$/;
print "cat <<'LAZYTEST_EOF'\n";
next;
}
next unless (/^\`\`\`bash/../^\`\`\`\s*$/) && !/^\`\`\`/;
if (s/^\$\s+//) {
# We've got a shell command, so read it along with any heredocs we have.
# Assume all multiline strings are single-quoted, and that an odd number of
# single-quotes means that we're still inside a string.
print "LAZYTEST_EOF\n";
print "lazytest_file='$ARGV'\nlazytest_line=$.\n";
$_ .= <> while line_continues $_;
# At this point we've appended our heredoc to the test wrapper, so we
# should be all set. Look for previous heredocs and scan forwards until
# they end -- that way we won't break the test case for things like this:
#
# $ rev <<'EOF'
# foo
# $ bar
# bif
# EOF
# oof
# rab $
# fib
if (/\<\<'?(\w+)'?/) {
my $hd = $1;
$_ .= <> until /\n$hd\n$/;
}
print "lazytest_case '" . s/'/'\\''/gr . "' 3<<'LAZYTEST_EOF'\n";
} else {
print;
}
}
print "LAZYTEST_EOF\n";
print "lazytest_end\n";