]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: refine help/option parsing, implement "init"
[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 use File::Path qw(rmtree);
10 require_mods(qw(json DBD::SQLite Search::Xapian));
11 my ($home, $for_destroy) = tmpdir();
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 delete local $ENV{XDG_DATA_HOME};
14 delete local $ENV{XDG_CONFIG_HOME};
15 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
16 local $ENV{HOME} = $home;
17 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
18
19 my $test_lei_common = sub {
20         ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
21         is($? >> 8, 1, '$? is 1');
22         is($out, '', 'nothing in stdout');
23         like($err, qr/^usage:/sm, 'usage in stderr');
24
25         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
26                 $out = $err = '';
27                 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
28                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
29                 is($err, '', "nothing in stderr (@$arg)");
30         }
31
32         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
33                 $out = $err = '';
34                 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
35                 is($? >> 8, 1, '$? set correctly');
36                 isnt($err, '', 'something in stderr');
37                 is($out, '', 'nothing in stdout');
38         }
39
40         # init tests
41         $out = $err = '';
42         my $ok_err_info = sub {
43                 my ($msg) = @_;
44                 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
45                         diag "$msg: err=$err";
46                 $err = '';
47         };
48         my $home_trash = [ "$home/.local", "$home/.config" ];
49         rmtree($home_trash);
50         ok(run_script([qw(lei init)], undef, $opt), 'init w/o args');
51         $ok_err_info->('after init w/o args');
52         ok(run_script([qw(lei init)], undef, $opt), 'idempotent init w/o args');
53         $ok_err_info->('after idempotent init w/o args');
54
55         ok(!run_script([qw(lei init), "$home/x"], undef, $opt),
56                 'init conflict');
57         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
58         ok(!-e "$home/x", 'nothing created on conflict');
59         rmtree($home_trash);
60
61         $err = '';
62         ok(run_script([qw(lei init), "$home/x"], undef, $opt),
63                 'init conflict resolved');
64         $ok_err_info->('init w/ arg');
65         ok(run_script([qw(lei init), "$home/x"], undef, $opt),
66                 'init idempotent with path');
67         $ok_err_info->('init idempotent w/ arg');
68         ok(-d "$home/x", 'created dir');
69         rmtree([ "$home/x", @$home_trash ]);
70
71         $err = '';
72         ok(!run_script([qw(lei init), "$home/x", "$home/2" ], undef, $opt),
73                 'too many args fails');
74         like($err, qr/too many/, 'noted excessive');
75         ok(!-e "$home/x", 'x not created on excessive');
76         for my $d (@$home_trash) {
77                 my $base = (split(m!/!, $d))[-1];
78                 ok(!-d $d, "$base not created");
79         }
80         is($out, '', 'nothing in stdout');
81 };
82
83 SKIP: {
84         require_mods('IO::FDPass', 16);
85         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
86
87         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
88         is($err, '', 'no error from daemon-pid');
89         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
90         chomp(my $pid = $out);
91         ok(kill(0, $pid), 'pid is valid');
92         ok(-S $sock, 'sock created');
93
94         $test_lei_common->();
95
96         $out = '';
97         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
98         chomp(my $pid_again = $out);
99         is($pid, $pid_again, 'daemon-pid idempotent');
100
101         ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
102         is($out, '', 'no output from daemon-stop');
103         is($err, '', 'no error from daemon-stop');
104         for (0..100) {
105                 kill(0, $pid) or last;
106                 tick();
107         }
108         ok(!-S $sock, 'sock gone');
109         ok(!kill(0, $pid), 'pid gone after stop');
110
111         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
112         chomp(my $new_pid = $out);
113         ok(kill(0, $new_pid), 'new pid is running');
114         ok(-S $sock, 'sock exists again');
115         unlink $sock or BAIL_OUT "unlink $!";
116         for (0..100) {
117                 kill('CHLD', $new_pid) or last;
118                 tick();
119         }
120         ok(!kill(0, $new_pid), 'daemon exits after unlink');
121         $test_lei_common = undef; # success over socket, can't test without
122 };
123
124 require_ok 'PublicInbox::LeiDaemon';
125 $test_lei_common->() if $test_lei_common;
126
127 done_testing;