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; |
2f381252 |
8 | use IPC::Open3; |
896c9ed2 |
9 | use IO::Socket; |
f0ee1a76 |
10 | |
81f25ce6 |
11 | use Catalyst::Devel 1.0; |
12 | use File::Copy::Recursive; |
040850b3 |
13 | |
547f8806 |
14 | # Run a single test by providing it as the first arg |
15 | my $single_test = shift; |
16 | |
81f25ce6 |
17 | my $tmpdir = "$FindBin::Bin/../../t/tmp"; |
2f381252 |
18 | |
040850b3 |
19 | # clean up |
2f381252 |
20 | rmtree $tmpdir if -d $tmpdir; |
040850b3 |
21 | |
22 | # create a TestApp and copy the test libs into it |
2f381252 |
23 | mkdir $tmpdir; |
24 | chdir $tmpdir; |
81f25ce6 |
25 | system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' ); |
67c3b305 |
26 | chdir "$FindBin::Bin/.."; |
81f25ce6 |
27 | File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die; |
10bdcbe8 |
28 | |
29 | # remove TestApp's tests |
81f25ce6 |
30 | rmtree '../t/tmp/TestApp/t' or die; |
040850b3 |
31 | |
32 | # spawn the standalone HTTP server |
896c9ed2 |
33 | my $port = 30000 + int rand(1 + 10000); |
81f25ce6 |
34 | my @cmd = ($^X, "-I$FindBin::Bin/../../lib", |
3dcfb4c8 |
35 | "$FindBin::Bin/../../t/tmp/TestApp/script/testapp_server.pl", '--port', $port ); |
81f25ce6 |
36 | my $pid = open3( undef, my $server, undef, @cmd) |
040850b3 |
37 | or die "Unable to spawn standalone HTTP server: $!"; |
896c9ed2 |
38 | |
040850b3 |
39 | # wait for it to start |
896c9ed2 |
40 | print "Waiting for server to start...\n"; |
81f25ce6 |
41 | my $timeout = 30; |
42 | my $count = 0; |
896c9ed2 |
43 | while ( check_port( 'localhost', $port ) != 1 ) { |
44 | sleep 1; |
81f25ce6 |
45 | die("Server did not start within $timeout seconds: " . join(' ', @cmd)) |
46 | if $count++ > $timeout; |
896c9ed2 |
47 | } |
32e231eb |
48 | |
040850b3 |
49 | # run the testsuite against the HTTP server |
896c9ed2 |
50 | $ENV{CATALYST_SERVER} = "http://localhost:$port"; |
547f8806 |
51 | |
e8e8895a |
52 | chdir '..'; |
53 | |
2f381252 |
54 | my $return; |
547f8806 |
55 | if ( $single_test ) { |
e8e8895a |
56 | $return = system( "$^X -Ilib/ $single_test" ); |
547f8806 |
57 | } |
58 | else { |
529d5abc |
59 | $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t')); |
547f8806 |
60 | } |
040850b3 |
61 | |
62 | # shut it down |
e1b364f4 |
63 | kill 'INT', $pid; |
896c9ed2 |
64 | close $server; |
040850b3 |
65 | |
66 | # clean up |
81f25ce6 |
67 | rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp"; |
896c9ed2 |
68 | |
2f381252 |
69 | is( $return, 0, 'live tests' ); |
55ddccae |
70 | |
896c9ed2 |
71 | sub check_port { |
72 | my ( $host, $port ) = @_; |
73 | |
74 | my $remote = IO::Socket::INET->new( |
75 | Proto => "tcp", |
76 | PeerAddr => $host, |
77 | PeerPort => $port |
78 | ); |
79 | if ($remote) { |
80 | close $remote; |
81 | return 1; |
82 | } |
83 | else { |
84 | return 0; |
85 | } |
86 | } |
868a7cca |
87 | |
88 | sub prove { |
529d5abc |
89 | my (@tests) = @_; |
868a7cca |
90 | if (!(my $pid = fork)) { |
641b0131 |
91 | require TAP::Harness; |
e8e8895a |
92 | |
93 | my $aggr = -e '.aggregating'; |
94 | my $harness = TAP::Harness->new({ |
529d5abc |
95 | ($aggr ? (test_args => \@tests) : ()), |
96 | lib => ['lib'], |
e8e8895a |
97 | }); |
98 | |
99 | my $aggregator = $aggr |
100 | ? $harness->runtests('t/aggregate.t') |
529d5abc |
101 | : $harness->runtests(@tests); |
641b0131 |
102 | |
103 | exit $aggregator->has_errors ? 1 : 0; |
868a7cca |
104 | } else { |
105 | waitpid $pid, 0; |
106 | return $?; |
107 | } |
108 | } |