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):
18 use File::Basename qw(basename);
19 use File::Temp qw(tempfile tempdir);
23 use Time::HiRes qw(sleep gettimeofday tv_interval);
26 use TAP::Parser::Aggregator;
27 # these are shipped with the testsuite
30 # the following modules are not shipped with Perl
33 use AnyEvent::I3 qw(:all);
36 # We actually use AnyEvent to make sure it loads an event loop implementation.
37 # Afterwards, we overwrite SIGCHLD:
38 my $cv = AnyEvent->condvar;
40 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
41 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
42 # needs to get the exit status to determine if a test is successful).
46 # reads in a whole file
48 open my $fh, '<', shift;
53 my $coverage_testing = 0;
57 my $result = GetOptions(
58 "coverage-testing" => \$coverage_testing,
59 "valgrind" => \$valgrind,
60 "display=s" => \@displays,
63 @displays = split(/,/, join(',', @displays));
64 @displays = map { s/ //g; $_ } @displays;
66 @displays = qw(:1) if @displays == 0;
68 # connect to all displays for two reasons:
69 # 1: check if the display actually works
70 # 2: keep the connection open so that i3 is not the only client. this prevents
71 # the X server from exiting (Xdummy will restart it, but not quick enough
75 for my $display (@displays) {
77 my $x = X11::XCB->new($display, $screen);
79 say STDERR "WARNING: Not using X11 display $display, could not connect";
82 push @wdisplays, $display;
86 my $config = slurp('i3-test.config');
88 # 1: 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 # 2: create an output directory for this test-run
95 my $outdir = "testsuite-";
96 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
97 $outdir .= `git describe --tags`;
99 mkdir($outdir) or die "Could not create $outdir";
100 unlink("latest") if -e "latest";
101 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
105 my $num = @testfiles;
106 my $harness = TAP::Harness->new({ });
108 my $aggregator = TAP::Parser::Aggregator->new();
109 $aggregator->start();
111 # We start tests concurrently: For each display, one test gets started. Every
112 # test starts another test after completing.
113 take_job($_) for @wdisplays;
116 # Takes a test from the beginning of @testfiles and runs it.
118 # The TAP::Parser (which reads the test output) will get called as soon as
119 # there is some activity on the stdout file descriptor of the test process
120 # (using an AnyEvent->io watcher).
122 # When a test completes and @done contains $num entries, the $cv condvar gets
123 # triggered to finish testing.
128 my $test = shift @testfiles;
130 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
131 my $logpath = "$outdir/i3-log-for-" . basename($test);
133 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
135 say $fh "ipc-socket /tmp/nested-$display";
138 my $activate_cv = AnyEvent->condvar;
139 my $time_before_start = [gettimeofday];
144 unix_socket_path => "/tmp/nested-$display-activation",
146 configfile => $tmpfile,
149 valgrind => $valgrind,
154 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
155 say "child died. pid = $pid";
156 undef $child_watcher;
161 # Don’t bother killing i3 when we haven’t started it
162 return if $dont_start;
164 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
165 # files are not written) and fallback to killing it
166 if ($coverage_testing) {
169 say "Exiting i3 cleanly...";
170 i3("/tmp/nested-$display")->command('exit')->recv;
176 say "[$display] killing i3";
177 kill(9, $pid) or die "could not kill i3";
180 # This will be called as soon as i3 is running and answered to our
182 $activate_cv->cb(sub {
183 my $time_activating = [gettimeofday];
184 my $start_duration = tv_interval($time_before_start, $time_activating);
185 my ($status) = $activate_cv->recv;
187 say "[$display] Not starting i3, testcase does that";
189 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
192 say "[$display] Running $test with logfile $logpath";
195 open(my $spool, '>', \$output);
196 my $parser = TAP::Parser->new({
197 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
203 my ($stdout, $stderr) = $parser->get_select_handles;
204 for my $handle ($parser->get_select_handles) {
210 # Ignore activity on stderr (unnecessary with merge => 1,
211 # but let’s keep it in here if we want to use merge => 0
212 # for some reason in the future).
213 return if defined($stderr) and $handle == $stderr;
215 my $result = $parser->next;
216 if (defined($result)) {
217 # TODO: check if we should bail out
221 # $result is not defined, we are done parsing
222 say "[$display] $test finished";
223 close($parser->delete_spool);
224 $aggregator->add($test, $parser);
225 push @done, [ $test, $output ];
229 undef $_ for @watchers;
241 $activate_cv->send(1) if $dont_start;
249 my ($test, $output) = @$_;
250 say "output for $test:";
255 $harness->summary($aggregator);