2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
8 # the following are modules which ship with Perl (>= 5.10):
11 use File::Temp qw(tempfile tempdir);
16 use TAP::Parser::Aggregator;
17 # these are shipped with the testsuite
22 # the following modules are not shipped with Perl
26 use AnyEvent::I3 qw(:all);
27 use X11::XCB::Connection;
29 # Close superfluous file descriptors which were passed by running in a VIM
30 # subshell or situations like that.
31 AnyEvent::Util::close_all_fds_except(0, 1, 2);
33 # convinience wrapper to write to the log file
35 sub Log { say $log "@_" }
37 my $coverage_testing = 0;
41 # Number of tests to run in parallel. Important to know how many Xdummy
42 # instances we need to start (unless @displays are given). Defaults to
47 my $result = GetOptions(
48 "coverage-testing" => \$coverage_testing,
49 "valgrind" => \$valgrind,
51 "display=s" => \@displays,
52 "parallel=i" => \$parallel,
56 pod2usage(-verbose => 2, -exitcode => 0) if $help;
58 @displays = split(/,/, join(',', @displays));
59 @displays = map { s/ //g; $_ } @displays;
61 # No displays specified, let’s start some Xdummy instances.
62 @displays = start_xdummy($parallel) if @displays == 0;
64 # 1: create an output directory for this test-run
65 my $outdir = "testsuite-";
66 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
67 $outdir .= `git describe --tags`;
69 mkdir($outdir) or die "Could not create $outdir";
70 unlink("latest") if -e "latest";
71 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
74 # connect to all displays for two reasons:
75 # 1: check if the display actually works
76 # 2: keep the connection open so that i3 is not the only client. this prevents
77 # the X server from exiting (Xdummy will restart it, but not quick enough
80 for my $display (@displays) {
82 my $x = X11::XCB::Connection->new(display => $display);
84 die "Could not connect to display $display\n";
86 # start a TestWorker for each display
87 push @worker, worker($display, $x, $outdir);
91 # 2: get a list of all testcases
92 my @testfiles = @ARGV;
94 # if no files were passed on command line, run all tests from t/
95 @testfiles = <t/*.t> if @testfiles == 0;
97 my $logfile = "$outdir/complete-run.log";
98 open $log, '>', $logfile or die "Could not create '$logfile': $!";
99 say "Writing logfile to '$logfile'...";
103 my $num = @testfiles;
104 my $harness = TAP::Harness->new({ });
106 my $aggregator = TAP::Parser::Aggregator->new();
107 $aggregator->start();
109 status_init(displays => \@displays, tests => $num);
113 # We start tests concurrently: For each display, one test gets started. Every
114 # test starts another test after completing.
115 for (@worker) { $cv->begin; take_job($_) }
121 # print empty lines to seperate failed tests from statuslines
125 my ($test, $output) = @$_;
126 say "no output for $test" unless $output;
127 Log "output for $test:";
129 # print error messages of failed tests
130 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
134 $harness->summary($aggregator);
143 # Takes a test from the beginning of @testfiles and runs it.
145 # The TAP::Parser (which reads the test output) will get called as soon as
146 # there is some activity on the stdout file descriptor of the test process
147 # (using an AnyEvent->io watcher).
149 # When a test completes and @done contains $num entries, the $cv condvar gets
150 # triggered to finish testing.
155 my $test = shift @testfiles
158 my $display = $worker->{display};
160 Log status($display, "$test: starting");
161 worker_next($worker, $test);
163 # create a TAP::Parser with an in-memory fh
165 my $parser = TAP::Parser->new({
166 source => do { open(my $fh, '<', \$output); $fh },
169 my $ipc = $worker->{ipc};
176 state $tests_completed = 0;
179 sysread($ipc, my $buf, 4096) or die "sysread: $!";
182 $buf = $partial . $buf;
186 # make sure we feed TAP::Parser complete lines so it doesn't blow up
187 if (substr($buf, -1, 1) ne "\n") {
188 my $nl = rindex($buf, "\n");
194 # strip partial from buffer
195 $partial = substr($buf, $nl + 1, '');
198 # count lines before stripping eof-marker otherwise we might
199 # end up with for (1 .. 0) { } which would effectivly skip the loop
200 my $lines = $buf =~ tr/\n//;
201 my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
206 my $result = $parser->next;
207 if (defined($result) and $result->is_test) {
209 status($display, "$test: [$tests_completed/??] ");
213 return unless $t_eof;
215 Log status($display, "$test: finished");
216 status_completed(scalar @done);
218 $aggregator->add($test, $parser);
219 push @done, [ $test, $output ];
228 $_->() for our @CLEANUP;
232 # must be in a begin block because we C<exit 0> above
235 require Carp; Carp::cluck("Caught SIG$_[0]\n");
237 } for qw(INT TERM QUIT KILL PIPE)
244 complete-run.pl - Run the i3 testsuite
248 complete-run.pl [files...]
252 To run the whole testsuite on a reasonable number of Xdummy instances (your
253 running X11 will not be touched), run:
256 To run only a specific test (useful when developing a new feature), run:
257 ./complete-run t/100-fullscreen.t
265 Specifies which X11 display should be used. Can be specified multiple times and
266 will parallelize the tests:
268 # Run tests on the second X server
269 ./complete-run.pl -d :1
271 # Run four tests in parallel on some Xdummy servers
272 ./complete-run.pl -d :1,:2,:3,:4
274 Note that it is not necessary to specify this anymore. If omitted,
275 complete-run.pl will start (num_cores * 2) Xdummy instances.
279 Runs i3 under valgrind to find memory problems. The output will be available in
280 C<latest/valgrind-for-$test.log>.
284 Runs i3 under strace to trace system calls. The output will be available in
285 C<latest/strace-for-$test.log>.
287 =item B<--coverage-testing>
289 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
293 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
294 instances for some reason).
296 # Run all tests on a single Xdummy instance
297 ./complete-run.pl -p 1