Commit | Line | Data |
60b86a55 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use FindBin qw/$Bin/; |
04f4497c |
5 | use lib "$Bin/../lib"; |
60b86a55 |
6 | |
6ec45f37 |
7 | use Test::More; |
60b86a55 |
8 | use Test::Exception; |
9 | |
10 | use Catalyst::Script::Server; |
11 | |
f3bf2976 |
12 | my $testopts; |
13 | |
14 | # Test default (no opts/args behaviour) |
b1bfeea6 |
15 | # Note undef for host means we bind to all interfaces. |
16 | testOption( [ qw// ], ['3000', undef, opthash()] ); |
f3bf2976 |
17 | |
18 | # Old version supports long format opts with either one or two dashes. New version only supports two. |
19 | # Old New |
fb533ac3 |
20 | # help -? -help --help -? --help |
f3bf2976 |
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 |
b1bfeea6 |
27 | testOption( [ qw/-p 3001/ ], ['3001', undef, opthash()] ); |
28 | testOption( [ qw/--port 3001/ ], ['3001', undef, opthash()] ); |
b89d8b98 |
29 | { |
30 | local $ENV{TESTAPPTOTESTSCRIPTS_PORT} = 5000; |
31 | testOption( [ qw// ], [5000, undef, opthash()] ); |
32 | } |
56eb9db4 |
33 | { |
34 | local $ENV{CATALYST_PORT} = 5000; |
35 | testOption( [ qw// ], [5000, undef, opthash()] ); |
36 | } |
f3bf2976 |
37 | |
38 | # fork -f -fork --fork -f --fork |
b1bfeea6 |
39 | testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] ); |
40 | testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] ); |
f3bf2976 |
41 | |
42 | # pidfile -pidfile --pid --pidfile |
b1bfeea6 |
43 | testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] ); |
44 | testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] ); |
f3bf2976 |
45 | |
46 | # keepalive -k -keepalive --keepalive -k --keepalive |
b1bfeea6 |
47 | testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] ); |
48 | testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] ); |
f3bf2976 |
49 | |
50 | # symlinks -follow_symlinks --sym --follow_symlinks |
b1bfeea6 |
51 | testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] ); |
52 | testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] ); |
f3bf2976 |
53 | |
54 | # background -background --bg --background |
b1bfeea6 |
55 | testOption( [ qw/--background/ ], ['3000', undef, opthash(background => 1)] ); |
56 | testOption( [ qw/--bg/ ], ['3000', undef, opthash(background => 1)] ); |
53c6ec79 |
57 | |
f3bf2976 |
58 | # restart -r -restart --restart -R --restart |
2c298960 |
59 | testRestart( ['-r'], restartopthash() ); |
56eb9db4 |
60 | { |
61 | local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1; |
62 | testRestart( [], restartopthash() ); |
63 | } |
64 | { |
65 | local $ENV{CATALYST_RELOAD} = 1; |
66 | testRestart( [], restartopthash() ); |
67 | } |
68 | |
2c298960 |
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 | |
f3bf2976 |
73 | # restart dir -restartdirectory --rdir --restart_directory |
2c298960 |
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/) ); |
f3bf2976 |
81 | |
d5a8ec3c |
82 | local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}; |
83 | local $ENV{CATALYST_RESTARTER}; |
84 | { |
85 | is _build_testapp([])->restarter_class, 'Catalyst::Restarter', 'default restarter with no $ENV{CATALYST_RESTARTER}'; |
86 | } |
87 | { |
88 | local $ENV{CATALYST_RESTARTER} = "CatalystX::Restarter::Other"; |
89 | is _build_testapp([])->restarter_class, $ENV{CATALYST_RESTARTER}, 'override restarter with $ENV{CATALYST_RESTARTER}'; |
90 | } |
91 | { |
92 | local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER} = "CatalystX::Restarter::Other2"; |
93 | is _build_testapp([])->restarter_class, $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}, 'override restarter with $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}'; |
94 | } |
6ec45f37 |
95 | done_testing; |
f3bf2976 |
96 | |
97 | sub testOption { |
98 | my ($argstring, $resultarray) = @_; |
2c298960 |
99 | my $app = _build_testapp($argstring); |
100 | lives_ok { |
101 | $app->run; |
102 | }; |
103 | # First element of RUN_ARGS will be the script name, which we don't care about |
104 | shift @TestAppToTestScripts::RUN_ARGS; |
30b70903 |
105 | # Mangle argv into the options.. |
106 | $resultarray->[-1]->{argv} = $argstring; |
2c298960 |
107 | is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison " . join(' ', @$argstring); |
108 | } |
109 | |
110 | sub testRestart { |
111 | my ($argstring, $resultarray) = @_; |
112 | my $app = _build_testapp($argstring); |
56eb9db4 |
113 | ok $app->restart, 'App is in restart mode'; |
2c298960 |
114 | my $args = {$app->_restarter_args}; |
115 | is_deeply delete $args->{argv}, $argstring, 'argv is arg string'; |
116 | is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present'; |
117 | is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring); |
118 | } |
119 | |
120 | sub _build_testapp { |
121 | my ($argstring, $resultarray) = @_; |
f3bf2976 |
122 | |
6ec45f37 |
123 | local @ARGV = @$argstring; |
124 | local @TestAppToTestScripts::RUN_ARGS; |
2c298960 |
125 | my $i; |
6ec45f37 |
126 | lives_ok { |
2c298960 |
127 | $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts'); |
128 | } "new_with_options " . join(' ', @$argstring);; |
129 | ok $i; |
130 | return $i; |
60b86a55 |
131 | } |
132 | |
f3bf2976 |
133 | # Returns the hash expected when no flags are passed |
134 | sub opthash { |
960bf5b0 |
135 | return { |
136 | 'pidfile' => undef, |
137 | 'fork' => 0, |
138 | 'follow_symlinks' => 0, |
139 | 'background' => 0, |
140 | 'keepalive' => 0, |
141 | @_, |
142 | }; |
60b86a55 |
143 | } |
2c298960 |
144 | |
145 | sub restartopthash { |
44cf11e1 |
146 | my $opthash = opthash(@_); |
147 | my $val = { |
148 | application_name => 'TestAppToTestScripts', |
149 | port => '3000', |
150 | debug => undef, |
151 | host => undef, |
152 | %$opthash, |
2c298960 |
153 | }; |
44cf11e1 |
154 | return $val; |
2c298960 |
155 | } |