fixed some broken tests
[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
6ec45f37 7use Test::More;
60b86a55 8use Test::Exception;
9
10use Catalyst::Script::Server;
11
f3bf2976 12my $testopts;
13
14# Test default (no opts/args behaviour)
b1bfeea6 15# Note undef for host means we bind to all interfaces.
16testOption( [ 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
23testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash()] );
24testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash()] );
25
26# port -p -port --port -l --listen
b1bfeea6 27testOption( [ qw/-p 3001/ ], ['3001', undef, opthash()] );
28testOption( [ 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 39testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] );
40testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] );
f3bf2976 41
42# pidfile -pidfile --pid --pidfile
b1bfeea6 43testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
44testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
f3bf2976 45
46# keepalive -k -keepalive --keepalive -k --keepalive
b1bfeea6 47testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] );
48testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] );
f3bf2976 49
50# symlinks -follow_symlinks --sym --follow_symlinks
29b0a615 51#
b1bfeea6 52testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
29b0a615 53testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
f3bf2976 54
55# background -background --bg --background
b1bfeea6 56testOption( [ qw/--background/ ], ['3000', undef, opthash(background => 1)] );
57testOption( [ qw/--bg/ ], ['3000', undef, opthash(background => 1)] );
53c6ec79 58
29b0a615 59
f3bf2976 60# restart -r -restart --restart -R --restart
2c298960 61testRestart( ['-r'], restartopthash() );
56eb9db4 62{
63 local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1;
64 testRestart( [], restartopthash() );
65}
66{
67 local $ENV{CATALYST_RELOAD} = 1;
68 testRestart( [], restartopthash() );
69}
70
2c298960 71# restart dly -rd -restartdelay --rd --restart_delay
72testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
73testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
74
f3bf2976 75# restart dir -restartdirectory --rdir --restart_directory
2c298960 76testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
77testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
78testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
79
80# restart regex -rr -restartregex --rr --restart_regex
81testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
82testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
f3bf2976 83
d5a8ec3c 84local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER};
85local $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}
6ec45f37 97done_testing;
f3bf2976 98
99sub testOption {
100 my ($argstring, $resultarray) = @_;
2c298960 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;
b17bc48c 107 my $server = pop @TestAppToTestScripts::RUN_ARGS;
29b0a615 108
109 use Data::Dump 'dump';
110 note dump 1,$server;
111 note dump 2,@TestAppToTestScripts::RUN_ARGS;
112 note dump 3,$argstring;
113
b17bc48c 114 like ref($server), qr/^Plack::Handler/, 'Is a Plack::Handler';
29b0a615 115
116 my @run_args = @TestAppToTestScripts::RUN_ARGS;
117 $run_args[-1]->{pidfile} = $run_args[-1]->{pidfile}->file->stringify
118 if $run_args[-1]->{pidfile};
119
120
30b70903 121 # Mangle argv into the options..
122 $resultarray->[-1]->{argv} = $argstring;
29b0a615 123 is_deeply \@run_args, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
2c298960 124}
125
126sub testRestart {
127 my ($argstring, $resultarray) = @_;
128 my $app = _build_testapp($argstring);
56eb9db4 129 ok $app->restart, 'App is in restart mode';
2c298960 130 my $args = {$app->_restarter_args};
131 is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
132 is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
133 is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
134}
135
136sub _build_testapp {
137 my ($argstring, $resultarray) = @_;
f3bf2976 138
6ec45f37 139 local @ARGV = @$argstring;
140 local @TestAppToTestScripts::RUN_ARGS;
2c298960 141 my $i;
6ec45f37 142 lives_ok {
2c298960 143 $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
144 } "new_with_options " . join(' ', @$argstring);;
145 ok $i;
146 return $i;
60b86a55 147}
148
f3bf2976 149# Returns the hash expected when no flags are passed
150sub opthash {
960bf5b0 151 return {
152 'pidfile' => undef,
153 'fork' => 0,
154 'follow_symlinks' => 0,
155 'background' => 0,
156 'keepalive' => 0,
157 @_,
158 };
60b86a55 159}
2c298960 160
161sub restartopthash {
44cf11e1 162 my $opthash = opthash(@_);
163 my $val = {
164 application_name => 'TestAppToTestScripts',
165 port => '3000',
166 debug => undef,
167 host => undef,
168 %$opthash,
2c298960 169 };
44cf11e1 170 return $val;
2c298960 171}