7 use File::Temp qw/ tempdir /;
12 use Catalyst::Script::Server;
15 chdir(tempdir(CLEANUP => 1));
19 # Test default (no opts/args behaviour)
20 # Note undef for host means we bind to all interfaces.
21 testOption( [ qw// ], ['3000', undef, opthash()] );
23 # Old version supports long format opts with either one or two dashes. New version only supports two.
25 # help -? -help --help -? --help
26 # debug -d -debug --debug -d --debug
27 # host -host --host --host
28 testOption( [ qw/--host testhost/ ], ['3000', 'testhost', opthash(host => 'testhost')] );
29 testOption( [ qw/-h testhost/ ], ['3000', 'testhost', opthash(host => 'testhost')] );
31 # port -p -port --port -l --listen
32 testOption( [ qw/-p 3001/ ], ['3001', undef, opthash(port => 3001)] );
33 testOption( [ qw/--port 3001/ ], ['3001', undef, opthash(port => 3001)] );
35 local $ENV{TESTAPPTOTESTSCRIPTS_PORT} = 5000;
36 testOption( [ qw// ], [5000, undef, opthash(port => 5000)] );
39 local $ENV{CATALYST_PORT} = 5000;
40 testOption( [ qw// ], [5000, undef, opthash(port => 5000)] );
43 if (try { require Plack::Handler::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)] );
49 if (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")] );
55 if (try { require Plack::Handler::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)] );
61 # symlinks -follow_symlinks --sym --follow_symlinks
63 testOption( [ qw/--sym/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
64 testOption( [ qw/--follow_symlinks/ ], ['3000', undef, opthash(follow_symlinks => 1)] );
66 if (try { require MooseX::Daemonize; 1; }) {
67 # background -background --bg --background
68 testBackgroundOptionWithFork( [ qw/--background/ ]);
69 testBackgroundOptionWithFork( [ qw/--bg/ ]);
72 # restart -r -restart --restart -R --restart
73 testRestart( ['-r'], restartopthash() );
75 local $ENV{TESTAPPTOTESTSCRIPTS_RELOAD} = 1;
76 testRestart( [], restartopthash() );
79 local $ENV{CATALYST_RELOAD} = 1;
80 testRestart( [], restartopthash() );
83 # restart dly -rd -restartdelay --rd --restart_delay
84 testRestart( ['-r', '--rd', 30], restartopthash(sleep_interval => 30) );
85 testRestart( ['-r', '--restart_delay', 30], restartopthash(sleep_interval => 30) );
87 # restart dir -restartdirectory --rdir --restart_directory
88 testRestart( ['-r', '--rdir', 'root'], restartopthash(directories => ['root']) );
89 testRestart( ['-r', '--rdir', 'root', '--rdir', 'lib'], restartopthash(directories => ['root', 'lib']) );
90 testRestart( ['-r', '--restart_directory', 'root'], restartopthash(directories => ['root']) );
92 # restart regex -rr -restartregex --rr --restart_regex
93 testRestart( ['-r', '--rr', 'foo'], restartopthash(filter => qr/foo/) );
94 testRestart( ['-r', '--restart_regex', 'foo'], restartopthash(filter => qr/foo/) );
96 local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER};
97 local $ENV{CATALYST_RESTARTER};
99 is _build_testapp([])->restarter_class, 'Catalyst::Restarter', 'default restarter with no $ENV{CATALYST_RESTARTER}';
102 local $ENV{CATALYST_RESTARTER} = "CatalystX::Restarter::Other";
103 is _build_testapp([])->restarter_class, $ENV{CATALYST_RESTARTER}, 'override restarter with $ENV{CATALYST_RESTARTER}';
106 local $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER} = "CatalystX::Restarter::Other2";
107 is _build_testapp([])->restarter_class, $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}, 'override restarter with $ENV{TESTAPPTOTESTSCRIPTS_RESTARTER}';
112 my ($argstring, $resultarray) = @_;
113 my $app = _build_testapp($argstring);
120 # First element of RUN_ARGS will be the script name, which we don't care about
122 shift @TestAppToTestScripts::RUN_ARGS;
123 my $server = pop @TestAppToTestScripts::RUN_ARGS;
124 like ref($server), qr/^Plack::Handler/, 'Is a Plack::Handler';
126 my @run_args = @TestAppToTestScripts::RUN_ARGS;
127 $run_args[-1]->{pidfile} = $run_args[-1]->{pidfile}->file->stringify
128 if scalar(@run_args) && $run_args[-1]->{pidfile};
130 # Mangle argv into the options..
131 $resultarray->[-1]->{argv} = $argstring;
132 $resultarray->[-1]->{extra_argv} = [];
133 is_deeply \@run_args, $resultarray, "is_deeply comparison " . join(' ', @$argstring);
136 sub testBackgroundOptionWithFork {
137 my ($argstring) = @_;
139 ## First, make sure we can get an app
140 my $app = _build_testapp($argstring);
142 ## Sorry, don't really fork since this cause trouble in Test::Aggregate
143 $app->meta->add_around_method_modifier('daemon_fork', sub { return; });
153 is_deeply $app->{ARGV}, $argstring;
154 is $app->port, '3000';
155 is($app->{background}, 1);
159 my ($argstring, $resultarray) = @_;
160 my $app = _build_testapp($argstring);
161 ok $app->restart, 'App is in restart mode';
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);
169 my ($argstring, $resultarray) = @_;
171 local @ARGV = @$argstring;
172 local @TestAppToTestScripts::RUN_ARGS;
175 $i = Catalyst::Script::Server->new_with_options(application_name => 'TestAppToTestScripts');
176 pass "new_with_options " . join(' ', @$argstring);
179 fail "new_with_options " . join(' ', @$argstring) . " " . $_;
185 # Returns the hash expected when no flags are passed
190 'follow_symlinks' => 0,
200 my $opthash = opthash(@_);
202 application_name => 'TestAppToTestScripts',