- $out = '';
- ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
- chomp(my $pid_again = $out);
- is($pid, $pid_again, 'daemon-pid idempotent');
-
- $out = '';
- ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
- is($err, '', 'no errors in env dump');
- my @env = split(/\0/, $out);
- is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
- is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
- is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
-
- $out = '';
- ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
- is($out.$err, '', 'no output for unset');
- ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
- is($err, '', 'no errors in env dump');
- @env = split(/\0/, $out);
- is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
-
- $out = '';
- ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
- undef, $opt), 'unset multiple');
- is($out.$err, '', 'no errors output for unset');
- ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
- is($err, '', 'no errors in env dump');
- @env = split(/\0/, $out);
- is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
- $out = '';
- ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
- is($out.$err, '', 'no output');
- ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
- is($out, '', 'env cleared');
-
- ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
- is($out, '', 'no output from daemon-kill');
- is($err, '', 'no error from daemon-kill');
- for (0..100) {
- kill(0, $pid) or last;
- tick();
- }
- ok(!-S $sock, 'sock gone');
- ok(!kill(0, $pid), 'pid gone after stop');
-
- ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
- chomp(my $new_pid = $out);
- ok(kill(0, $new_pid), 'new pid is running');
- ok(-S $sock, 'sock exists again');
-
- $out = $err = '';
- for my $sig (qw(-0 -CHLD)) {
- ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
- "handles $sig");
- }
- is($out.$err, '', 'no output on innocuous signals');
- ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
- chomp $out;
- is($out, $new_pid, 'PID unchanged after -0/-CHLD');
-
- if ('socket inaccessible') {
- chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
- $out = $err = '';
- ok(run_script([qw(lei help)], undef, $opt),
- 'connect fail, one-shot fallback works');
- like($err, qr/\bconnect\(/, 'connect error noted');
- like($out, qr/^usage: /, 'help output works');
- chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
- }
- if ('oneshot on cwd gone') {
- my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
- my $d = "$home/to-be-removed";
- my $lei_path = 'lei';
- # we chdir, so we need an abs_path fur run_script
- if (($ENV{TEST_RUN_MODE}//2) != 2) {
- $lei_path = PublicInbox::TestCommon::key2script('lei');
- $lei_path = Cwd::abs_path($lei_path);
- }
- mkdir $d or BAIL_OUT "mkdir($d) $!";
- chdir $d or BAIL_OUT "chdir($d) $!";
- if (rmdir($d)) {
- $out = $err = '';
- ok(run_script([$lei_path, 'help'], undef, $opt),
- 'cwd fail, one-shot fallback works');
- } else {
- $err = "rmdir=$!";
- }
- chdir $cwd or BAIL_OUT "chdir($cwd) $!";
- like($err, qr/cwd\(/, 'cwd error noted');
- like($out, qr/^usage: /, 'help output still works');
- }
-
- unlink $sock or BAIL_OUT "unlink($sock) $!";
- for (0..100) {
- kill('CHLD', $new_pid) or last;
- tick();
- }
- ok(!kill(0, $new_pid), 'daemon exits after unlink');
- # success over socket, can't test without
-};