Merge branch 'master' into psgi
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_server.t
1 use strict;
2 use warnings;
3
4 use FindBin qw/$Bin/;
5 use lib "$Bin/../lib";
6
7 use Test::More;
8 use Test::Exception;
9
10 use Catalyst::Script::Server;
11
12 my $testopts;
13
14 # Test default (no opts/args behaviour)
15 # Note undef for host means we bind to all interfaces.
16 testOption( [ qw// ], ['3000', undef, opthash()] );
17
18 # Old version supports long format opts with either one or two dashes.  New version only supports two.
19 #                Old                       New
20 # help           -? -help --help           -? --help
21 # debug          -d -debug --debug         -d --debug
22 # host           -host --host              --host
23 testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash()] );
24 testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash()] );
25
26 # port           -p -port --port           -l --listen
27 testOption( [ qw/-p 3001/ ], ['3001', undef, opthash()] );
28 testOption( [ qw/--port 3001/ ], ['3001', undef, opthash()] );
29 {
30     local $ENV{TESTAPPTOTESTSCRIPTS_PORT} = 5000;
31     testOption( [ qw// ], [5000, undef, opthash()] );
32 }
33 {
34     local $ENV{CATALYST_PORT} = 5000;
35     testOption( [ qw// ], [5000, undef, opthash()] );
36 }
37
38 # fork           -f -fork --fork           -f --fork
39 testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] );
40 testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] );
41
42 # pidfile        -pidfile                  --pid --pidfile
43 testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
44 testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
45
46 # keepalive      -k -keepalive --keepalive -k --keepalive
47 testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] );
48 testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] );
49
50 # symlinks       -follow_symlinks          --sym --follow_symlinks
51 testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
52 testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
53
54 # background     -background               --bg --background
55 testOption( [ qw/--background/ ], ['3000', undef, opthash(background => 1)] );
56 testOption( [ qw/--bg/ ], ['3000', undef, opthash(background => 1)] );
57
58 # restart        -r -restart --restart     -R --restart
59 testRestart( ['-r'], restartopthash() );
60 {
61     local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1;
62     testRestart( [], restartopthash() );
63 }
64 {
65     local $ENV{CATALYST_RELOAD} = 1;
66     testRestart( [], restartopthash() );
67 }
68
69 # restart dly    -rd -restartdelay         --rd --restart_delay
70 testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
71 testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
72
73 # restart dir    -restartdirectory         --rdir --restart_directory
74 testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
75 testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
76 testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
77
78 # restart regex  -rr -restartregex         --rr --restart_regex
79 testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
80 testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
81
82 done_testing;
83
84 sub testOption {
85     my ($argstring, $resultarray) = @_;
86     my $app = _build_testapp($argstring);
87     lives_ok {
88         $app->run;
89     };
90     # First element of RUN_ARGS will be the script name, which we don't care about
91     shift @TestAppToTestScripts::RUN_ARGS;
92     my $server = shift @TestAppToTestScripts::RUN_ARGS;
93     like ref($server), qr/^Plack::Server/, 'Is a Plack Server';
94     # Mangle argv into the options..
95     $resultarray->[-1]->{argv} = $argstring;
96     is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
97 }
98
99 sub testRestart {
100     my ($argstring, $resultarray) = @_;
101     my $app = _build_testapp($argstring);
102     ok $app->restart, 'App is in restart mode';
103     my $args = {$app->_restarter_args};
104     is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
105     is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
106     is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
107 }
108
109 sub _build_testapp {
110     my ($argstring, $resultarray) = @_;
111
112     local @ARGV = @$argstring;
113     local @TestAppToTestScripts::RUN_ARGS;
114     my $i;
115     lives_ok {
116         $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
117     } "new_with_options " . join(' ', @$argstring);;
118     ok $i;
119     return $i;
120 }
121
122 # Returns the hash expected when no flags are passed
123 sub opthash {
124     return {
125         'pidfile' => undef,
126         'fork' => 0,
127         'follow_symlinks' => 0,
128         'background' => 0,
129         'keepalive' => 0,
130         @_,
131     };
132 }
133
134 sub restartopthash {
135     return {
136         follow_symlinks => 0,
137         @_,
138     };
139 }