]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
tests: more common JSON module loading
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 require_mods(qw(json DBD::SQLite Search::Xapian));
10 my ($home, $for_destroy) = tmpdir();
11 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
12
13 SKIP: {
14         require_mods('IO::FDPass', 51);
15         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
16         mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
17         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
18
19         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
20         is($err, '', 'no error from daemon-pid');
21         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
22         chomp(my $pid = $out);
23         ok(kill(0, $pid), 'pid is valid');
24         ok(-S $sock, 'sock created');
25
26         ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
27         is($? >> 8, 1, '$? is 1');
28         is($out, '', 'nothing in stdout');
29         like($err, qr/^usage:/sm, 'usage in stderr');
30
31         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
32                 $out = $err = '';
33                 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
34                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
35                 is($err, '', "nothing in stderr (@$arg)");
36         }
37
38         ok(!run_script([qw(lei DBG-false)], undef, $opt), 'false(1) emulation');
39         is($? >> 8, 1, '$? set correctly');
40         is($err, '', 'no error from false(1) emulation');
41
42         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
43                 $out = $err = '';
44                 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
45                 is($? >> 8, 1, '$? set correctly');
46                 isnt($err, '', 'something in stderr');
47                 is($out, '', 'nothing in stdout');
48         }
49
50         $out = '';
51         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
52         chomp(my $pid_again = $out);
53         is($pid, $pid_again, 'daemon-pid idempotent');
54
55         ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
56         is($out, '', 'no output from daemon-stop');
57         is($err, '', 'no error from daemon-stop');
58         for (0..100) {
59                 kill(0, $pid) or last;
60                 tick();
61         }
62         ok(!-S $sock, 'sock gone');
63         ok(!kill(0, $pid), 'pid gone after stop');
64
65         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
66         chomp(my $new_pid = $out);
67         ok(kill(0, $new_pid), 'new pid is running');
68         ok(-S $sock, 'sock exists again');
69         unlink $sock or BAIL_OUT "unlink $!";
70         for (0..100) {
71                 kill('CHLD', $new_pid) or last;
72                 tick();
73         }
74         ok(!kill(0, $new_pid), 'daemon exits after unlink');
75 };
76
77 require_ok 'PublicInbox::LeiDaemon';
78
79 done_testing;