2 # vim:ts=4:sw=4:expandtab
4 # © 2010-2011 Michael Stapelberg and contributors
6 # syntax: ./complete-run.pl --display :1 --display :2
7 # to run the test suite on the X11 displays :1 and :2
8 # use 'Xdummy :1' and 'Xdummy :2' before to start two
15 # the following are modules which ship with Perl (>= 5.10):
17 use File::Basename qw(basename);
18 use File::Temp qw(tempfile tempdir);
22 use Time::HiRes qw(sleep gettimeofday tv_interval);
25 use TAP::Parser::Aggregator;
26 # these are shipped with the testsuite
29 # the following modules are not shipped with Perl
32 use AnyEvent::I3 qw(:all);
35 # We actually use AnyEvent to make sure it loads an event loop implementation.
36 # Afterwards, we overwrite SIGCHLD:
37 my $cv = AnyEvent->condvar;
39 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
40 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
41 # needs to get the exit status to determine if a test is successful).
45 # reads in a whole file
47 open my $fh, '<', shift;
52 my $coverage_testing = 0;
56 my $result = GetOptions(
57 "coverage-testing" => \$coverage_testing,
58 "valgrind" => \$valgrind,
59 "display=s" => \@displays,
62 @displays = split(/,/, join(',', @displays));
63 @displays = map { s/ //g; $_ } @displays;
65 @displays = qw(:1) if @displays == 0;
67 # connect to all displays for two reasons:
68 # 1: check if the display actually works
69 # 2: keep the connection open so that i3 is not the only client. this prevents
70 # the X server from exiting (Xdummy will restart it, but not quick enough
74 for my $display (@displays) {
76 my $x = X11::XCB->new($display, $screen);
78 say STDERR "WARNING: Not using X11 display $display, could not connect";
81 push @wdisplays, $display;
85 my $config = slurp('i3-test.config');
87 # 1: get a list of all testcases
88 my @testfiles = @ARGV;
90 # if no files were passed on command line, run all tests from t/
91 @testfiles = <t/*.t> if @testfiles == 0;
93 # 2: create an output directory for this test-run
94 my $outdir = "testsuite-";
95 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
96 $outdir .= `git describe --tags`;
98 mkdir($outdir) or die "Could not create $outdir";
99 unlink("latest") if -e "latest";
100 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
104 my $num = @testfiles;
105 my $harness = TAP::Harness->new({ });
107 my $aggregator = TAP::Parser::Aggregator->new();
108 $aggregator->start();
110 # We start tests concurrently: For each display, one test gets started. Every
111 # test starts another test after completing.
112 take_job($_) for @wdisplays;
115 # Takes a test from the beginning of @testfiles and runs it.
117 # The TAP::Parser (which reads the test output) will get called as soon as
118 # there is some activity on the stdout file descriptor of the test process
119 # (using an AnyEvent->io watcher).
121 # When a test completes and @done contains $num entries, the $cv condvar gets
122 # triggered to finish testing.
127 my $test = shift @testfiles;
129 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
130 my $logpath = "$outdir/i3-log-for-" . basename($test);
132 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
134 say $fh "ipc-socket /tmp/nested-$display";
137 my $activate_cv = AnyEvent->condvar;
138 my $time_before_start = [gettimeofday];
143 unix_socket_path => "/tmp/nested-$display-activation",
145 configfile => $tmpfile,
148 valgrind => $valgrind,
153 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
154 say "child died. pid = $pid";
155 undef $child_watcher;
160 # Don’t bother killing i3 when we haven’t started it
161 return if $dont_start;
163 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
164 # files are not written) and fallback to killing it
165 if ($coverage_testing) {
168 say "Exiting i3 cleanly...";
169 i3("/tmp/nested-$display")->command('exit')->recv;
175 say "[$display] killing i3";
176 kill(9, $pid) or die "could not kill i3";
179 # This will be called as soon as i3 is running and answered to our
181 $activate_cv->cb(sub {
182 my $time_activating = [gettimeofday];
183 my $start_duration = tv_interval($time_before_start, $time_activating);
184 my ($status) = $activate_cv->recv;
186 say "[$display] Not starting i3, testcase does that";
188 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
191 say "[$display] Running $test with logfile $logpath";
194 open(my $spool, '>', \$output);
195 my $parser = TAP::Parser->new({
196 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
202 my ($stdout, $stderr) = $parser->get_select_handles;
203 for my $handle ($parser->get_select_handles) {
209 # Ignore activity on stderr (unnecessary with merge => 1,
210 # but let’s keep it in here if we want to use merge => 0
211 # for some reason in the future).
212 return if defined($stderr) and $handle == $stderr;
214 my $result = $parser->next;
215 if (defined($result)) {
216 # TODO: check if we should bail out
220 # $result is not defined, we are done parsing
221 say "[$display] $test finished";
222 close($parser->delete_spool);
223 $aggregator->add($test, $parser);
224 push @done, [ $test, $output ];
228 undef $_ for @watchers;
240 $activate_cv->send(1) if $dont_start;
248 my ($test, $output) = @$_;
249 say "output for $test:";
254 $harness->summary($aggregator);