Remove testsuite's assumptions about where it's mounted on a web server
[catagits/Catalyst-Runtime.git] / t / author / http-server.t
CommitLineData
040850b3 1use strict;
2use warnings;
3
81f25ce6 4use Test::More tests => 1;
4853fb50 5
040850b3 6use File::Path;
7use FindBin;
29057674 8use Test::TCP;
9use Try::Tiny;
f0ee1a76 10
81f25ce6 11use Catalyst::Devel 1.0;
12use File::Copy::Recursive;
040850b3 13
547f8806 14# Run a single test by providing it as the first arg
15my $single_test = shift;
16
81f25ce6 17my $tmpdir = "$FindBin::Bin/../../t/tmp";
2f381252 18
040850b3 19# clean up
2f381252 20rmtree $tmpdir if -d $tmpdir;
040850b3 21
22# create a TestApp and copy the test libs into it
2f381252 23mkdir $tmpdir;
24chdir $tmpdir;
81f25ce6 25system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
67c3b305 26chdir "$FindBin::Bin/..";
81f25ce6 27File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
10bdcbe8 28
29# remove TestApp's tests
81f25ce6 30rmtree '../t/tmp/TestApp/t' or die;
040850b3 31
32# spawn the standalone HTTP server
29057674 33my $port = empty_port;
d93d402c 34
35my $pid = fork;
36if ($pid) {
37 # parent.
38 print "Waiting for server to start...\n";
29057674 39 wait_port_timeout($port, 30);
d93d402c 40} elsif ($pid == 0) {
41 # child process
42 unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
43 require TestApp;
44
45 my $psgi_app = TestApp->_wrapped_legacy_psgi_app(TestApp->psgi_app);
46 Plack::Loader->auto(port => $port)->run($psgi_app);
47
48 exit 0;
49} else {
50 die "fork failed: $!";
896c9ed2 51}
32e231eb 52
040850b3 53# run the testsuite against the HTTP server
896c9ed2 54$ENV{CATALYST_SERVER} = "http://localhost:$port";
547f8806 55
e8e8895a 56chdir '..';
57
2f381252 58my $return;
547f8806 59if ( $single_test ) {
e8e8895a 60 $return = system( "$^X -Ilib/ $single_test" );
547f8806 61}
62else {
529d5abc 63 $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
547f8806 64}
040850b3 65
66# shut it down
e1b364f4 67kill 'INT', $pid;
040850b3 68
69# clean up
81f25ce6 70rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 71
2f381252 72is( $return, 0, 'live tests' );
55ddccae 73
29057674 74sub wait_port_timeout {
75 my ($port, $timeout) = @_;
76
77 # wait_port waits for 10 seconds
78 for (1 .. int($timeout / 10)) { # meh, good enough.
79 try { wait_port $port; 1 } and return;
896c9ed2 80 }
29057674 81
82 die "Server did not start within $timeout seconds";
896c9ed2 83}
868a7cca 84
85sub prove {
529d5abc 86 my (@tests) = @_;
868a7cca 87 if (!(my $pid = fork)) {
641b0131 88 require TAP::Harness;
e8e8895a 89
90 my $aggr = -e '.aggregating';
91 my $harness = TAP::Harness->new({
529d5abc 92 ($aggr ? (test_args => \@tests) : ()),
93 lib => ['lib'],
e8e8895a 94 });
95
96 my $aggregator = $aggr
97 ? $harness->runtests('t/aggregate.t')
529d5abc 98 : $harness->runtests(@tests);
641b0131 99
100 exit $aggregator->has_errors ? 1 : 0;
868a7cca 101 } else {
102 waitpid $pid, 0;
103 return $?;
104 }
105}