forked from edymtt/opendetex
-
Notifications
You must be signed in to change notification settings - Fork 35
/
test.pl
executable file
·96 lines (82 loc) · 2.76 KB
/
test.pl
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
#!/usr/bin/perl -w
use strict;
use File::stat;
assert_produces_correct_output('in.tex', 'correct.txt');
assert_produces_correct_output('in.tex', 'correct.txt', '-l');
assert_produces_correct_output('noinclude.tex', 'noinclude-correct.txt', '-n');
assert_produces_correct_output('words.tex', 'words-correct.txt', '-w');
assert_produces_correct_output('words.tex', 'words-correct.txt', '-w -l');
assert_produces_correct_output('nouns.tex', 'nouns-correct.txt', '-r');
assert_produces_correct_output('with-srcloc.tex', 'with-srcloc-correct.txt', '-1');
assert_produces_correct_output('comments.tex', 'comments-correct.txt');
run_for_wrong_input("non-existent-file");
run_for_wrong_input("non-existent-file.tex");
run_for_wrong_input("non-existent-file.txt");
run_for_wrong_input("test/unterminated.tex");
run_for_input_with_error("test/unterminated-verb-pipe.tex");
run_for_input_with_error("test/unterminated-verb.tex");
run_for_input_with_error("test/unterminated-verb-eol.tex");
run_for_input_with_error("test/unterminated-verb-nofinal.tex");
fuzz();
print "Tests ok\n";
sub assert_produces_correct_output {
my ($input, $correct, $options) = @_;
$options ||= '';
my $options_desc = $options ? " ($options)" : '';
print "Checking correct output is produced for $input->$correct$options_desc...\n";
chdir 'test';
execute_cmd("../delatex $options $input > /tmp/testDelatex.txt");
my $compared = "$correct /tmp/testDelatex.txt";
my $diffResult = `diff $compared 2>&1`;
if ($diffResult ne '') {
print "Test failed:\n";
if (`which kdiff3`) {
system("kdiff3 $compared");
} elsif (!$ENV{CI} && `which vimdiff`) {
system("vimdiff $compared");
} else {
system("diff -u $compared");
}
exit(11);
}
chdir '..'
}
sub run_for_wrong_input {
my ($input) = @_;
print "Checking response for $input...\n";
execute_cmd("./delatex $input");
}
sub run_for_input_with_error {
my ($input) = @_;
print "Checking response for $input...\n";
my $res = system(get_cmd("./delatex $input"));
# https://www.perlmonks.org/?node_id=81640
die "exit code $res (" . ($res >> 8) . ")" if ($res != 1 << 8);
}
sub fuzz {
chdir 'test';
my $input = 'in.tex';
my $sb = stat($input);
for (my $i=0; $i<25; ++$i) {
my $bytes = int(rand($sb->size));
printf("Fuzz: bytes %5d/%5d of %s. ", $bytes, $sb->size, $input);
system("head -c $bytes $input > /tmp/trunc.tex");
my $res = system(get_cmd("../delatex /tmp/trunc.tex > /tmp/trunc.log"));
print "Exit code $res\n";
if ($res != 0 && $res != 1 << 8) {
die;
}
}
}
sub execute_cmd {
my ($cmd) = @_;
my $res = system(get_cmd($cmd));
die "exit code $res" if ($res != 0);
}
sub get_cmd {
my ($cmd) = @_;
if ($ARGV[0] && $ARGV[0] eq '--valgrind') {
$cmd = "valgrind --leak-check=yes --leak-check=full --error-exitcode=15 $cmd";
}
return $cmd;
}