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