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 | |
2f381252 |
52 | my $return; |
547f8806 |
53 | if ( $single_test ) { |
81f25ce6 |
54 | $return = system( "$^X -I../lib/ $single_test" ); |
547f8806 |
55 | } |
56 | else { |
81f25ce6 |
57 | $return = prove( '-r', '-I../lib/', glob('../t/aggregate/live_*.t') ); |
547f8806 |
58 | } |
040850b3 |
59 | |
60 | # shut it down |
e1b364f4 |
61 | kill 'INT', $pid; |
896c9ed2 |
62 | close $server; |
040850b3 |
63 | |
64 | # clean up |
81f25ce6 |
65 | rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp"; |
896c9ed2 |
66 | |
2f381252 |
67 | is( $return, 0, 'live tests' ); |
55ddccae |
68 | |
896c9ed2 |
69 | sub check_port { |
70 | my ( $host, $port ) = @_; |
71 | |
72 | my $remote = IO::Socket::INET->new( |
73 | Proto => "tcp", |
74 | PeerAddr => $host, |
75 | PeerPort => $port |
76 | ); |
77 | if ($remote) { |
78 | close $remote; |
79 | return 1; |
80 | } |
81 | else { |
82 | return 0; |
83 | } |
84 | } |
868a7cca |
85 | |
86 | sub prove { |
87 | if (!(my $pid = fork)) { |
88 | require App::Prove; |
89 | my $prove = App::Prove->new; |
90 | $prove->process_args(@_); |
91 | exit( $prove->run ? 0 : 1 ); |
92 | } else { |
93 | waitpid $pid, 0; |
94 | return $?; |
95 | } |
96 | } |