Store the script options in the engine.
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_script_server.t
CommitLineData
60b86a55 1use strict;
2use warnings;
3
4use FindBin qw/$Bin/;
04f4497c 5use lib "$Bin/../lib";
60b86a55 6
d9d41c5a 7use File::Temp qw/ tempdir /;
2a1acc71 8use Cwd;
6ec45f37 9use Test::More;
1a3dd976 10use Try::Tiny;
60b86a55 11
12use Catalyst::Script::Server;
13
2a1acc71 14my $cwd = getcwd;
d9d41c5a 15chdir(tempdir(CLEANUP => 1));
16
f3bf2976 17my $testopts;
18
19# Test default (no opts/args behaviour)
b1bfeea6 20# Note undef for host means we bind to all interfaces.
21testOption( [ 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 28testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash(host => 'testhost')] );
29testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash(host => 'testhost')] );
f3bf2976 30
31# port -p -port --port -l --listen
aee7cdcc 32testOption( [ qw/-p 3001/ ], ['3001', undef, opthash(port => 3001)] );
33testOption( [ 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
1a3dd976 43if (try { require Starman; 1; }) {
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 49if (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
1a3dd976 55if (try { require Starman; 1; }) {
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 63testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
29b0a615 64testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
f3bf2976 65
1a3dd976 66if (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 73testRestart( ['-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
84testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
85testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
86
f3bf2976 87# restart dir -restartdirectory --rdir --restart_directory
2c298960 88testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
89testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
90testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
91
92# restart regex -rr -restartregex --rr --restart_regex
93testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
94testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
f3bf2976 95
d5a8ec3c 96local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER};
97local $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 109done_testing;
f3bf2976 110
111sub 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 136sub 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 158sub 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
168sub _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
186sub 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
199sub 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 211chdir($cwd);
212
ed7e95f2 2131;
214