Commit | Line | Data |
040850b3 |
1 | use strict; |
2 | use warnings; |
3 | |
040850b3 |
4 | use File::Path; |
5 | use FindBin; |
2f381252 |
6 | use IPC::Open3; |
896c9ed2 |
7 | use IO::Socket; |
f0ee1a76 |
8 | use Test::More; |
f0ee1a76 |
9 | |
f0ee1a76 |
10 | plan skip_all => 'set TEST_HTTP to enable this test' unless $ENV{TEST_HTTP}; |
5c9c810d |
11 | eval "use Catalyst::Devel 1.0"; |
12 | plan skip_all => 'Catalyst::Devel required' if $@; |
0d348d55 |
13 | eval "use File::Copy::Recursive"; |
14 | plan skip_all => 'File::Copy::Recursive required' if $@; |
55ddccae |
15 | plan tests => 1; |
040850b3 |
16 | |
547f8806 |
17 | # Run a single test by providing it as the first arg |
18 | my $single_test = shift; |
19 | |
2f381252 |
20 | my $tmpdir = "$FindBin::Bin/../t/tmp"; |
21 | |
040850b3 |
22 | # clean up |
2f381252 |
23 | rmtree $tmpdir if -d $tmpdir; |
040850b3 |
24 | |
25 | # create a TestApp and copy the test libs into it |
2f381252 |
26 | mkdir $tmpdir; |
27 | chdir $tmpdir; |
868a7cca |
28 | system( $^X, "-I$FindBin::Bin/../lib", "$FindBin::Bin/../script/catalyst.pl", 'TestApp' ); |
67c3b305 |
29 | chdir "$FindBin::Bin/.."; |
a2e038a1 |
30 | File::Copy::Recursive::dircopy( 't/lib', 't/tmp/TestApp/lib' ); |
10bdcbe8 |
31 | |
32 | # remove TestApp's tests |
33 | rmtree 't/tmp/TestApp/t'; |
040850b3 |
34 | |
35 | # spawn the standalone HTTP server |
896c9ed2 |
36 | my $port = 30000 + int rand(1 + 10000); |
2f381252 |
37 | my $pid = open3( undef, my $server, undef, |
868a7cca |
38 | $^X, "-I$FindBin::Bin/../lib", |
2f381252 |
39 | "$FindBin::Bin/../t/tmp/TestApp/script/testapp_server.pl", '-port', $port ) |
040850b3 |
40 | or die "Unable to spawn standalone HTTP server: $!"; |
896c9ed2 |
41 | |
040850b3 |
42 | # wait for it to start |
896c9ed2 |
43 | print "Waiting for server to start...\n"; |
44 | while ( check_port( 'localhost', $port ) != 1 ) { |
45 | sleep 1; |
46 | } |
32e231eb |
47 | |
040850b3 |
48 | # run the testsuite against the HTTP server |
896c9ed2 |
49 | $ENV{CATALYST_SERVER} = "http://localhost:$port"; |
547f8806 |
50 | |
2f381252 |
51 | my $return; |
547f8806 |
52 | if ( $single_test ) { |
868a7cca |
53 | $return = system( "$^X -Ilib/ $single_test" ); |
547f8806 |
54 | } |
55 | else { |
a68314c2 |
56 | $return = prove( '-r', '-Ilib/', glob('t/aggregate/live_*.t') ); |
547f8806 |
57 | } |
040850b3 |
58 | |
59 | # shut it down |
e1b364f4 |
60 | kill 'INT', $pid; |
896c9ed2 |
61 | close $server; |
040850b3 |
62 | |
63 | # clean up |
a2e038a1 |
64 | rmtree "$FindBin::Bin/../t/tmp" if -d "$FindBin::Bin/../t/tmp"; |
896c9ed2 |
65 | |
2f381252 |
66 | is( $return, 0, 'live tests' ); |
55ddccae |
67 | |
896c9ed2 |
68 | sub check_port { |
69 | my ( $host, $port ) = @_; |
70 | |
71 | my $remote = IO::Socket::INET->new( |
72 | Proto => "tcp", |
73 | PeerAddr => $host, |
74 | PeerPort => $port |
75 | ); |
76 | if ($remote) { |
77 | close $remote; |
78 | return 1; |
79 | } |
80 | else { |
81 | return 0; |
82 | } |
83 | } |
868a7cca |
84 | |
85 | sub prove { |
86 | if (!(my $pid = fork)) { |
87 | require App::Prove; |
88 | my $prove = App::Prove->new; |
89 | $prove->process_args(@_); |
90 | exit( $prove->run ? 0 : 1 ); |
91 | } else { |
92 | waitpid $pid, 0; |
93 | return $?; |
94 | } |
95 | } |