Fix author requires to not fatally error on a single command
[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;
876db346 10use Plack::Builder;
f0ee1a76 11
81f25ce6 12use Catalyst::Devel 1.0;
13use File::Copy::Recursive;
040850b3 14
547f8806 15# Run a single test by providing it as the first arg
16my $single_test = shift;
17
81f25ce6 18my $tmpdir = "$FindBin::Bin/../../t/tmp";
2f381252 19
040850b3 20# clean up
2f381252 21rmtree $tmpdir if -d $tmpdir;
040850b3 22
23# create a TestApp and copy the test libs into it
2f381252 24mkdir $tmpdir;
25chdir $tmpdir;
81f25ce6 26system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
67c3b305 27chdir "$FindBin::Bin/..";
81f25ce6 28File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;
10bdcbe8 29
30# remove TestApp's tests
81f25ce6 31rmtree '../t/tmp/TestApp/t' or die;
040850b3 32
33# spawn the standalone HTTP server
29057674 34my $port = empty_port;
d93d402c 35
36my $pid = fork;
37if ($pid) {
38 # parent.
39 print "Waiting for server to start...\n";
29057674 40 wait_port_timeout($port, 30);
d93d402c 41} elsif ($pid == 0) {
42 # child process
43 unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
44 require TestApp;
45
1316cc64 46 my $psgi_app = TestApp->apply_default_middlewares(TestApp->psgi_app);
876db346 47 Plack::Loader->auto(port => $port)->run(builder {
48 mount '/test_prefix' => $psgi_app;
49 mount '/' => sub {
50 return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
51 };
52 });
d93d402c 53
54 exit 0;
55} else {
56 die "fork failed: $!";
896c9ed2 57}
32e231eb 58
040850b3 59# run the testsuite against the HTTP server
876db346 60$ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";
547f8806 61
e8e8895a 62chdir '..';
63
2f381252 64my $return;
547f8806 65if ( $single_test ) {
e8e8895a 66 $return = system( "$^X -Ilib/ $single_test" );
547f8806 67}
68else {
529d5abc 69 $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
547f8806 70}
040850b3 71
72# shut it down
e1b364f4 73kill 'INT', $pid;
040850b3 74
75# clean up
81f25ce6 76rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";
896c9ed2 77
2f381252 78is( $return, 0, 'live tests' );
55ddccae 79
8b260bdf 80# kill 'INT' doesn't exist in Windows, so to prevent child hanging,
81# this process will need to commit seppuku to clean up the children.
82if ($^O eq 'MSWin32') {
83 # Furthermore, it needs to do it 'politely' so that TAP doesn't
84 # smell anything 'dubious'.
85 require Win32::Process; # core in all versions of Win32 Perl
86 Win32::Process::KillProcess($$, $return);
87}
88
29057674 89sub wait_port_timeout {
90 my ($port, $timeout) = @_;
91
92 # wait_port waits for 10 seconds
93 for (1 .. int($timeout / 10)) { # meh, good enough.
94 try { wait_port $port; 1 } and return;
896c9ed2 95 }
29057674 96
97 die "Server did not start within $timeout seconds";
896c9ed2 98}
868a7cca 99
100sub prove {
529d5abc 101 my (@tests) = @_;
868a7cca 102 if (!(my $pid = fork)) {
641b0131 103 require TAP::Harness;
e8e8895a 104
105 my $aggr = -e '.aggregating';
106 my $harness = TAP::Harness->new({
529d5abc 107 ($aggr ? (test_args => \@tests) : ()),
108 lib => ['lib'],
e8e8895a 109 });
110
111 my $aggregator = $aggr
112 ? $harness->runtests('t/aggregate.t')
529d5abc 113 : $harness->runtests(@tests);
641b0131 114
115 exit $aggregator->has_errors ? 1 : 0;
868a7cca 116 } else {
117 waitpid $pid, 0;
118 return $?;
119 }
120}