ok, here we go :)
I have a preliminary implementation. what happens now is that two files are autogenerated if they don't already exist:
t/server-start.t t/server-stop.t
they are used to control the starting and stopping of the server. so, the output of make test looks like
# writing file: server-start.t # writing file: server-stop.t pre-start/ping....ok server-start......*** server localhost.localdomain:8529 started server-start......ok ping..............ok request...........ok server-stop.......*** server localhost.localdomain:8529 shutdown server-stop.......ok post-stop/ping....ok All tests successful. Files=6, Tests=20, 3 wallclock secs ( 2.03 cusr + 0.16 csys = 2.19 CPU) # removing file: server-start.t # removing file: server-stop.t
another condition of server-control file autogeneration is that there be real server tests to run. this makes it trivial to run pre-start or post-stop tests by themselves:
$ t/TEST t/pre-start/ *** setting ulimit to allow core files ulimit -c unlimited; t/TEST 't/pre-start/' pre-start/ping....ok All tests successful. Files=1, Tests=3, 1 wallclock secs ( 0.28 cusr + 0.03 csys = 0.31 CPU)
anyway, so that's how it looks on the outside. on the inside is another matter...
as I mentioned, I'm really not too sure about mucking with the core engine like this (although I did take some comfort in the fact that not only did the changes work for all of my personal A-T modules, but I didn't need any tweaks to get the mod_perl 2.0 suite running successfully). I might prefer to create a subclass of TestRun and TestHarness that works this way and leave it up to people to choose which they use from TEST.PL - it depends on how much the people here like the implementation (after any suggested refinements and refactoring are applied, of course :)
so, give it a whirl and let me know what you think.
--Geoff
Index: lib/Apache/TestHarness.pm =================================================================== RCS file: /home/cvspublic/httpd-test/perl-framework/Apache-Test/lib/Apache/TestHarness.pm,v retrieving revision 1.12 diff -u -r1.12 TestHarness.pm --- lib/Apache/TestHarness.pm 12 Sep 2003 02:20:27 -0000 1.12 +++ lib/Apache/TestHarness.pm 23 Sep 2003 14:04:54 -0000 @@ -6,6 +6,7 @@ use Test::Harness (); use Apache::TestSort (); use Apache::TestTrace; +use Apache::TestUtil qw(t_write_file); use File::Spec::Functions qw(catfile); use File::Find qw(finddepth); use File::Basename qw(dirname); @@ -98,12 +99,11 @@ sub get_tests { my $self = shift; - my $args = shift; - my @tests = (); + my (@tests, @pretests, @posttests) = (); chdir_t(); - my $ts = $args->{tests} || []; + my $ts = $self->{tests} || []; if (@$ts) { for (@$ts) { @@ -117,8 +117,8 @@ } } else { - if ($args->{tdirs}) { - push @tests, map { sort <$_/*.t> } @{ $args->{tdirs} }; + if ($self->{tdirs}) { + push @tests, map { sort <$_/*.t> } @{ $self->{tdirs} }; } else { finddepth(sub { @@ -138,26 +138,97 @@ @tests = grep { not /(?:$skip)/ } @tests; } - Apache::TestSort->run([EMAIL PROTECTED], $args); + Apache::TestSort->run([EMAIL PROTECTED], $self); #when running 't/TEST t/dir' shell tab completion adds a / #dir//foo output is annoying, fix that. s:/+:/:g for @tests; - return @tests; + # post-sort ordering: + # 1 - pre-start/ + # 2 - start-server.t + # 3 - @tests + # 4 - stop-server.t + # 5 - post-stop/ + + for (my $i = 0; $i < @tests; $i++) { + + my $prestart = catfile 'pre-start', ''; + my $poststop = catfile 'post-stop', ''; + my $delim = catfile '', ''; + + if ($tests[$i] =~ m!^\Q$prestart!) { + push @pretests, delete $tests[$i]; + } + elsif ($tests[$i] =~ m!^\Q$poststop!) { + push @posttests, delete $tests[$i]; + } + + # server-start.t and server-stop.t need special + # placement if they already exist, so delete them + # from here and add them later + elsif ($tests[$i] =~ m!server-start.t!) { + delete $tests[$i]; + } + elsif ($tests[$i] =~ m!server-stop.t!) { + delete $tests[$i]; + } + else { + # nothing to do + } + } + + # start and stop the server if required + if (@tests) { + $self->generate_server_script('start'); + $self->generate_server_script('stop'); + unshift @tests, 'server-start.t'; + push @tests, 'server-stop.t'; + } + + return @pretests, grep { $_ } @tests, @posttests; } -sub run { +sub harness { my $self = shift; - my $args = shift || {}; - $Test::Harness::verbose ||= $args->{verbose}; + $Test::Harness::verbose ||= $self->{opts}->{verbose}; - if (my(@subtests) = @{ $args->{subtests} || [] }) { + if (my(@subtests) = @{ $self->{subtests} || [] }) { $ENV{HTTPD_TEST_SUBTESTS} = "@subtests"; } - Test::Harness::runtests($self->get_tests($args, @_)); + Test::Harness::runtests($self->get_tests(@_)); +} + +sub generate_server_script { + my $self = shift; + my $action = shift; + + return if -e "server-$action.t"; + + my $d = Data::Dumper->new([$self->{opts}]); + my $clone = $d->Purity(1)->Terse(1)->Dump; + + chomp $clone; + + my $file = <<EOF; +use Apache::TestConfig (); +use Apache::TestRun (); +use Apache::Test; + +use strict; +use warnings FATAL => 'all'; + +plan tests => 1; + +ok (Apache::TestRun->new(test_config => Apache::TestConfig->thaw, + opts => $clone) + ->$action); + +EOF + + t_write_file ("server-$action.t", $file); } 1; Index: lib/Apache/TestRun.pm =================================================================== RCS file: /home/cvspublic/httpd-test/perl-framework/Apache-Test/lib/Apache/TestRun.pm,v retrieving revision 1.114 diff -u -r1.114 TestRun.pm --- lib/Apache/TestRun.pm 12 Sep 2003 02:21:32 -0000 1.114 +++ lib/Apache/TestRun.pm 23 Sep 2003 14:04:55 -0000 @@ -10,12 +10,16 @@ use Apache::TestRequest (); use Apache::TestHarness (); use Apache::TestTrace; +use Apache::TestUtil qw(t_write_file); use File::Find qw(finddepth); use File::Spec::Functions qw(catfile); use Getopt::Long qw(GetOptions); use Config; +use vars qw(@ISA); [EMAIL PROTECTED] = qw(Apache::TestHarness); + use constant STARTUP_TIMEOUT => 300; # secs (good for extreme debug cases) use subs qw(exit_shell exit_perl); @@ -447,7 +451,7 @@ } my $opts = $self->{opts}; - my $server = $self->{server}; + my $server = $self->{server} || $self->{test_config}->server; #if t/TEST -d is running make sure we don't try to stop/start the server my $file = $server->debugger_file; @@ -482,20 +486,11 @@ sub run_tests { my $self = shift; - my $test_opts = { - verbose => $self->{opts}->{verbose}, - tests => $self->{tests}, - times => $self->{opts}->{times}, - order => $self->{opts}->{order}, - subtests => $self->{subtests} || [], - }; - if (grep { exists $self->{opts}->{$_} } @request_opts) { run_request($self->{test_config}, $self->{opts}); } else { - Apache::TestHarness->run($test_opts) - if $self->{opts}->{'run-tests'}; + $self->harness if $self->{opts}->{'run-tests'}; } } @@ -504,7 +499,9 @@ $self->restore_t_perms; - return $self->{server}->stop if $self->{opts}->{'stop-httpd'}; + my $server = $self->{server} || $self->{test_config}->server; + + return $server->stop if $self->{opts}->{'stop-httpd'}; } sub new_test_config { @@ -609,11 +606,7 @@ $self->die_on_invalid_args; - $self->start; - $self->run_tests; - - $self->stop; } my @oh = qw(jeez golly gosh darn shucks dangit rats nuts dangnabit crap); Index: lib/Apache/TestServer.pm =================================================================== RCS file: /home/cvspublic/httpd-test/perl-framework/Apache-Test/lib/Apache/TestServer.pm,v retrieving revision 1.65 diff -u -r1.65 TestServer.pm --- lib/Apache/TestServer.pm 18 Sep 2003 07:39:35 -0000 1.65 +++ lib/Apache/TestServer.pm 23 Sep 2003 14:04:55 -0000 @@ -518,7 +518,7 @@ $SIG{CHLD} = $old_sig || 'DEFAULT'; if (my $pid = $self->pid) { - print "server $self->{name} started\n"; + warning "server $self->{name} started"; my $vh = $config->{vhosts}; my $by_port = sub { $vh->{$a}->{port} <=> $vh->{$b}->{port} };
--- /dev/null 2003-01-30 05:24:37.000000000 -0500 +++ t/pre-start/ping.t 2003-09-23 09:26:40.000000000 -0400 @@ -0,0 +1,17 @@ +use strict; +use warnings FATAL => 'all'; + +use Apache::Test; + +plan tests => 3; + +my $config = Apache::Test::config(); + +ok $config; + +my $server = $config->server; + +ok $server; + +ok ! $server->ping; + --- /dev/null 2003-01-30 05:24:37.000000000 -0500 +++ t/post-stop/ping.t 2003-09-23 09:26:31.000000000 -0400 @@ -0,0 +1,17 @@ +use strict; +use warnings FATAL => 'all'; + +use Apache::Test; + +plan tests => 3; + +my $config = Apache::Test::config(); + +ok $config; + +my $server = $config->server; + +ok $server; + +ok ! $server->ping; +