Fixes port environment, RT#52604
[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}
f3bf2976 33
34# fork -f -fork --fork -f --fork
b1bfeea6 35testOption( [ qw/--fork/ ], ['3000', undef, opthash(fork => 1)] );
36testOption( [ qw/-f/ ], ['3000', undef, opthash(fork => 1)] );
f3bf2976 37
38# pidfile -pidfile --pid --pidfile
b1bfeea6 39testOption( [ qw/--pidfile cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
40testOption( [ qw/--pid cat.pid/ ], ['3000', undef, opthash(pidfile => "cat.pid")] );
f3bf2976 41
42# keepalive -k -keepalive --keepalive -k --keepalive
b1bfeea6 43testOption( [ qw/-k/ ], ['3000', undef, opthash(keepalive => 1)] );
44testOption( [ qw/--keepalive/ ], ['3000', undef, opthash(keepalive => 1)] );
f3bf2976 45
46# symlinks -follow_symlinks --sym --follow_symlinks
b1bfeea6 47testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
48testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
f3bf2976 49
50# background -background --bg --background
b1bfeea6 51testOption( [ qw/--background/ ], ['3000', undef, opthash(background => 1)] );
52testOption( [ qw/--bg/ ], ['3000', undef, opthash(background => 1)] );
53c6ec79 53
f3bf2976 54# restart -r -restart --restart -R --restart
2c298960 55testRestart( ['-r'], restartopthash() );
56# restart dly -rd -restartdelay --rd --restart_delay
57testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
58testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
59
f3bf2976 60# restart dir -restartdirectory --rdir --restart_directory
2c298960 61testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
62testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
63testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
64
65# restart regex -rr -restartregex --rr --restart_regex
66testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
67testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
f3bf2976 68
6ec45f37 69done_testing;
f3bf2976 70
71sub testOption {
72 my ($argstring, $resultarray) = @_;
2c298960 73 my $app = _build_testapp($argstring);
74 lives_ok {
75 $app->run;
76 };
77 # First element of RUN_ARGS will be the script name, which we don't care about
78 shift @TestAppToTestScripts::RUN_ARGS;
79 is_deeply \@TestAppToTestScripts::RUN_ARGS, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
80}
81
82sub testRestart {
83 my ($argstring, $resultarray) = @_;
84 my $app = _build_testapp($argstring);
85 my $args = {$app->_restarter_args};
86 is_deeply delete $args->{argv}, $argstring, 'argv is arg string';
87 is ref(delete $args->{start_sub}), 'CODE', 'Closure to start app present';
88 is_deeply $args, $resultarray, "is_deeply comparison of restarter args " . join(' ', @$argstring);
89}
90
91sub _build_testapp {
92 my ($argstring, $resultarray) = @_;
f3bf2976 93
6ec45f37 94 local @ARGV = @$argstring;
95 local @TestAppToTestScripts::RUN_ARGS;
2c298960 96 my $i;
6ec45f37 97 lives_ok {
2c298960 98 $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
99 } "new_with_options " . join(' ', @$argstring);;
100 ok $i;
101 return $i;
60b86a55 102}
103
f3bf2976 104# Returns the hash expected when no flags are passed
105sub opthash {
960bf5b0 106 return {
107 'pidfile' => undef,
108 'fork' => 0,
109 'follow_symlinks' => 0,
110 'background' => 0,
111 'keepalive' => 0,
112 @_,
113 };
60b86a55 114}
2c298960 115
116sub restartopthash {
117 return {
34be7d45 118 follow_symlinks => 0,
2c298960 119 @_,
120 };
121}