Commit | Line | Data |
040850b3 |
1 | use strict; |
2 | use warnings; |
3 | |
ae29b412 |
4 | use Test::More; |
5 | BEGIN { |
6 | plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP}; |
7 | } |
8 | |
040850b3 |
9 | use File::Path; |
10 | use FindBin; |
b63f73dc |
11 | use IPC::Open3; |
896c9ed2 |
12 | use IO::Socket; |
f0ee1a76 |
13 | |
5c9c810d |
14 | eval "use Catalyst::Devel 1.0"; |
15 | plan skip_all => 'Catalyst::Devel required' if $@; |
0d348d55 |
16 | eval "use File::Copy::Recursive"; |
17 | plan skip_all => 'File::Copy::Recursive required' if $@; |
55ddccae |
18 | plan tests => 1; |
040850b3 |
19 | |
547f8806 |
20 | # Run a single test by providing it as the first arg |
21 | my $single_test = shift; |
22 | |
b63f73dc |
23 | my $tmpdir = "$FindBin::Bin/../t/tmp"; |
24 | |
040850b3 |
25 | # clean up |
b63f73dc |
26 | rmtree $tmpdir if -d $tmpdir; |
040850b3 |
27 | |
28 | # create a TestApp and copy the test libs into it |
b63f73dc |
29 | mkdir $tmpdir; |
30 | chdir $tmpdir; |
ae29b412 |
31 | system( $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/catalyst.pl", 'TestApp' ); |
67c3b305 |
32 | chdir "$FindBin::Bin/.."; |
a2e038a1 |
33 | File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' ); |
10bdcbe8 |
34 | |
35 | # remove TestApp's tests |
36 | rmtree 't/tmp/TestApp/t'; |
040850b3 |
37 | |
38 | # spawn the standalone HTTP server |
896c9ed2 |
39 | my $port = 30000 + int rand(1 + 10000); |
b63f73dc |
40 | my $pid = open3( undef, my $server, undef, |
ae29b412 |
41 | $^X, "-I$FindBin::Bin/../lib", |
b63f73dc |
42 | "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '-port', $port ) |
040850b3 |
43 | or die "Unable to spawn standalone HTTP server: $!"; |
896c9ed2 |
44 | |
040850b3 |
45 | # wait for it to start |
896c9ed2 |
46 | print "Waiting for server to start...\n"; |
47 | while ( check_port( 'localhost', $port ) != 1 ) { |
48 | sleep 1; |
49 | } |
32e231eb |
50 | |
040850b3 |
51 | # run the testsuite against the HTTP server |
896c9ed2 |
52 | $ENV{CATALYST_SERVER} = "http://localhost:$port"; |
547f8806 |
53 | |
b63f73dc |
54 | my $return; |
547f8806 |
55 | if ( $single_test ) { |
ae29b412 |
56 | $return = system( "$^X -Ilib/ $single_test" ); |
547f8806 |
57 | } |
58 | else { |
ae29b412 |
59 | $return = prove( '-r', '-Ilib/', 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 |
a2e038a1 |
67 | rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp"; |
896c9ed2 |
68 | |
b63f73dc |
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 | } |
ae29b412 |
87 | |
88 | sub prove { |
89 | if (!(my $pid = fork)) { |
90 | require App::Prove; |
91 | my $prove = App::Prove->new; |
92 | $prove->process_args(@_); |
93 | exit( $prove->run ? 0 : 1 ); |
94 | } else { |
95 | waitpid $pid, 0; |
96 | return $?; |
97 | } |
98 | } |