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;
55 my $result = GetOptions(
56 "coverage-testing" => \$coverage_testing,
57 "display=s" => \@displays,
60 @displays = split(/,/, join(',', @displays));
61 @displays = map { s/ //g; $_ } @displays;
63 @displays = qw(:1) if @displays == 0;
65 # connect to all displays for two reasons:
66 # 1: check if the display actually works
67 # 2: keep the connection open so that i3 is not the only client. this prevents
68 # the X server from exiting (Xdummy will restart it, but not quick enough
72 for my $display (@displays) {
74 my $x = X11::XCB->new($display, $screen);
76 say STDERR "WARNING: Not using X11 display $display, could not connect";
79 push @wdisplays, $display;
83 my $config = slurp('i3-test.config');
85 # 1: get a list of all testcases
86 my @testfiles = @ARGV;
88 # if no files were passed on command line, run all tests from t/
89 @testfiles = <t/*.t> if @testfiles == 0;
91 # 2: create an output directory for this test-run
92 my $outdir = "testsuite-";
93 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
94 $outdir .= `git describe --tags`;
96 mkdir($outdir) or die "Could not create $outdir";
97 unlink("latest") if -e "latest";
98 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
102 my $num = @testfiles;
103 my $harness = TAP::Harness->new({ });
105 my $aggregator = TAP::Parser::Aggregator->new();
106 $aggregator->start();
108 # We start tests concurrently: For each display, one test gets started. Every
109 # test starts another test after completing.
110 take_job($_) for @wdisplays;
113 # Takes a test from the beginning of @testfiles and runs it.
115 # The TAP::Parser (which reads the test output) will get called as soon as
116 # there is some activity on the stdout file descriptor of the test process
117 # (using an AnyEvent->io watcher).
119 # When a test completes and @done contains $num entries, the $cv condvar gets
120 # triggered to finish testing.
125 my $test = shift @testfiles;
127 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
128 my $logpath = "$outdir/i3-log-for-" . basename($test);
130 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
132 say $fh "ipc-socket /tmp/nested-$display";
135 my $activate_cv = AnyEvent->condvar;
136 my $time_before_start = [gettimeofday];
141 unix_socket_path => "/tmp/nested-$display-activation",
143 configfile => $tmpfile,
149 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
150 say "child died. pid = $pid";
151 undef $child_watcher;
156 # Don’t bother killing i3 when we haven’t started it
157 return if $dont_start;
159 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
160 # files are not written) and fallback to killing it
161 if ($coverage_testing) {
164 say "Exiting i3 cleanly...";
165 i3("/tmp/nested-$display")->command('exit')->recv;
171 say "[$display] killing i3";
172 kill(9, $pid) or die "could not kill i3";
175 # This will be called as soon as i3 is running and answered to our
177 $activate_cv->cb(sub {
178 my $time_activating = [gettimeofday];
179 my $start_duration = tv_interval($time_before_start, $time_activating);
180 my ($status) = $activate_cv->recv;
182 say "[$display] Not starting i3, testcase does that";
184 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
187 say "[$display] Running $test with logfile $logpath";
190 open(my $spool, '>', \$output);
191 my $parser = TAP::Parser->new({
192 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -Ilib $test| ],
198 my ($stdout, $stderr) = $parser->get_select_handles;
199 for my $handle ($parser->get_select_handles) {
205 # Ignore activity on stderr (unnecessary with merge => 1,
206 # but let’s keep it in here if we want to use merge => 0
207 # for some reason in the future).
208 return if defined($stderr) and $handle == $stderr;
210 my $result = $parser->next;
211 if (defined($result)) {
212 # TODO: check if we should bail out
216 # $result is not defined, we are done parsing
217 say "[$display] $test finished";
218 close($parser->delete_spool);
219 $aggregator->add($test, $parser);
220 push @done, [ $test, $output ];
224 undef $_ for @watchers;
236 $activate_cv->send(1) if $dont_start;
244 my ($test, $output) = @$_;
245 say "output for $test:";
250 $harness->summary($aggregator);