removed debugging stuff
[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 #
52 testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
53 testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
54
55 # background     -background               --bg --background
56 testOption( [ qw/--background/ ], ['3000', undef, opthash(background => 1)] );
57 testOption( [ qw/--bg/ ], ['3000', undef, opthash(background => 1)] );
58
59
60 # restart        -r -restart --restart     -R --restart
61 testRestart( ['-r'], restartopthash() );
62 {
63     local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1;
64     testRestart( [], restartopthash() );
65 }
66 {
67     local $ENV{CATALYST_RELOAD} = 1;
68     testRestart( [], restartopthash() );
69 }
70
71 # restart dly    -rd -restartdelay         --rd --restart_delay
72 testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
73 testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
74
75 # restart dir    -restartdirectory         --rdir --restart_directory
76 testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
77 testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
78 testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
79
80 # restart regex  -rr -restartregex         --rr --restart_regex
81 testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
82 testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
83
84 local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER};
85 local $ENV{CATALYST_RESTARTER};
86 {
87     is _build_testapp([])->restarter_class, 'Catalyst::Restarter', 'default restarter with no $ENV{CATALYST_RESTARTER}';
88 }
89 {
90     local $ENV{CATALYST_RESTARTER} = "CatalystX::Restarter::Other";
91     is _build_testapp([])->restarter_class, $ENV{CATALYST_RESTARTER}, 'override restarter with $ENV{CATALYST_RESTARTER}';
92 }
93 {
94     local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER} = "CatalystX::Restarter::Other2";
95     is _build_testapp([])->restarter_class, $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}, 'override restarter with $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}';
96 }
97 done_testing;
98
99 sub testOption {
100     my ($argstring, $resultarray) = @_;
101     my $app = _build_testapp($argstring);
102     lives_ok {
103         $app->run;
104     };
105     # First element of RUN_ARGS will be the script name, which we don't care about
106     shift @TestAppToTestScripts::RUN_ARGS;
107     my $server = pop @TestAppToTestScripts::RUN_ARGS;
108     like ref($server), qr/^Plack::Handler/, 'Is a Plack::Handler';
109
110     my @run_args =  @TestAppToTestScripts::RUN_ARGS;
111     $run_args[-1]->{pidfile} = $run_args[-1]->{pidfile}->file->stringify
112       if $run_args[-1]->{pidfile};
113
114
115     # Mangle argv into the options..
116     $resultarray->[-1]->{argv} = $argstring;
117     is_deeply \@run_args, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
118 }
119
120 sub testRestart {
121     my ($argstring, $resultarray) = @_;
122     my $app = _build_testapp($argstring);
123     ok $app->restart, 'App is in restart mode';
124     my $args = {$app->_restarter_args};
125     is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
126     is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
127     is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
128 }
129
130 sub _build_testapp {
131     my ($argstring, $resultarray) = @_;
132
133     local @ARGV = @$argstring;
134     local @TestAppToTestScripts::RUN_ARGS;
135     my $i;
136     lives_ok {
137         $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
138     } "new_with_options " . join(' ', @$argstring);;
139     ok $i;
140     return $i;
141 }
142
143 # Returns the hash expected when no flags are passed
144 sub opthash {
145     return {
146         'pidfile' => undef,
147         'fork' => 0,
148         'follow_symlinks' => 0,
149         'background' => 0,
150         'keepalive' => 0,
151         @_,
152     };
153 }
154
155 sub restartopthash {
156     my $opthash = opthash(@_);
157     my $val = {
158         application_name => 'TestAppToTestScripts',
159         port => '3000',
160         debug => undef,
161         host => undef,
162         %$opthash,
163     };
164     return $val;
165 }