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
33 use AnyEvent::I3 qw(:all);
34 use Try::Tiny; # not in core
37 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
38 # XXX: we could maybe also use a different loop than the default loop in EV?
42 # reads in a whole file
44 open my $fh, '<', shift;
49 my $coverage_testing = 0;
52 my $result = GetOptions(
53 "coverage-testing" => \$coverage_testing,
54 "display=s" => \@displays,
57 @displays = split(/,/, join(',', @displays));
58 @displays = map { s/ //g; $_ } @displays;
60 @displays = qw(:1) if @displays == 0;
62 # connect to all displays for two reasons:
63 # 1: check if the display actually works
64 # 2: keep the connection open so that i3 is not the only client. this prevents
65 # the X server from exiting (Xdummy will restart it, but not quick enough
69 for my $display (@displays) {
71 my $x = X11::XCB->new($display, $screen);
73 say STDERR "WARNING: Not using X11 display $display, could not connect";
76 push @wdisplays, $display;
80 my $config = slurp('i3-test.config');
82 # 1: get a list of all testcases
83 my @testfiles = @ARGV;
85 # if no files were passed on command line, run all tests from t/
86 @testfiles = <t/*.t> if @testfiles == 0;
88 # 2: create an output directory for this test-run
89 my $outdir = "testsuite-";
90 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
91 $outdir .= `git describe --tags`;
93 mkdir($outdir) or die "Could not create $outdir";
94 unlink("latest") if -e "latest";
95 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
100 my $harness = TAP::Harness->new({ });
102 my $aggregator = TAP::Parser::Aggregator->new();
103 $aggregator->start();
105 my $cv = AnyEvent->condvar;
107 # We start tests concurrently: For each display, one test gets started. Every
108 # test starts another test after completing.
109 take_job($_) for @wdisplays;
112 # Takes a test from the beginning of @testfiles and runs it.
114 # The TAP::Parser (which reads the test output) will get called as soon as
115 # there is some activity on the stdout file descriptor of the test process
116 # (using an AnyEvent->io watcher).
118 # When a test completes and @done contains $num entries, the $cv condvar gets
119 # triggered to finish testing.
124 my $test = shift @testfiles;
126 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
127 my $logpath = "$outdir/i3-log-for-" . basename($test);
129 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
131 say $fh "ipc-socket /tmp/nested-$display";
134 my $activate_cv = AnyEvent->condvar;
135 my $time_before_start = [gettimeofday];
140 unix_socket_path => "/tmp/nested-$display-activation",
142 configfile => $tmpfile,
148 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
149 say "child died. pid = $pid";
150 undef $child_watcher;
155 # Don’t bother killing i3 when we haven’t started it
156 return if $dont_start;
158 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
159 # files are not written) and fallback to killing it
160 if ($coverage_testing) {
163 say "Exiting i3 cleanly...";
164 i3("/tmp/nested-$display")->command('exit')->recv;
170 say "[$display] killing i3";
171 kill(9, $pid) or die "could not kill i3";
174 # This will be called as soon as i3 is running and answered to our
176 $activate_cv->cb(sub {
177 my $time_activating = [gettimeofday];
178 my $start_duration = tv_interval($time_before_start, $time_activating);
179 my ($status) = $activate_cv->recv;
181 say "[$display] Not starting i3, testcase does that";
183 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
186 say "[$display] Running $test with logfile $logpath";
189 open(my $spool, '>', \$output);
190 my $parser = TAP::Parser->new({
191 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -Ilib $test| ],
197 my ($stdout, $stderr) = $parser->get_select_handles;
198 for my $handle ($parser->get_select_handles) {
204 # Ignore activity on stderr (unnecessary with merge => 1,
205 # but let’s keep it in here if we want to use merge => 0
206 # for some reason in the future).
207 return if defined($stderr) and $handle == $stderr;
209 my $result = $parser->next;
210 if (defined($result)) {
211 # TODO: check if we should bail out
215 # $result is not defined, we are done parsing
216 say "[$display] $test finished";
217 close($parser->delete_spool);
218 $aggregator->add($test, $parser);
219 push @done, [ $test, $output ];
223 undef $_ for @watchers;
235 $activate_cv->send(1) if $dont_start;
243 my ($test, $output) = @$_;
244 say "output for $test:";
249 $harness->summary($aggregator);