2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
8 # the following are modules which ship with Perl (>= 5.10):
11 use File::Temp qw(tempfile tempdir);
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
19 # these are shipped with the testsuite
24 # the following modules are not shipped with Perl
28 use AnyEvent::I3 qw(:all);
29 use X11::XCB::Connection;
30 use JSON::XS; # AnyEvent::I3 depends on it, too.
32 # Close superfluous file descriptors which were passed by running in a VIM
33 # subshell or situations like that.
34 AnyEvent::Util::close_all_fds_except(0, 1, 2);
36 # convinience wrapper to write to the log file
38 sub Log { say $log "@_" }
42 # Number of tests to run in parallel. Important to know how many Xdummy
43 # instances we need to start (unless @displays are given). Defaults to
54 my $keep_xdummy_output = 0;
56 my $result = GetOptions(
57 "coverage-testing" => \$options{coverage},
58 "keep-xdummy-output" => \$keep_xdummy_output,
59 "valgrind" => \$options{valgrind},
60 "strace" => \$options{strace},
61 "xtrace" => \$options{xtrace},
62 "display=s" => \@displays,
63 "parallel=i" => \$parallel,
67 pod2usage(-verbose => 2, -exitcode => 0) if $help;
69 # Check for missing executables
73 ../i3-config-wizard/i3-config-wizard
74 ../i3-dump-log/i3-dump-log
77 ../i3-nagbar/i3-nagbar
80 foreach my $binary (@binaries) {
81 die "$binary executable not found" unless -e $binary;
82 die "$binary is not an executable" unless -x $binary;
85 @displays = split(/,/, join(',', @displays));
86 @displays = map { s/ //g; $_ } @displays;
88 # 2: get a list of all testcases
89 my @testfiles = @ARGV;
91 # if no files were passed on command line, run all tests from t/
92 @testfiles = <t/*.t> if @testfiles == 0;
94 my $numtests = scalar @testfiles;
96 # No displays specified, let’s start some Xdummy instances.
98 @displays = start_xdummy($parallel, $numtests, $keep_xdummy_output);
101 # 1: create an output directory for this test-run
102 my $outdir = "testsuite-";
103 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
104 $outdir .= `git describe --tags`;
106 mkdir($outdir) or die "Could not create $outdir";
107 unlink("latest") if -e "latest";
108 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
111 # connect to all displays for two reasons:
112 # 1: check if the display actually works
113 # 2: keep the connection open so that i3 is not the only client. this prevents
114 # the X server from exiting (Xdummy will restart it, but not quick enough
117 for my $display (@displays) {
119 my $x = X11::XCB::Connection->new(display => $display);
121 die "Could not connect to display $display\n";
123 # start a TestWorker for each display
124 push @single_worker, worker($display, $x, $outdir, \%options);
128 # Read previous timing information, if available. We will be able to roughly
129 # predict the test duration and schedule a good order for the tests.
130 my $timingsjson = StartXDummy::slurp('.last_run_timings.json');
131 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
133 # Re-order the files so that those which took the longest time in the previous
134 # run will be started at the beginning to not delay the whole run longer than
136 @testfiles = map { $_->[0] }
137 sort { $b->[1] <=> $a->[1] }
138 map { [$_, $timings{$_} // 999] } @testfiles;
140 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
141 if exists($timings{GLOBAL});
143 # Forget the old timings, we don’t necessarily run the same set of tests as
144 # before. Otherwise we would end up with left-overs.
145 %timings = (GLOBAL => time());
147 my $logfile = "$outdir/complete-run.log";
148 open $log, '>', $logfile or die "Could not create '$logfile': $!";
150 say "Writing logfile to '$logfile'...";
154 my $num = @testfiles;
155 my $harness = TAP::Harness->new({ });
157 my $aggregator = TAP::Parser::Aggregator->new();
158 $aggregator->start();
160 status_init(displays => \@displays, tests => $num);
162 my $single_cv = AE::cv;
164 # We start tests concurrently: For each display, one test gets started. Every
165 # test starts another test after completing.
166 for (@single_worker) {
168 take_job($_, $single_cv, \@testfiles);
175 # print empty lines to seperate failed tests from statuslines
179 my ($test, $output) = @$_;
180 say "no output for $test" unless $output;
181 Log "output for $test:";
183 # print error messages of failed tests
184 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
188 $harness->summary($aggregator);
192 # 5: Save the timings for better scheduling/prediction next run.
193 $timings{GLOBAL} = time() - $timings{GLOBAL};
194 open(my $fh, '>', '.last_run_timings.json');
195 print $fh encode_json(\%timings);
198 # 6: Print the slowest test files.
199 my @slowest = map { $_->[0] }
200 sort { $b->[1] <=> $a->[1] }
201 map { [$_, $timings{$_}] }
202 grep { !/^GLOBAL$/ } keys %timings;
204 say 'The slowest tests are:';
205 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
206 for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
208 # When we are running precisely one test, print the output. Makes developing
209 # with a single testcase easier.
210 if ($numtests == 1) {
213 say StartXDummy::slurp($logfile);
221 # Takes a test from the beginning of @testfiles and runs it.
223 # The TAP::Parser (which reads the test output) will get called as soon as
224 # there is some activity on the stdout file descriptor of the test process
225 # (using an AnyEvent->io watcher).
227 # When a test completes and @done contains $num entries, the $cv condvar gets
228 # triggered to finish testing.
231 my ($worker, $cv, $tests) = @_;
233 my $test = shift @$tests
236 my $display = $worker->{display};
238 Log status($display, "$test: starting");
239 $timings{$test} = time();
240 worker_next($worker, $test);
242 # create a TAP::Parser with an in-memory fh
244 my $parser = TAP::Parser->new({
245 source => do { open(my $fh, '<', \$output); $fh },
248 my $ipc = $worker->{ipc};
255 state $tests_completed = 0;
258 sysread($ipc, my $buf, 4096) or die "sysread: $!";
261 $buf = $partial . $buf;
265 # make sure we feed TAP::Parser complete lines so it doesn't blow up
266 if (substr($buf, -1, 1) ne "\n") {
267 my $nl = rindex($buf, "\n");
273 # strip partial from buffer
274 $partial = substr($buf, $nl + 1, '');
277 # count lines before stripping eof-marker otherwise we might
278 # end up with for (1 .. 0) { } which would effectivly skip the loop
279 my $lines = $buf =~ tr/\n//;
280 my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
285 my $result = $parser->next;
286 next unless defined($result);
287 if ($result->is_test) {
289 status($display, "$test: [$tests_completed/??] ");
290 } elsif ($result->is_bailout) {
291 Log status($display, "$test: BAILOUT");
292 status_completed(scalar @done);
294 say "test $test bailed out: " . $result->explanation;
299 return unless $t_eof;
301 Log status($display, "$test: finished");
302 $timings{$test} = time() - $timings{$test};
303 status_completed(scalar @done);
305 $aggregator->add($test, $parser);
306 push @done, [ $test, $output ];
309 take_job($worker, $cv, $tests);
315 $_->() for our @CLEANUP;
319 # must be in a begin block because we C<exit 0> above
322 require Carp; Carp::cluck("Caught SIG$_[0]\n");
324 } for qw(INT TERM QUIT KILL PIPE)
331 complete-run.pl - Run the i3 testsuite
335 complete-run.pl [files...]
339 To run the whole testsuite on a reasonable number of Xdummy instances (your
340 running X11 will not be touched), run:
343 To run only a specific test (useful when developing a new feature), run:
344 ./complete-run t/100-fullscreen.t
352 Specifies which X11 display should be used. Can be specified multiple times and
353 will parallelize the tests:
355 # Run tests on the second X server
356 ./complete-run.pl -d :1
358 # Run four tests in parallel on some Xdummy servers
359 ./complete-run.pl -d :1,:2,:3,:4
361 Note that it is not necessary to specify this anymore. If omitted,
362 complete-run.pl will start (num_cores * 2) Xdummy instances.
366 Runs i3 under valgrind to find memory problems. The output will be available in
367 C<latest/valgrind-for-$test.log>.
371 Runs i3 under strace to trace system calls. The output will be available in
372 C<latest/strace-for-$test.log>.
376 Runs i3 under xtrace to trace X11 requests/replies. The output will be
377 available in C<latest/xtrace-for-$test.log>.
379 =item B<--coverage-testing>
381 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
385 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
386 instances for some reason).
388 # Run all tests on a single Xdummy instance
389 ./complete-run.pl -p 1