File: http-server.t

package info (click to toggle)
libcatalyst-perl 5.90124-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,992 kB
  • sloc: perl: 11,123; makefile: 7
file content (121 lines) | stat: -rw-r--r-- 2,926 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use strict;
use warnings;

use Test::More tests => 1;
use Test::TCP;

use File::Path;
use FindBin;
use Net::EmptyPort qw(wait_port empty_port);
use Try::Tiny;
use Plack::Builder;

eval { require Catalyst::Devel; Catalyst::Devel->VERSION(1.0); 1; } || do {
    fail("Could not load Catalyst::Devel: $@");
    exit 1;
};

eval { require File::Copy::Recursive; 1 } || do {
    fail("Could not load File::Copy::Recursive: $@");
    exit 1;
};

# Run a single test by providing it as the first arg
my $single_test = shift;

my $tmpdir = "$FindBin::Bin/../../t/tmp";

# clean up
rmtree $tmpdir if -d $tmpdir;

# create a TestApp and copy the test libs into it
mkdir $tmpdir;
chdir $tmpdir;
system( $^X, "-I$FindBin::Bin/../../lib", "$FindBin::Bin/../../script/catalyst.pl", 'TestApp' );
chdir "$FindBin::Bin/..";
File::Copy::Recursive::dircopy( '../t/lib', '../t/tmp/TestApp/lib' ) or die;

# remove TestApp's tests
rmtree '../t/tmp/TestApp/t' or die;

# spawn the standalone HTTP server
my $port = empty_port;

my $pid = fork;
if ($pid) {
    # parent.
    print "Waiting for server to start...\n";
    wait_port_timeout($port, 30);
} elsif ($pid == 0) {
    # child process
    unshift @INC, "$tmpdir/TestApp/lib", "$FindBin::Bin/../../lib";
    require TestApp;

    my $psgi_app = TestApp->apply_default_middlewares(TestApp->psgi_app);
    Plack::Loader->auto(port => $port)->run(builder {
        mount '/test_prefix' => $psgi_app;
        mount '/' => sub {
            return [501, ['Content-Type' => 'text/plain'], ['broken tests']];
        };
    });

    exit 0;
} else {
    die "fork failed: $!";
}

# run the testsuite against the HTTP server
$ENV{CATALYST_SERVER} = "http://localhost:$port/test_prefix";

chdir '..';

my $return;
if ( $single_test ) {
    $return = system( "$^X -Ilib/ $single_test" );
}
else {
    $return = prove(grep { $_ ne '..' } glob('t/aggregate/live_*.t'));
}

# shut it down
kill 'INT', $pid;

# clean up
rmtree "$FindBin::Bin/../../t/tmp" if -d "$FindBin::Bin/../../t/tmp";

is( $return, 0, 'live tests' );

# kill 'INT' doesn't exist in Windows, so to prevent child hanging,
# this process will need to commit seppuku to clean up the children.
if ($^O eq 'MSWin32') {
    # Furthermore, it needs to do it 'politely' so that TAP doesn't
    # smell anything 'dubious'.
    require Win32::Process;  # core in all versions of Win32 Perl
    Win32::Process::KillProcess($$, $return);
}

sub wait_port_timeout {
    my ($port, $timeout) = @_;

    wait_port($port, $timeout * 10) and return;

    die "Server did not start within $timeout seconds";
}

sub prove {
    my (@tests) = @_;
    if (!(my $pid = fork)) {
        require TAP::Harness;

        my $harness = TAP::Harness->new({
            lib => ['lib'],
        });

        my $aggregator = $harness->runtests(@tests);

        exit $aggregator->has_errors ? 1 : 0;
    } else {
        waitpid $pid, 0;
        return $?;
    }
}