/
auto_run_tests.pl
executable file
·430 lines (375 loc) · 13.4 KB
/
auto_run_tests.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
& eval 'exec perl -S $0 $argv:q'
if 0;
# Executes test list files (*.lst), which contain commands with conditions
# called configurations under which the commands are run.
use strict;
use warnings;
use Env qw(ACE_ROOT DDS_ROOT PATH);
use lib "$ACE_ROOT/bin";
use lib "$DDS_ROOT/bin";
use PerlDDS::Run_Test;
use lib "$DDS_ROOT/tools/scripts/modules/";
use command_utils qw//;
use Getopt::Long;
use constant windows => $^O eq "MSWin32";
my $gh_actions = ($ENV{GITHUB_ACTIONS} // "") eq "true";
my %os_configs = (
MSWin32 => 'Win32',
darwin => 'macOS',
linux => 'Linux',
);
sub cd {
my $dir = shift;
chdir($dir) or die "auto_run_tests.pl: Error: Cannot chdir to $dir: $!";
}
my $dry_run = 0;
sub run_command {
my $test_name = shift;
return command_utils::run_command(@_,
name => $test_name,
script_name => 'auto_run_tests.pl',
dry_run => $dry_run,
verbose => $dry_run,
);
}
sub mark_test_start {
my $name = shift;
print
"\n==============================================================================\n" .
"auto_run_tests: $name\n\n";
}
sub mark_test_finish {
my $name = shift;
my $time = shift;
my $result = shift;
print "\nauto_run_tests_finished: $name Time:${time}s Result:$result\n";
}
my $stop_on_fail = 0;
sub run_test {
my $name = shift;
my $command = shift;
my $start_time = time();
my $failed = run_command($name, $command, @_);
mark_test_finish($name, time() - $start_time, $failed);
exit(1) if ($failed && $stop_on_fail);
}
my @builtin_test_lists = (
{
name => 'dcps',
file => "tests/dcps_tests.lst",
default => 1,
},
{
name => 'security',
file => "tests/security/security_tests.lst",
},
{
name => 'java',
file => "java/tests/dcps_java_tests.lst",
},
{
name => 'modeling',
file => "tools/modeling/tests/modeling_tests.lst",
},
);
sub print_usage {
my $error = shift // 1;
my $fd = $error ? *STDERR : *STDOUT;
print $fd
"auto_run_tests.pl [<options> ...] [<list_file> ...]\n" .
"auto_run_tests.pl -h | --help\n" .
"\n";
if ($error) {
print STDERR "ERROR: Use auto_run_tests.pl --help to see all the options\n";
exit(1);
}
}
sub print_help {
print_usage(0);
print
"Executes test list files (*.lst), which contain commands with conditions called\n" .
"configurations under which the commands are run.\n" .
"\n" .
"<list_file> can be a path or - to use stdin.\n" .
"\n" .
"Options:\n" .
" --help | -h Display this help\n" .
" --no-auto-config Don't set common options by default. This includes\n" .
" the RTPS and per-OS configs and the default test\n" .
" lists below.\n";
my $indent = 29;
foreach my $list (@builtin_test_lists) {
my $prefix = " --" . ($list->{default} ? "no-" : "");
print sprintf("%s%-" . ($indent - length($prefix) - 1) . "s Include %s\n",
$prefix, $list->{name}, $list->{file});
print sprintf(" " x 29 . "%sncluded by default\n",
$list->{default} ? "I" : "Not i");
}
print
" --cmake Run CMake Tests\n" .
" Not included by default\n" .
" --cmake-cmd <cmd> CMake command that is used internally\n" .
" Tests still have to be built before hand\n" .
" Default is `cmake`\n" .
" --cmake-build-dir <path> Path to the CMake tests binary directory\n" .
" Default is \$DDS_ROOT/tests/cmake/build\n" .
" --cmake-build-cfg <cfg> CMake build configuration, like the one passed to\n" .
" `cmake --config`. Mostly used by Visual Studio.\n" .
" Default is \"Debug\" on Windows, no default\n" .
" elsewhere.\n" .
" --ctest <cmd> CTest to use to run CMake Tests\n" .
" Default is `ctest`\n" .
" --ctest-args <args> Additional arguments to pass to CTest\n" .
" --python <cmd> Python command to use to run\n" .
" ctest-to-auto-run-tests.py.\n" .
" Default is `python` on Windows, `python3`\n" .
" elsewhere.\n" .
# These two are processed by PerlACE/ConfigList.pm
" -Config <cfg> Include tests with <cfg> configuration\n" .
" -Exclude <cfg> Exclude tests with <cfg> configuration\n" .
" This is parsed as a Perl regex and will always\n" .
" override -Config regardless of the order\n" .
# This one is processed by PerlACE/Process.pm
" -ExeSubDir <dir> Subdirectory for finding the executables\n" .
" --sandbox | -s <sandbox> Runs each program using a sandbox program\n" .
" --dry-run | -z Do everything except run the tests\n" .
" --show-configs Print possible values for -Config and -Excludes\n" .
" broken down by list file\n" .
" --show-all-configs Same as --show-configs, but for all list files\n" .
" --list-configs Print combined set of the configs from the list\n" .
" files\n" .
" --list-all-configs Same as --list-configs, but for all list files\n" .
" --list-tests List all the tests that would run\n" .
" --list-all-tests Same as --list-tests, but for all list files\n" .
" --stop-on-fail | -x Stop on any failure\n";
exit(0);
}
my $cmake_tests = "$DDS_ROOT/tests/cmake";
# Parse Options
my $help = 0;
my $auto_config = 1;
my $sandbox = '';
my $show_configs = 0;
my $show_all_configs = 0;
my $list_configs = 0;
my $list_all_configs = 0;
my $list_tests = 0;
my $list_all_tests = 0;
my $cmake = 0;
my $cmake_cmd = 'cmake';
my $cmake_build_dir = "$cmake_tests/build";
my $cmake_build_cfg = windows ? 'Debug' : undef;
my $ctest = 'ctest';
my $ctest_args = '';
# Python on Windows is called python from what I can see. On other platforms
# that are legacy-mindful python is Python 2, but we can rely on python3.
my $python = windows ? 'python' : 'python3';
my %opts = (
'help|h' => \$help,
'auto-config!' => \$auto_config,
'sandbox|s=s' => \$sandbox,
'dry-run|z' => \$dry_run,
'show-configs' => \$show_configs,
'show-all-configs' => \$show_all_configs,
'list-configs' => \$list_configs,
'list-all-configs' => \$list_all_configs,
'list-tests' => \$list_tests,
'list-all-tests' => \$list_all_tests,
'stop-on-fail|x' => \$stop_on_fail,
'cmake' => \$cmake,
'cmake-cmd=s' => \$cmake_cmd,
'cmake-build-dir=s' => \$cmake_build_dir,
'cmake-build-cfg=s' => \$cmake_build_cfg,
'ctest=s' => \$ctest,
'ctest-args=s' => \$ctest_args,
'python=s' => \$python,
);
foreach my $list (@builtin_test_lists) {
$list->{option} = undef;
my $opt = "$list->{name}!";
$opts{$opt} = \$list->{option};
}
Getopt::Long::Configure('bundling', 'no_auto_abbrev');
if (!GetOptions(%opts)) {
print_usage(1);
}
elsif ($help) {
print_help();
}
my $files_passed = scalar(@ARGV);
for my $list (@builtin_test_lists) {
my $default = exists($list->{default}) ? $auto_config && $list->{default} : 0;
if (!exists($list->{enabled})) {
$list->{enabled} = defined($list->{option}) ? $list->{option} :
$files_passed ? 0 : $default;
}
}
my $query_all = $show_all_configs || $list_all_configs || $list_all_tests;
$show_configs |= $show_all_configs;
$list_configs |= $list_all_configs;
$list_tests |= $list_all_tests;
my $query = $show_configs || $list_configs || $list_tests;
# Determine what test list files to use
my @file_list = ();
foreach my $list (@builtin_test_lists) {
push(@file_list, "$DDS_ROOT/$list->{file}") if ($query_all || $list->{enabled});
}
push(@file_list, @ARGV);
if ($auto_config) {
die("auto_run_tests.pl: Error: unknown perl OS: $^O") if (!exists($os_configs{$^O}));
push(@PerlACE::ConfigList::Configs, $os_configs{$^O});
push(@PerlACE::ConfigList::Configs, "RTPS");
if ($gh_actions) {
push(@PerlACE::ConfigList::Configs, 'GH_ACTIONS');
}
}
# ConfigList can't properly handle entries with duplicate commands
foreach my $test_list (@file_list) {
my $config_list = new PerlACE::ConfigList;
$config_list->load($test_list);
my %entry_count;
for my $entry (@{$config_list->{ENTRIES}}) {
if (exists($entry_count{$entry})) {
$entry_count{$entry} += 1;
}
else {
$entry_count{$entry} = 1;
}
}
for my $entry (keys(%entry_count)) {
my $count = $entry_count{$entry};
if ($count > 1) {
print STDERR ("WARNING: \"$entry\" in \"$test_list\" is duplicated $count times\n");
}
}
}
if ($show_configs) {
foreach my $test_list (@file_list) {
my $config_list = new PerlACE::ConfigList;
$config_list->load($test_list);
print "$test_list: " . $config_list->list_configs() . "\n";
}
exit(0);
}
if ($list_configs) {
my %configs = ();
foreach my $test_list (@file_list) {
my $config_list = new PerlACE::ConfigList;
$config_list->load($test_list);
for my $config (split(/ /, $config_list->list_configs())) {
$configs{$config} = 1;
}
}
for my $config (sort(keys(%configs))) {
print "$config\n";
}
exit(0);
}
if (!$list_tests) {
print "Test Lists: ", join(', ', @file_list), "\n";
print "Configs: ", join(', ', @PerlACE::ConfigList::Configs), "\n";
print "Excludes: ", join(', ', @PerlACE::ConfigList::Excludes), "\n";
}
foreach my $test_lst (@file_list) {
my $config_list = new PerlACE::ConfigList;
$config_list->load($test_lst);
# Ensures that we search for stuff in the current directory.
$PATH .= $Config::Config{path_sep} . '.';
foreach my $test ($config_list->valid_entries()) {
if ($list_tests) {
print "$test\n";
next;
}
my $directory = ".";
my $program = ".";
## Remove intermediate '.' directories to allow the
## scoreboard matrix to read things correctly
$test =~ s!/./!/!g;
if ($test =~ /(.*)\/([^\/]*)$/) {
$directory = $1;
$program = $2;
}
else {
$program = $test;
}
mark_test_start($test);
cd("$DDS_ROOT/$directory");
my $subdir = $PerlACE::Process::ExeSubDir;
my $progNoArgs = $program;
if ($program =~ /(.*?) (.*)/) {
$progNoArgs = $1;
if (! -e $progNoArgs) {
print STDERR "auto_run_tests.pl: Error: $directory/$1 does not exist\n";
next;
}
}
else {
my $cmd = $program;
$cmd = $subdir.$cmd if ($program !~ /\.pl$/);
if ((! -e $cmd) && (! -e "$cmd.exe")) {
print STDERR "auto_run_tests.pl: Error: $directory/$cmd does not exist\n";
next;
}
}
### Generate the -ExeSubDir and -Config options
my $inherited_options = " -ExeSubDir $subdir ";
foreach my $config ($config_list->my_config_list()) {
$inherited_options .= " -Config $config ";
}
my $cmd = '';
$program = "perl $program" if ($progNoArgs =~ /\.pl$/);
if ($sandbox) {
$cmd = "$sandbox \"$program $inherited_options\"";
}
else {
$cmd = $program.$inherited_options;
$cmd = $subdir.$cmd if ($progNoArgs !~ /\.pl$/);
}
run_test($test, $cmd);
}
}
if ($cmake) {
my $fake_name = "Run CMake Tests";
if (!$list_tests) {
mark_test_start($fake_name);
}
cd($cmake_build_dir);
my $process_name;
my $process_func;
my @process_cmd = (
$python,
"$cmake_tests/ctest-to-auto-run-tests.py",
'--cmake', $cmake_cmd,
$cmake_tests, '.'
);
my $art_output = 'art-output';
if ($list_tests) {
$process_name = "List CMake Tests";
$process_func = \&run_command;
push(@process_cmd, '--list');
}
else {
my @run_test_cmd = ($ctest, '--no-compress-output', '-T', 'Test');
if ($ctest_args) {
push(@run_test_cmd, $ctest_args);
}
if ($ctest_args !~ /--build-config/ && defined($cmake_build_cfg)) {
push(@run_test_cmd, "--build-config", $cmake_build_cfg);
}
run_test($fake_name, \@run_test_cmd, verbose => 1);
$process_name = "Process CMake Test Results";
$process_func = \&run_test;
push(@process_cmd, '--art-output', $art_output);
mark_test_start($process_name);
}
$process_func->($process_name, \@process_cmd);
if (!$list_tests) {
open(my $fh, $art_output) or die("Couldn't open $art_output: $!");
while(<$fh>){
print($_);
}
close($fh);
}
}
# vim: expandtab:ts=4:sw=4