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
16 use IO::Scalar; # not in core :\
17 use File::Temp qw(tempfile tempdir);
25 use TAP::Parser::Aggregator;
26 use File::Basename qw(basename);
27 use AnyEvent::I3 qw(:all);
30 use Time::HiRes qw(sleep);
31 use X11::XCB::Connection;
32 use IO::Socket::UNIX; # core
36 # open a file so that we get file descriptor 3. we will later close it in the
37 # child and dup() the listening socket file descriptor to 3 to pass it to i3
38 open(my $reserved, '<', '/dev/null');
39 if (fileno($reserved) != 3) {
40 warn "Socket file descriptor is not 3.";
41 warn "Please don't start this script within a subshell of vim or something.";
45 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
46 # XXX: we could maybe also use a different loop than the default loop in EV?
50 # reads in a whole file
52 open my $fh, '<', shift;
57 my $coverage_testing = 0;
60 my $result = GetOptions(
61 "coverage-testing" => \$coverage_testing,
62 "display=s" => \@displays,
65 @displays = split(/,/, join(',', @displays));
66 @displays = map { s/ //g; $_ } @displays;
68 @displays = qw(:1) if @displays == 0;
70 # connect to all displays for two reasons:
71 # 1: check if the display actually works
72 # 2: keep the connection open so that i3 is not the only client. this prevents
73 # the X server from exiting (Xdummy will restart it, but not quick enough
77 for my $display (@displays) {
79 my $x = X11::XCB::Connection->new(display => $display);
81 push @wdisplays, $display;
83 say STDERR "WARNING: Not using X11 display $display, could not connect";
87 my $config = slurp('i3-test.config');
89 # 1: get a list of all testcases
90 my @testfiles = @ARGV;
92 # if no files were passed on command line, run all tests from t/
93 @testfiles = <t/*.t> if @testfiles == 0;
95 # 2: create an output directory for this test-run
96 my $outdir = "testsuite-";
97 $outdir .= DateTime->now->strftime("%Y-%m-%d-%H-%M-%S-");
98 $outdir .= `git describe --tags`;
100 mkdir($outdir) or die "Could not create $outdir";
101 unlink("latest") if -e "latest";
102 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
106 my $num = @testfiles;
107 my $harness = TAP::Harness->new({ });
109 my $aggregator = TAP::Parser::Aggregator->new();
110 $aggregator->start();
112 my $cv = AnyEvent->condvar;
114 # We start tests concurrently: For each display, one test gets started. Every
115 # test starts another test after completing.
116 take_job($_) for @wdisplays;
119 # Takes a test from the beginning of @testfiles and runs it.
121 # The TAP::Parser (which reads the test output) will get called as soon as
122 # there is some activity on the stdout file descriptor of the test process
123 # (using an AnyEvent->io watcher).
125 # When a test completes and @done contains $num entries, the $cv condvar gets
126 # triggered to finish testing.
131 my $test = shift @testfiles;
133 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
134 my $logpath = "$outdir/i3-log-for-" . basename($test);
136 my ($fh, $tmpfile) = tempfile();
138 say $fh "ipc-socket /tmp/nested-$display";
141 my $activate_cv = AnyEvent->condvar;
143 # remove the old unix socket
144 unlink("/tmp/nested-$display-activation");
146 # pass all file descriptors up to three to the children.
147 # we need to set this flag before opening the socket.
148 open(my $fdtest, '<', '/dev/null');
149 $^F = fileno($fdtest);
151 my $socket = IO::Socket::UNIX->new(
153 Local => "/tmp/nested-$display-activation",
157 if (!defined($pid)) {
158 die "could not fork()";
163 $ENV{LISTEN_PID} = $$;
164 $ENV{LISTEN_FDS} = 1;
165 $ENV{DISPLAY} = $display;
168 say "fileno is " . fileno($socket);
170 POSIX::dup2(fileno($socket), 3);
173 my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
174 my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
175 exec "/bin/sh", '-c', $cmd;
177 # if we are still here, i3 could not be found or exec failed. bail out.
182 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
183 say "child died. pid = $pid";
184 undef $child_watcher;
187 # close the socket, the child process should be the only one which keeps a file
188 # descriptor on the listening socket.
191 # We now connect (will succeed immediately) and send a request afterwards.
192 # As soon as the reply is there, i3 is considered ready.
193 my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
195 $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
197 # send a get_tree message without payload
198 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
201 $hdl->push_read(chunk => 1, => sub {
203 say "read something from i3";
204 $activate_cv->send(1);
212 $pid = $start_i3->() unless $dont_start;
215 # Don’t bother killing i3 when we haven’t started it
216 return if $dont_start;
218 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
219 # files are not written) and fallback to killing it
220 if ($coverage_testing) {
223 say "Exiting i3 cleanly...";
224 i3("/tmp/nested-$display")->command('exit')->recv;
230 say "[$display] killing i3";
231 kill(9, $pid) or die "could not kill i3";
234 # This will be called as soon as i3 is running and answered to our
236 $activate_cv->cb(sub {
238 my ($status) = $activate_cv->recv;
239 say "complete-run: status = $status";
241 say "[$display] Running $test with logfile $logpath";
244 my $parser = TAP::Parser->new({
245 exec => [ 'sh', '-c', "DISPLAY=$display /usr/bin/perl -It/lib $test" ],
246 spool => IO::Scalar->new(\$output),
251 my ($stdout, $stderr) = $parser->get_select_handles;
252 for my $handle ($parser->get_select_handles) {
258 # Ignore activity on stderr (unnecessary with merge => 1,
259 # but let’s keep it in here if we want to use merge => 0
260 # for some reason in the future).
261 return if defined($stderr) and $handle == $stderr;
263 my $result = $parser->next;
264 if (defined($result)) {
265 # TODO: check if we should bail out
269 # $result is not defined, we are done parsing
270 say "[$display] $test finished";
271 close($parser->delete_spool);
272 $aggregator->add($test, $parser);
273 push @done, [ $test, $output ];
277 undef $_ for @watchers;
289 $activate_cv->send(1) if $dont_start;
297 my ($test, $output) = @$_;
298 say "output for $test:";
303 $harness->summary($aggregator);