Commit | Line | Data |
60b86a55 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use FindBin qw/$Bin/; |
04f4497c |
5 | use lib "$Bin/../lib"; |
60b86a55 |
6 | |
d9d41c5a |
7 | use File::Temp qw/ tempdir /; |
6ec45f37 |
8 | use Test::More; |
1a3dd976 |
9 | use Try::Tiny; |
60b86a55 |
10 | |
11 | use Catalyst::Script::Server; |
12 | |
d9d41c5a |
13 | chdir(tempdir(CLEANUP => 1)); |
14 | |
f3bf2976 |
15 | my $testopts; |
16 | |
17 | # Test default (no opts/args behaviour) |
b1bfeea6 |
18 | # Note undef for host means we bind to all interfaces. |
19 | testOption( [ qw// ], ['3000', undef, opthash()] ); |
f3bf2976 |
20 | |
21 | # Old version supports long format opts with either one or two dashes. New version only supports two. |
22 | # Old New |
fb533ac3 |
23 | # help -? -help --help -? --help |
f3bf2976 |
24 | # debug -d -debug --debug -d --debug |
25 | # host -host --host --host |
26 | testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash()] ); |
27 | testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash()] ); |
28 | |
29 | # port -p -port --port -l --listen |
b1bfeea6 |
30 | testOption( [ qw/-p 3001/ ], ['3001', undef, opthash()] ); |
31 | testOption( [ qw/--port 3001/ ], ['3001', undef, opthash()] ); |
b89d8b98 |
32 | { |
33 | local $ENV{TESTAPPTOTESTSCRIPTS_PORT} = 5000; |
34 | testOption( [ qw// ], [5000, undef, opthash()] ); |
35 | } |
56eb9db4 |
36 | { |
37 | local $ENV{CATALYST_PORT} = 5000; |
38 | testOption( [ qw// ], [5000, undef, opthash()] ); |
39 | } |
f3bf2976 |
40 | |
1a3dd976 |
41 | if (try { require Starman; 1; }) { |
42 | # fork -f -fork --fork -f --fork |
43 | testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] ); |
44 | testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] ); |
45 | } |
f3bf2976 |
46 | |
1a3dd976 |
47 | if (try { require MooseX::Daemonize; 1; }) { |
48 | # pidfile -pidfile --pid --pidfile |
49 | testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] ); |
50 | testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] ); |
51 | } |
f3bf2976 |
52 | |
1a3dd976 |
53 | if (try { require Starman; 1; }) { |
54 | # keepalive -k -keepalive --keepalive -k --keepalive |
55 | testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] ); |
56 | testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] ); |
57 | } |
f3bf2976 |
58 | |
59 | # symlinks -follow_symlinks --sym --follow_symlinks |
29b0a615 |
60 | # |
b1bfeea6 |
61 | testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] ); |
29b0a615 |
62 | testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] ); |
f3bf2976 |
63 | |
1a3dd976 |
64 | if (try { require MooseX::Daemonize; 1; }) { |
65 | # background -background --bg --background |
06208537 |
66 | testBackgroundOptionWithFork( [ qw/--background/ ]); |
67 | testBackgroundOptionWithFork( [ qw/--bg/ ]); |
1a3dd976 |
68 | } |
29b0a615 |
69 | |
f3bf2976 |
70 | # restart -r -restart --restart -R --restart |
2c298960 |
71 | testRestart( ['-r'], restartopthash() ); |
56eb9db4 |
72 | { |
73 | local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1; |
74 | testRestart( [], restartopthash() ); |
75 | } |
76 | { |
77 | local $ENV{CATALYST_RELOAD} = 1; |
78 | testRestart( [], restartopthash() ); |
79 | } |
80 | |
2c298960 |
81 | # restart dly -rd -restartdelay --rd --restart_delay |
82 | testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) ); |
83 | testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) ); |
84 | |
f3bf2976 |
85 | # restart dir -restartdirectory --rdir --restart_directory |
2c298960 |
86 | testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) ); |
87 | testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) ); |
88 | testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) ); |
89 | |
90 | # restart regex -rr -restartregex --rr --restart_regex |
91 | testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) ); |
92 | testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) ); |
f3bf2976 |
93 | |
d5a8ec3c |
94 | local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}; |
95 | local $ENV{CATALYST_RESTARTER}; |
96 | { |
97 | is _build_testapp([])->restarter_class, 'Catalyst::Restarter', 'default restarter with no $ENV{CATALYST_RESTARTER}'; |
98 | } |
99 | { |
100 | local $ENV{CATALYST_RESTARTER} = "CatalystX::Restarter::Other"; |
101 | is _build_testapp([])->restarter_class, $ENV{CATALYST_RESTARTER}, 'override restarter with $ENV{CATALYST_RESTARTER}'; |
102 | } |
103 | { |
104 | local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER} = "CatalystX::Restarter::Other2"; |
105 | is _build_testapp([])->restarter_class, $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}, 'override restarter with $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}'; |
106 | } |
6ec45f37 |
107 | done_testing; |
f3bf2976 |
108 | |
109 | sub testOption { |
110 | my ($argstring, $resultarray) = @_; |
2c298960 |
111 | my $app = _build_testapp($argstring); |
1a3dd976 |
112 | try { |
2c298960 |
113 | $app->run; |
1a3dd976 |
114 | } |
115 | catch { |
116 | fail $_; |
2c298960 |
117 | }; |
118 | # First element of RUN_ARGS will be the script name, which we don't care about |
06208537 |
119 | |
2c298960 |
120 | shift @TestAppToTestScripts::RUN_ARGS; |
b17bc48c |
121 | my $server = pop @TestAppToTestScripts::RUN_ARGS; |
122 | like ref($server), qr/^Plack::Handler/, 'Is a Plack::Handler'; |
29b0a615 |
123 | |
124 | my @run_args = @TestAppToTestScripts::RUN_ARGS; |
125 | $run_args[-1]->{pidfile} = $run_args[-1]->{pidfile}->file->stringify |
1a3dd976 |
126 | if scalar(@run_args) && $run_args[-1]->{pidfile}; |
29b0a615 |
127 | |
128 | |
30b70903 |
129 | # Mangle argv into the options.. |
130 | $resultarray->[-1]->{argv} = $argstring; |
29b0a615 |
131 | is_deeply \@run_args, $resultarray, "is_deeply comparison " . join(' ', @$argstring); |
2c298960 |
132 | } |
133 | |
06208537 |
134 | sub testBackgroundOptionWithFork { |
135 | my ($argstring) = @_; |
136 | |
137 | ## First, make sure we can get an app |
138 | my $app = _build_testapp($argstring); |
71782f3e |
139 | |
140 | ## Sorry, don't really fork since this cause trouble in Test::Aggregate |
141 | $app->meta->add_around_method_modifier('daemon_fork', sub { return; }); |
142 | |
06208537 |
143 | try { |
144 | $app->run; |
145 | } |
146 | catch { |
147 | fail $_; |
148 | }; |
149 | |
150 | ## Check a few args |
151 | is_deeply $app->{ARGV}, $argstring; |
152 | is $app->{port}, '3000'; |
153 | is($app->{background}, 1); |
154 | } |
155 | |
2c298960 |
156 | sub testRestart { |
157 | my ($argstring, $resultarray) = @_; |
158 | my $app = _build_testapp($argstring); |
56eb9db4 |
159 | ok $app->restart, 'App is in restart mode'; |
2c298960 |
160 | my $args = {$app->_restarter_args}; |
161 | is_deeply delete $args->{argv}, $argstring, 'argv is arg string'; |
162 | is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present'; |
163 | is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring); |
164 | } |
165 | |
166 | sub _build_testapp { |
167 | my ($argstring, $resultarray) = @_; |
f3bf2976 |
168 | |
6ec45f37 |
169 | local @ARGV = @$argstring; |
170 | local @TestAppToTestScripts::RUN_ARGS; |
2c298960 |
171 | my $i; |
1a3dd976 |
172 | try { |
2c298960 |
173 | $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts'); |
1a3dd976 |
174 | pass "new_with_options " . join(' ', @$argstring); |
175 | } |
176 | catch { |
177 | fail "new_with_options " . join(' ', @$argstring) . " " . $_; |
178 | }; |
2c298960 |
179 | ok $i; |
180 | return $i; |
60b86a55 |
181 | } |
182 | |
f3bf2976 |
183 | # Returns the hash expected when no flags are passed |
184 | sub opthash { |
960bf5b0 |
185 | return { |
186 | 'pidfile' => undef, |
187 | 'fork' => 0, |
188 | 'follow_symlinks' => 0, |
189 | 'background' => 0, |
190 | 'keepalive' => 0, |
191 | @_, |
192 | }; |
60b86a55 |
193 | } |
2c298960 |
194 | |
195 | sub restartopthash { |
44cf11e1 |
196 | my $opthash = opthash(@_); |
197 | my $val = { |
198 | application_name => 'TestAppToTestScripts', |
199 | port => '3000', |
200 | debug => undef, |
201 | host => undef, |
202 | %$opthash, |
2c298960 |
203 | }; |
44cf11e1 |
204 | return $val; |
2c298960 |
205 | } |
ed7e95f2 |
206 | |
207 | 1; |
208 | |