update dependency to Ruby 2.5+
[unicorn.git] / t / working_directory.t
blobf9254eb827948bb782fc7607ff57b324702a98e9
1 #!perl -w
2 # Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 use v5.14; BEGIN { require './t/lib.perl' };
5 use autodie;
6 mkdir "$tmpdir/alt";
7 my $ru = "$tmpdir/alt/config.ru";
8 open my $fh, '>', $u_conf;
9 print $fh <<EOM;
10 pid "$pid_file"
11 preload_app true
12 stderr_path "$err_log"
13 working_directory "$tmpdir/alt" # the whole point of this test
14 before_fork { |_,_| \$master_ppid = Process.ppid }
15 EOM
16 close $fh;
18 my $common_ru = <<'EOM';
19 use Rack::ContentLength
20 use Rack::ContentType, 'text/plain'
21 run lambda { |env| [ 200, {}, [ "#{$master_ppid}\n" ] ] }
22 EOM
24 open $fh, '>', $ru;
25 print $fh <<EOM;
26 #\\--daemonize --listen $u_sock
27 $common_ru
28 EOM
29 close $fh;
31 unicorn('-c', $u_conf)->join; # will daemonize
32 chomp($daemon_pid = slurp($pid_file));
34 my ($status, $hdr, $bdy) = do_req($u_sock, 'GET / HTTP/1.0');
35 is($bdy, "1\n", 'got expected $master_ppid');
37 stop_daemon;
38 check_stderr;
40 if ('test without CLI switches in config.ru') {
41         truncate $err_log, 0;
42         open $fh, '>', $ru;
43         print $fh $common_ru;
44         close $fh;
46         unicorn('-D', '-l', $u_sock, '-c', $u_conf)->join; # will daemonize
47         chomp($daemon_pid = slurp($pid_file));
49         ($status, $hdr, $bdy) = do_req($u_sock, 'GET / HTTP/1.0');
50         is($bdy, "1\n", 'got expected $master_ppid');
52         stop_daemon;
53         check_stderr;
56 if ('ensures broken working_directory (missing config.ru) is OK') {
57         truncate $err_log, 0;
58         unlink $ru;
60         my $auto_reap = unicorn('-c', $u_conf);
61         $auto_reap->join;
62         isnt($?, 0, 'exited with error due to missing config.ru');
64         like(slurp($err_log), qr/rackup file \Q(config.ru)\E not readable/,
65                 'noted unreadability of config.ru in stderr');
68 if ('fooapp.rb (not config.ru) works with working_directory') {
69         truncate $err_log, 0;
70         my $fooapp = "$tmpdir/alt/fooapp.rb";
71         open $fh, '>', $fooapp;
72         print $fh <<EOM;
73 class Fooapp
74   def self.call(env)
75     b = "dir=#{Dir.pwd}"
76     h = { 'content-type' => 'text/plain', 'content-length' => b.bytesize.to_s }
77     [ 200, h, [ b ] ]
78   end
79 end
80 EOM
81         close $fh;
82         my $srv = tcp_server;
83         my $auto_reap = unicorn(qw(-c), $u_conf, qw(-I. fooapp.rb),
84                                 { -C => '/', 3 => $srv });
85         ($status, $hdr, $bdy) = do_req($srv, 'GET / HTTP/1.0');
86         is($bdy, "dir=$tmpdir/alt",
87                 'fooapp.rb (w/o config.ru) w/ working_directory');
88         $auto_reap->join('TERM');
89         is($?, 0, 'fooapp.rb process exited');
90         check_stderr;
93 undef $tmpdir;
94 done_testing;