-
Notifications
You must be signed in to change notification settings - Fork 298
/
runtest.pl
executable file
·343 lines (302 loc) · 8.91 KB
/
runtest.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl
# Author: Per Östlund
#
# This script runs a testcase, usually given to it by the runtests.pl script.
use strict;
use warnings;
use Cwd;
use Term::ANSIColor;
use File::Path qw(rmtree);
use Encode qw(decode encode);
# Get the testcase to run from the command line argument.
my $test_full = $ARGV[0];
my $no_colour = 0;
my $withxml = 0;
my $rtest_extra_args = "";
my $test_baseline = 0;
for(@ARGV){
if(/--no-colour/) {
$no_colour = 1;
}
elsif(/-with-xml/) {
$withxml = 1;
}
elsif(/-have-dwdiff/) {
$rtest_extra_args = $rtest_extra_args . " -c";
}
elsif(/^--with-omc=(.*)$/) {
$rtest_extra_args = $rtest_extra_args . " --with-omc=$1";
}
elsif(/-b/) {
$rtest_extra_args = $rtest_extra_args . " -b";
$test_baseline = 1;
}
}
#if($no_colour) {
# $rtest_extra_args = "";
#}
# Extract the directory and test name.
(my $test_dir, my $test) = $test_full =~ /(.*)\/([^\/]*)$/;
# Add a random number to the temporary directory, to avoid problems with rtest
# when we have two test cases with the same name in different directories.
my $test_id = int(rand(9999));
# Build the full path to the temporary directory to run the test in.
my $tmp_path_full = $test_dir . "/" . $test . "_temp" . $test_id;
# Makes a symbolic link to a file.
sub make_link {
my $file = shift;
# Depending on how the path is given we need to use different rules for how
# the symlink should be created.
for ($file) {
if (/\.\.\/(\w*)\/package.mo/) { symlink("../" . $1, "../" . $1); }
elsif (/\.\/(\w*)\/package.mo/) { symlink("../" . $1, $1); }
elsif (/\.\.\/([\w-]*)\//) { symlink("../" . $1, "../" . $1); }
elsif (/^(\w*)\/(.*)/) { symlink("../" . $1, $1); }
elsif (/(.*)/) { symlink("../" . $1, $1); }
else { symlink("../" . $file, $file); }
}
}
# Some tests use libraries that we need to symlink the corresponding headers for.
sub lib_to_header {
my $lib = shift;
for ($lib) {
if (/lib(\w*)\.\w*/) { return $1 . ".h"; }
elsif (/(\w*)\.lib/) { return $1 . ".h"; }
else { return ""; }
}
}
# Some tests needs some special symlinks that are hard to determine by just
# parsing the test-scripts, so this functions applies a couple of special rules.
sub make_test_specific_links {
if (-d "../ReferenceFiles") {
make_link("ReferenceFiles");
}
if (-d "../ReferenceGraphs") {
make_link("ReferenceGraphs");
}
# search for any _prof.xml and _TaskGraph.graphml and link those too
my $dir = '../';
opendir(DIR, $dir);
while (my $file = readdir(DIR)) {
# We only want files
next unless (-f "$dir/$file");
# Use a regular expression to find files ending in _prof.xml and _TaskGraph.graphml
next unless ($file =~ m/_prof\.xml$/) or ($file =~ m/_TaskGraph\.graphml$/);
make_link($file);
}
closedir(DIR);
}
sub remove_test_specific_links {
my %links = (
"ParseModel.mos" => ["ParseModel.mo"]
);
map { unlink($_) } @{$links{"ParseModel.mos"}};
}
# This functions sets up a sandbox for a tests by creating a temporary directory
# and symlinking the needed files into it.
sub enter_sandbox {
mkdir($tmp_path_full);
chdir($tmp_path_full);
make_link($test);
make_test_specific_links();
# Parse the testscript to see if it has any special requirements.
my $open_ret = open(my $in, "<", $test);
unless($open_ret) {#or die "Couldn't open $test: $!\n";
print " ";
if ($no_colour) {
print "[$test] FAILED\n";
} else {
print color 'red on_blue';
print "[$test]";
print color 'reset';
}
exit_sandbox();
exit 0;
}
my $stop_expr;
# If we are parsing a mos-file, stop when we reach 'Result:'. Otherwise, parse
# until we reach a line that's not a comment.
if(substr($test, -3) eq "mos") {
$stop_expr = "// Result:";
} else {
$stop_expr = "^[^/]";
}
# Check for a couple of keywords such as loadFile, and create the neccessary
# symlinks.
while(<$in>) {
if (/$stop_expr/) { last; }
elsif (/setup_command.*\s(.*\.c)/) { make_link($1); }
elsif (/depends: *([A-Za-z0-9_.-]*)/) { make_link($1); }
elsif (/loadFile.*\(\"linearized_model\.mo\"\)/) {}
elsif (/loadFile.*\(\"(.*)\"\)/) { make_link($1); }
elsif (/runScript.*\(\"(.*)\"\)/) { make_link($1); }
elsif (/importFMU.*\(\"(.*)\"\)/) { make_link($1); }
elsif (/partest-link: *([A-Za-z0-9.]*)/) { make_link($1); }
elsif (/system\(\"(gcc|g\+\+).*\s(\w*\.\w*)\s(\w*\.\w*)/) {
my $header = lib_to_header($2);
make_link($header);
make_link($3);
}
elsif (/system\(\"(gcc|g\+\+).*\s(\w*\.\w*)/) {
make_link($2);
}
elsif (/external.*\\\"(.*\.h)\\\"/){
make_link($1);
while(<$in>) {
if(/end (.*);/) {
make_link($1 . ".c");
last;
}
}
}
elsif (/env:\s*OPENMODELICALIBRARY\s*=\s*(.*)/) {
my $lib = $1;
$lib =~ s/((?:\.\.\/)+)/..\/$1/g;
$ENV{'OPENMODELICALIBRARY'} = $lib;
}
}
remove_test_specific_links();
}
# Exit the sandbox by going up one directory level and delete the temporary
# directory.
sub exit_sandbox {
chdir("..");
# Hack to get RunScript working.
sleep 1 if $test eq "RunScript.mos";
rmtree($test . "_temp" . $test_id);
}
sub needs_sandbox {
if ($test eq "CheckSourcesForTabs.mos") { return 0; }
if ($test eq "testCompileInteractive.mos") { return 0; }
return 1;
}
$ENV{'PATH'} = "./:" . $ENV{'PATH'};
# Some tests are hard to sandbox and don't really need to be, so skip sandboxing
# for those tests.
my $sandbox_needed = needs_sandbox();
enter_sandbox() if $sandbox_needed;
chdir($test_dir) if !$sandbox_needed;
my $fail_log = ($sandbox_needed ? "../" : "") . "$test.fail_log";
my $xml_log = ($sandbox_needed ? "../" : "") . "$test.result.xml";
# Clean up fail logs from previous runs.
unlink("$fail_log");
unlink("$xml_log") if $withxml;
# Determine the full path to rtest.
my $n = ($test_full =~ tr/\///) - ($sandbox_needed ? 0 : 1);
my $test_suit_path_rel = "../" x $n;
my $rtest = $test_suit_path_rel . "rtest $rtest_extra_args -v -nolib ";
# Run the testscript and redirect output to a logfile.
my $cmd = "$rtest $test > $test.test_log 2>&1";
system("$cmd");
# Read the logfile and see if the test succeeded or failed.
open(my $test_log, "<", "$test.test_log") or die "Couldn't open test log $test.log: $!\n";
my $exit_status = 1;
my $erroneous = 0;
my $time = 0;
my $nfailed = 1;
while(<$test_log>) {
if(/\.\.\. erroneous/) {
$erroneous = 1;
}
elsif(/== (\d) out of 1 tests failed.*time: (\d*)/) {
$nfailed = $1;
$time = $2;
}
elsif(/== Failed to set baseline.*time: (\d*)/) {
$nfailed = 1;
$time = $2;
}
elsif(/.*time: (\d*)/) {
$nfailed = 0;
$time = $1;
}
}
if (!$no_colour) {
if($nfailed =~ /0/) {
if ($test_baseline) {
print color 'blue';
} else {
print color 'green';
}
} else {
if($erroneous == 0) {
system("cp $test.test_log $fail_log");
print color 'red';
$exit_status = 0;
} else {
system("cp $test.test_log $fail_log");
print color 'magenta';
}
}
print " ";
}
if ($test_baseline) {
print "[Baselining $test:$time]";
} else {
print "[$test:$time]";
}
if ($no_colour) {
if($nfailed =~ /0/) {
print " OK\n";
} else {
if($erroneous == 0) {
system("cp $test.test_log $fail_log");
print " Failed\n";
$exit_status = 0;
} else {
system("cp $test.test_log $fail_log");
print " Erroneous\n";
}
}
} else {
print color 'reset';
}
if ($withxml) {
my $XMLOUT;
open $XMLOUT, '>', $xml_log or die "Couldn't open result.xml: $!";
binmode $XMLOUT, ":encoding(UTF-8)";
my $classname = $test_dir;
# Replace ./abc/def with abc.def
$classname =~ s,\./,,g;
$classname =~ s,/,_,g;
print $XMLOUT "<testcase classname=\"$classname\" name=\"$test\" time=\"$time\">";
if ($erroneous == 1) {
print $XMLOUT '<skipped />';
}
elsif ($exit_status == 0) {
print $XMLOUT '<failure type="Failure">Output mismatch (see stdout for details)</failure>';
print $XMLOUT '<system-out>';
open my $fh, '<', $fail_log;
my $data;
if (!$fh) {
$data = 'Unknown result';
} else {
$data = do { local $/; <$fh> };
# Convert illegal characters in the UTF-8 stream into valid UTF-8 character question mark (�)
$data = decode('UTF-8', $data, Encode::FB_DEFAULT);
$data = encode('UTF-8', $data, Encode::FB_CROAK);
$data =~ s/&/&/g;
$data =~ s/</</g;
$data =~ s/>/>/g;
$data =~ s/"/"/g;
$data =~ s/'/'/g;
# Remove non printable characters as they are not valid xml
$data =~ s/[^[:print:]\n]+//g;
}
print $XMLOUT $data;
print $XMLOUT '</system-out>';
}
print $XMLOUT "</testcase>\n";
close $XMLOUT;
}
exit_sandbox() if $sandbox_needed;
if ($exit_status == 0) {
exit 0;
} else {
if ($time < 1) {
exit 1;
} elsif ($time > 100) {
exit 100;
}
exit $time;
}