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