2 # vim:ts=4:sw=4:expandtab
3 # © 2010 Michael Stapelberg and contributors
9 # 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);
26 # fileparse()[1] contains the directory portion of the specified path.
27 # See File::Basename(3p) for more details.
28 $dirname = (fileparse(abs_path($0)))[1];
31 # these are shipped with the testsuite
32 use lib $dirname . 'lib';
33 use i3test::Util qw(slurp);
37 # the following modules are not shipped with Perl
41 use AnyEvent::I3 qw(:all);
42 use X11::XCB::Connection;
43 use JSON::XS; # AnyEvent::I3 depends on it, too.
45 binmode STDOUT, ':utf8';
46 binmode STDERR, ':utf8';
48 # Close superfluous file descriptors which were passed by running in a VIM
49 # subshell or situations like that.
50 AnyEvent::Util::close_all_fds_except(0, 1, 2);
52 # convinience wrapper to write to the log file
54 sub Log { say $log "@_" }
58 # Number of tests to run in parallel. Important to know how many Xephyr
59 # instances we need to start (unless @displays are given). Defaults to
70 my $keep_xserver_output = 0;
72 my $result = GetOptions(
73 "coverage-testing" => \$options{coverage},
74 "keep-xserver-output" => \$keep_xserver_output,
75 "valgrind" => \$options{valgrind},
76 "strace" => \$options{strace},
77 "xtrace" => \$options{xtrace},
78 "display=s" => \@displays,
79 "parallel=i" => \$parallel,
83 pod2usage(-verbose => 2, -exitcode => 0) if $help;
85 chdir $dirname or die "Could not chdir into $dirname";
87 # Check for missing executables
91 ../i3-config-wizard/i3-config-wizard
92 ../i3-dump-log/i3-dump-log
95 ../i3-nagbar/i3-nagbar
98 foreach my $binary (@binaries) {
99 die "$binary executable not found, did you run “make”?" unless -e $binary;
100 die "$binary is not an executable" unless -x $binary;
103 if ($options{coverage}) {
104 qx(command -v lcov &> /dev/null);
105 die "Cannot find lcov needed for coverage testing." if $?;
106 qx(command -v genhtml &> /dev/null);
107 die "Cannot find genhtml needed for coverage testing." if $?;
109 # clean out the counters that may be left over from previous tests.
110 qx(lcov -d ../ --zerocounters &> /dev/null);
113 qx(Xephyr -help 2>&1);
114 die "Xephyr was not found in your path. Please install Xephyr (xserver-xephyr on Debian)." if $?;
116 @displays = split(/,/, join(',', @displays));
117 @displays = map { s/ //g; $_ } @displays;
119 # 2: get a list of all testcases
120 my @testfiles = @ARGV;
122 # if no files were passed on command line, run all tests from t/
123 @testfiles = <t/*.t> if @testfiles == 0;
125 my $numtests = scalar @testfiles;
127 # No displays specified, let’s start some Xephyr instances.
128 if (@displays == 0) {
129 @displays = start_xserver($parallel, $numtests, $keep_xserver_output);
132 # 1: create an output directory for this test-run
133 my $outdir = "testsuite-";
134 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
135 $outdir .= `git describe --tags`;
137 mkdir($outdir) or die "Could not create $outdir";
138 unlink("latest") if -l "latest";
139 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
142 # connect to all displays for two reasons:
143 # 1: check if the display actually works
144 # 2: keep the connection open so that i3 is not the only client. this prevents
145 # the X server from exiting
147 for my $display (@displays) {
149 my $x = X11::XCB::Connection->new(display => $display);
151 die "Could not connect to display $display\n";
153 # start a TestWorker for each display
154 push @single_worker, worker($display, $x, $outdir, \%options);
158 # Read previous timing information, if available. We will be able to roughly
159 # predict the test duration and schedule a good order for the tests.
160 my $timingsjson = slurp('.last_run_timings.json') if -e '.last_run_timings.json';
161 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
163 # Re-order the files so that those which took the longest time in the previous
164 # run will be started at the beginning to not delay the whole run longer than
166 @testfiles = map { $_->[0] }
167 sort { $b->[1] <=> $a->[1] }
168 map { [$_, $timings{$_} // 999] } @testfiles;
170 # Run 000-load-deps.t first to bail out early when dependencies are missing.
171 my $loadtest = "t/000-load-deps.t";
172 if ((scalar grep { $_ eq $loadtest } @testfiles) > 0) {
173 @testfiles = ($loadtest, grep { $_ ne $loadtest } @testfiles);
176 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
177 if exists($timings{GLOBAL});
179 # Forget the old timings, we don’t necessarily run the same set of tests as
180 # before. Otherwise we would end up with left-overs.
181 %timings = (GLOBAL => time());
183 my $logfile = "$outdir/complete-run.log";
184 open $log, '>', $logfile or die "Could not create '$logfile': $!";
186 say "Writing logfile to '$logfile'...";
190 my $num = @testfiles;
191 my $harness = TAP::Harness->new({ });
193 my $aggregator = TAP::Parser::Aggregator->new();
194 $aggregator->start();
196 status_init(displays => \@displays, tests => $num);
198 my $single_cv = AE::cv;
200 # We start tests concurrently: For each display, one test gets started. Every
201 # test starts another test after completing.
202 for (@single_worker) {
204 take_job($_, $single_cv, \@testfiles);
211 # print empty lines to seperate failed tests from statuslines
215 my ($test, $output) = @$_;
216 say "no output for $test" unless $output;
217 Log "output for $test:";
219 # print error messages of failed tests
220 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
224 $harness->summary($aggregator);
228 # 5: Save the timings for better scheduling/prediction next run.
229 $timings{GLOBAL} = time() - $timings{GLOBAL};
230 open(my $fh, '>', '.last_run_timings.json');
231 print $fh encode_json(\%timings);
234 # 6: Print the slowest test files.
235 my @slowest = map { $_->[0] }
236 sort { $b->[1] <=> $a->[1] }
237 map { [$_, $timings{$_}] }
238 grep { !/^GLOBAL$/ } keys %timings;
240 say 'The slowest tests are:';
241 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
242 for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
244 # When we are running precisely one test, print the output. Makes developing
245 # with a single testcase easier.
246 if ($numtests == 1) {
254 if ($options{coverage}) {
255 print("\nGenerating test coverage report...\n");
256 qx(lcov -d ../ -b ../ --capture -o latest/i3-coverage.info);
257 qx(genhtml -o latest/i3-coverage latest/i3-coverage.info);
259 print("Could not generate test coverage html. Did you compile i3 with test coverage support?\n");
261 print("Test coverage report generated in latest/i3-coverage\n");
265 exit ($aggregator->failed > 0);
268 # Takes a test from the beginning of @testfiles and runs it.
270 # The TAP::Parser (which reads the test output) will get called as soon as
271 # there is some activity on the stdout file descriptor of the test process
272 # (using an AnyEvent->io watcher).
274 # When a test completes and @done contains $num entries, the $cv condvar gets
275 # triggered to finish testing.
278 my ($worker, $cv, $tests) = @_;
280 my $test = shift @$tests
283 my $display = $worker->{display};
285 Log status($display, "$test: starting");
286 $timings{$test} = time();
287 worker_next($worker, $test);
289 # create a TAP::Parser with an in-memory fh
291 my $parser = TAP::Parser->new({
292 source => do { open(my $fh, '<', \$output); $fh },
295 my $ipc = $worker->{ipc};
302 state $tests_completed = 0;
305 sysread($ipc, my $buf, 4096) or die "sysread: $!";
308 $buf = $partial . $buf;
312 # make sure we feed TAP::Parser complete lines so it doesn't blow up
313 if (substr($buf, -1, 1) ne "\n") {
314 my $nl = rindex($buf, "\n");
320 # strip partial from buffer
321 $partial = substr($buf, $nl + 1, '');
324 # count lines before stripping eof-marker otherwise we might
325 # end up with for (1 .. 0) { } which would effectivly skip the loop
326 my $lines = $buf =~ tr/\n//;
327 my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
332 my $result = $parser->next;
333 next unless defined($result);
334 if ($result->is_test) {
336 status($display, "$test: [$tests_completed/??] ");
337 } elsif ($result->is_bailout) {
338 Log status($display, "$test: BAILOUT");
339 status_completed(scalar @done);
341 say "test $test bailed out: " . $result->explanation;
346 return unless $t_eof;
348 Log status($display, "$test: finished");
349 $timings{$test} = time() - $timings{$test};
350 status_completed(scalar @done);
352 $aggregator->add($test, $parser);
353 push @done, [ $test, $output ];
356 take_job($worker, $cv, $tests);
363 $_->() for our @CLEANUP;
367 # must be in a begin block because we C<exit 0> above
370 require Carp; Carp::cluck("Caught SIG$_[0]\n");
372 } for qw(INT TERM QUIT KILL PIPE)
379 complete-run.pl - Run the i3 testsuite
383 complete-run.pl [files...]
387 To run the whole testsuite on a reasonable number of Xephyr instances (your
388 running X11 will not be touched), run:
391 To run only a specific test (useful when developing a new feature), run:
392 ./complete-run t/100-fullscreen.t
400 Specifies which X11 display should be used. Can be specified multiple times and
401 will parallelize the tests:
403 # Run tests on the second X server
404 ./complete-run.pl -d :1
406 # Run four tests in parallel on some Xephyr servers
407 ./complete-run.pl -d :1,:2,:3,:4
409 Note that it is not necessary to specify this anymore. If omitted,
410 complete-run.pl will start (num_cores * 2) Xephyr instances.
414 Runs i3 under valgrind to find memory problems. The output will be available in
415 C<latest/valgrind-for-$test.log>.
419 Runs i3 under strace to trace system calls. The output will be available in
420 C<latest/strace-for-$test.log>.
424 Runs i3 under xtrace to trace X11 requests/replies. The output will be
425 available in C<latest/xtrace-for-$test.log>.
427 =item B<--coverage-testing>
429 Generates a test coverage report at C<latest/i3-coverage>. Exits i3 cleanly
430 during tests (instead of kill -9) to make coverage testing work properly.
434 Number of Xephyr instances to start (if you don't want to start num_cores * 2
435 instances for some reason).
437 # Run all tests on a single Xephyr instance
438 ./complete-run.pl -p 1