Commit | Line | Data |
040850b3 |
1 | use strict; |
2 | use warnings; |
3 | |
81f25ce6 |
4 | use Test::More tests => 1; |
4853fb50 |
5 | |
040850b3 |
6 | use File::Path; |
7 | use FindBin; |
29057674 |
8 | use Test::TCP; |
9 | use Try::Tiny; |
876db346 |
10 | use Plack::Builder; |
f0ee1a76 |
11 | |
81f25ce6 |
12 | use Catalyst::Devel 1.0; |
13 | use File::Copy::Recursive; |
040850b3 |
14 | |
547f8806 |
15 | # Run a single test by providing it as the first arg |
16 | my $single_test = shift; |
17 | |
81f25ce6 |
18 | my $tmpdir = "$FindBin::Bin/../../t/tmp"; |
2f381252 |
19 | |
040850b3 |
20 | # clean up |
2f381252 |
21 | rmtree $tmpdir if -d $tmpdir; |
040850b3 |
22 | |
23 | # create a TestApp and copy the test libs into it |
2f381252 |
24 | mkdir $tmpdir; |
25 | chdir $tmpdir; |
81f25ce6 |
26 | system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' ); |
67c3b305 |
27 | chdir "$FindBin::Bin/.."; |
81f25ce6 |
28 | File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die; |
10bdcbe8 |
29 | |
30 | # remove TestApp's tests |
81f25ce6 |
31 | rmtree '../t/tmp/TestApp/t' or die; |
040850b3 |
32 | |
33 | # spawn the standalone HTTP server |
29057674 |
34 | my $port = empty_port; |
d93d402c |
35 | |
36 | my $pid = fork; |
37 | if ($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 |
62 | chdir '..'; |
63 | |
2f381252 |
64 | my $return; |
547f8806 |
65 | if ( $single_test ) { |
e8e8895a |
66 | $return = system( "$^X -Ilib/ $single_test" ); |
547f8806 |
67 | } |
68 | else { |
529d5abc |
69 | $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t')); |
547f8806 |
70 | } |
040850b3 |
71 | |
72 | # shut it down |
e1b364f4 |
73 | kill 'INT', $pid; |
040850b3 |
74 | |
75 | # clean up |
81f25ce6 |
76 | rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp"; |
896c9ed2 |
77 | |
2f381252 |
78 | is( $return, 0, 'live tests' ); |
55ddccae |
79 | |
29057674 |
80 | sub wait_port_timeout { |
81 | my ($port, $timeout) = @_; |
82 | |
83 | # wait_port waits for 10 seconds |
84 | for (1 .. int($timeout / 10)) { # meh, good enough. |
85 | try { wait_port $port; 1 } and return; |
896c9ed2 |
86 | } |
29057674 |
87 | |
88 | die "Server did not start within $timeout seconds"; |
896c9ed2 |
89 | } |
868a7cca |
90 | |
91 | sub prove { |
529d5abc |
92 | my (@tests) = @_; |
868a7cca |
93 | if (!(my $pid = fork)) { |
641b0131 |
94 | require TAP::Harness; |
e8e8895a |
95 | |
96 | my $aggr = -e '.aggregating'; |
97 | my $harness = TAP::Harness->new({ |
529d5abc |
98 | ($aggr ? (test_args => \@tests) : ()), |
99 | lib => ['lib'], |
e8e8895a |
100 | }); |
101 | |
102 | my $aggregator = $aggr |
103 | ? $harness->runtests('t/aggregate.t') |
529d5abc |
104 | : $harness->runtests(@tests); |
641b0131 |
105 | |
106 | exit $aggregator->has_errors ? 1 : 0; |
868a7cca |
107 | } else { |
108 | waitpid $pid, 0; |
109 | return $?; |
110 | } |
111 | } |