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::Basename qw(basename);
12 use File::Temp qw(tempfile tempdir);
16 use Time::HiRes qw(sleep gettimeofday tv_interval);
19 use TAP::Parser::Aggregator;
20 # these are shipped with the testsuite
25 # the following modules are not shipped with Perl
28 use AnyEvent::I3 qw(:all);
31 # We actually use AnyEvent to make sure it loads an event loop implementation.
32 # Afterwards, we overwrite SIGCHLD:
33 my $cv = AnyEvent->condvar;
35 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
36 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
37 # needs to get the exit status to determine if a test is successful).
41 # reads in a whole file
43 open(my $fh, '<', shift);
48 # convinience wrapper to write to the log file
50 sub Log { say $log "@_" }
52 my $coverage_testing = 0;
55 # Number of tests to run in parallel. Important to know how many Xdummy
56 # instances we need to start (unless @displays are given). Defaults to
62 my $result = GetOptions(
63 "coverage-testing" => \$coverage_testing,
64 "valgrind" => \$valgrind,
65 "display=s" => \@displays,
66 "parallel=i" => \$parallel,
70 pod2usage(-verbose => 2, -exitcode => 0) if $help;
72 @displays = split(/,/, join(',', @displays));
73 @displays = map { s/ //g; $_ } @displays;
75 # No displays specified, let’s start some Xdummy instances.
77 my ($displays, $pids) = start_xdummy($parallel);
78 @displays = @$displays;
82 # connect to all displays for two reasons:
83 # 1: check if the display actually works
84 # 2: keep the connection open so that i3 is not the only client. this prevents
85 # the X server from exiting (Xdummy will restart it, but not quick enough
89 for my $display (@displays) {
91 my $x = X11::XCB->new($display, $screen);
93 Log "WARNING: Not using X11 display $display, could not connect";
96 push @wdisplays, $display;
100 die "No usable displays found" if @wdisplays == 0;
102 my $config = slurp('i3-test.config');
104 # 1: get a list of all testcases
105 my @testfiles = @ARGV;
107 # if no files were passed on command line, run all tests from t/
108 @testfiles = <t/*.t> if @testfiles == 0;
110 # 2: create an output directory for this test-run
111 my $outdir = "testsuite-";
112 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
113 $outdir .= `git describe --tags`;
115 mkdir($outdir) or die "Could not create $outdir";
116 unlink("latest") if -e "latest";
117 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
119 my $logfile = "$outdir/complete-run.log";
120 open $log, '>', $logfile or die "Could not create '$logfile': $!";
121 say "Writing logfile to '$logfile'...";
125 my $num = @testfiles;
126 my $harness = TAP::Harness->new({ });
128 my $aggregator = TAP::Parser::Aggregator->new();
129 $aggregator->start();
131 status_init(displays => \@wdisplays, tests => $num);
133 # We start tests concurrently: For each display, one test gets started. Every
134 # test starts another test after completing.
135 take_job($_) for @wdisplays;
138 # Takes a test from the beginning of @testfiles and runs it.
140 # The TAP::Parser (which reads the test output) will get called as soon as
141 # there is some activity on the stdout file descriptor of the test process
142 # (using an AnyEvent->io watcher).
144 # When a test completes and @done contains $num entries, the $cv condvar gets
145 # triggered to finish testing.
150 my $test = shift @testfiles;
153 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
154 my $basename = basename($test);
155 my $logpath = "$outdir/i3-log-for-$basename";
157 my ($fh, $tmpfile) = tempfile("i3-cfg-for-$basename.XXXXXX", UNLINK => 1);
159 say $fh "ipc-socket /tmp/nested-$display";
162 my $activate_cv = AnyEvent->condvar;
163 my $time_before_start = [gettimeofday];
167 $activate_cv->send(1);
170 unix_socket_path => "/tmp/nested-$display-activation",
172 configfile => $tmpfile,
175 valgrind => $valgrind,
180 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
181 Log status($display, "child died. pid = $pid");
182 undef $child_watcher;
187 my $kill_cv = AnyEvent->condvar;
189 # Don’t bother killing i3 when we haven’t started it
195 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
196 # files are not written) and fallback to killing it
197 if ($coverage_testing || $valgrind) {
199 Log status($display, 'Exiting i3 cleanly...');
200 my $i3 = i3("/tmp/nested-$display");
201 $i3->connect->cb(sub {
203 # Could not connect to i3, just kill -9 it
204 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
207 # Connected. Now send exit and continue once that’s acked.
208 $i3->command('exit')->cb(sub {
214 Log status($display, 'killing i3');
216 # No coverage testing or valgrind? Just kill -9 i3.
217 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
224 # This will be called as soon as i3 is running and answered to our
226 $activate_cv->cb(sub {
227 my $time_activating = [gettimeofday];
228 my $start_duration = tv_interval($time_before_start, $time_activating);
229 my ($status) = $activate_cv->recv;
231 Log status($display, 'Not starting i3, testcase does that');
233 my $duration = sprintf("%.2f", $start_duration);
234 Log status($display, "i3 startup: took $duration sec, status = $status");
237 Log status($display, "Starting $test");
240 open(my $spool, '>', \$output);
241 my $parser = TAP::Parser->new({
242 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
250 my ($stdout, $stderr) = $parser->get_select_handles;
251 for my $handle ($parser->get_select_handles) {
257 # Ignore activity on stderr (unnecessary with merge => 1,
258 # but let’s keep it in here if we want to use merge => 0
259 # for some reason in the future).
260 return if defined($stderr) and $handle == $stderr;
262 my $result = $parser->next;
263 if (defined($result)) {
265 status($display, "Running $test: [$tests_completed/??]");
266 # TODO: check if we should bail out
270 # $result is not defined, we are done parsing
271 Log status($display, "$test finished");
272 close($parser->delete_spool);
273 $aggregator->add($test, $parser);
274 push @done, [ $test, $output ];
276 status_completed(scalar @done);
278 my $exitcv = $kill_i3->();
281 undef $_ for @watchers;
299 # print empty lines to seperate failed tests from statuslines
303 my ($test, $output) = @$_;
304 Log "output for $test:";
306 # print error messages of failed tests
307 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
311 $harness->summary($aggregator);
315 kill(15, $_) for @childpids;
321 complete-run.pl - Run the i3 testsuite
325 complete-run.pl [files...]
329 To run the whole testsuite on a reasonable number of Xdummy instances (your
330 running X11 will not be touched), run:
333 To run only a specific test (useful when developing a new feature), run:
334 ./complete-run t/100-fullscreen.t
342 Specifies which X11 display should be used. Can be specified multiple times and
343 will parallelize the tests:
345 # Run tests on the second X server
346 ./complete-run.pl -d :1
348 # Run four tests in parallel on some Xdummy servers
349 ./complete-run.pl -d :1,:2,:3,:4
351 Note that it is not necessary to specify this anymore. If omitted,
352 complete-run.pl will start (num_cores * 2) Xdummy instances.
356 Runs i3 under valgrind to find memory problems. The output will be available in
357 C<latest/valgrind.log>.
359 =item B<--coverage-testing>
361 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
365 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
366 instances for some reason).
368 # Run all tests on a single Xdummy instance
369 ./complete-run.pl -p 1