Fix typo, bricas++, add simple test for restarter arguments
[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 testOption( [ qw// ], ['3000', 'localhost', opthash()] );
16
17 # Old version supports long format opts with either one or two dashes.  New version only supports two.
18 #                Old                       New
19 # help           -? -help --help           -? --help
20 # debug          -d -debug --debug         -d --debug
21 # host           -host --host              --host
22 testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash()] );
23 testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash()] );
24
25 # port           -p -port --port           -l --listen
26 testOption( [ qw/-p 3001/ ], ['3001', 'localhost', opthash()] );
27 testOption( [ qw/--port 3001/ ], ['3001', 'localhost', opthash()] );
28
29 # fork           -f -fork --fork           -f --fork
30 testOption( [ qw/--fork/ ], ['3000', 'localhost', opthash(fork => 1)] );
31 testOption( [ qw/-f/ ], ['3000', 'localhost', opthash(fork => 1)] );
32
33 # pidfile        -pidfile                  --pid --pidfile
34 testOption( [ qw/--pidfile cat.pid/ ], ['3000', 'localhost', opthash(pidfile => "cat.pid")] );
35 testOption( [ qw/--pid cat.pid/ ], ['3000', 'localhost', opthash(pidfile => "cat.pid")] );
36
37 # keepalive      -k -keepalive --keepalive -k --keepalive
38 testOption( [ qw/-k/ ], ['3000', 'localhost', opthash(keepalive => 1)] );
39 testOption( [ qw/--keepalive/ ], ['3000', 'localhost', opthash(keepalive => 1)] );
40
41 # symlinks       -follow_symlinks          --sym --follow_symlinks
42 testOption( [ qw/--follow_symlinks/ ], ['3000', 'localhost', opthash(follow_symlinks => 1)] );
43 testOption( [ qw/--sym/ ], ['3000', 'localhost', opthash(follow_symlinks => 1)] );
44
45 # background     -background               --bg --background
46 testOption( [ qw/--background/ ], ['3000', 'localhost', opthash(background => 1)] );
47 testOption( [ qw/--bg/ ], ['3000', 'localhost', opthash(background => 1)] );
48
49 # restart        -r -restart --restart     -R --restart
50 testRestart( ['-r'], restartopthash() );
51 # restart dly    -rd -restartdelay         --rd --restart_delay
52 testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
53 testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
54
55 # restart dir    -restartdirectory         --rdir --restart_directory
56 testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
57 testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
58 testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
59
60 # restart regex  -rr -restartregex         --rr --restart_regex
61 testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
62 testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
63
64 done_testing;
65
66 sub testOption {
67     my ($argstring, $resultarray) = @_;
68     my $app = _build_testapp($argstring);
69     lives_ok {
70         $app->run;
71     };
72     # First element of RUN_ARGS will be the script name, which we don't care about
73     shift @TestAppToTestScripts::RUN_ARGS;
74     is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
75 }
76
77 sub testRestart {
78     my ($argstring, $resultarray) = @_;
79     my $app = _build_testapp($argstring);
80     my $args = {$app->_restarter_args};
81     is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
82     is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
83     is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
84 }
85
86 sub _build_testapp {
87     my ($argstring, $resultarray) = @_;
88
89     local @ARGV = @$argstring;
90     local @TestAppToTestScripts::RUN_ARGS;
91     my $i;
92     lives_ok {
93         $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
94     } "new_with_options " . join(' ', @$argstring);;
95     ok $i;
96     return $i;
97 }
98
99 # Returns the hash expected when no flags are passed
100 sub opthash {
101     return {
102         'pidfile' => undef,
103         'fork' => 0,
104         'follow_symlinks' => 0,
105         'background' => 0,
106         'keepalive' => 0,
107         @_,
108     };
109 }
110
111 sub restartopthash {
112     return {
113         @_,
114     };
115 }