]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: support pass-through for `lei config'
[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 $LEI = 'lei';
12 my $lei = sub {
13         my ($cmd, $env, $opt) = @_;
14         run_script([$LEI, @$cmd], $env, $opt);
15 };
16
17 my ($home, $for_destroy) = tmpdir();
18 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
19 delete local $ENV{XDG_DATA_HOME};
20 delete local $ENV{XDG_CONFIG_HOME};
21 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
22 local $ENV{HOME} = $home;
23 local $ENV{FOO} = 'BAR';
24 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
25 my $home_trash = [ "$home/.local", "$home/.config" ];
26 my $cleanup = sub {
27         rmtree([@$home_trash, @_]);
28         $out = $err = '';
29 };
30
31 my $test_help = sub {
32         ok(!$lei->([], undef, $opt), 'no args fails');
33         is($? >> 8, 1, '$? is 1');
34         is($out, '', 'nothing in stdout');
35         like($err, qr/^usage:/sm, 'usage in stderr');
36
37         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
38                 $out = $err = '';
39                 ok($lei->($arg, undef, $opt), "lei @$arg");
40                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
41                 is($err, '', "nothing in stderr (@$arg)");
42         }
43
44         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
45                 $out = $err = '';
46                 ok(!$lei->($arg, undef, $opt), "lei @$arg");
47                 is($? >> 8, 1, '$? set correctly');
48                 isnt($err, '', 'something in stderr');
49                 is($out, '', 'nothing in stdout');
50         }
51 };
52
53 my $ok_err_info = sub {
54         my ($msg) = @_;
55         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
56                 diag "$msg: err=$err";
57         $err = '';
58 };
59
60 my $test_init = sub {
61         $cleanup->();
62         ok($lei->(['init'], undef, $opt), 'init w/o args');
63         $ok_err_info->('after init w/o args');
64         ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
65         $ok_err_info->('after idempotent init w/o args');
66
67         ok(!$lei->(['init', "$home/x"], undef, $opt),
68                 'init conflict');
69         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
70         ok(!-e "$home/x", 'nothing created on conflict');
71         $cleanup->();
72
73         ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
74         $ok_err_info->('init w/ arg');
75         ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
76         $ok_err_info->('init idempotent w/ arg');
77         ok(-d "$home/x", 'created dir');
78         $cleanup->("$home/x");
79
80         ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
81                 'too many args fails');
82         like($err, qr/too many/, 'noted excessive');
83         ok(!-e "$home/x", 'x not created on excessive');
84         for my $d (@$home_trash) {
85                 my $base = (split(m!/!, $d))[-1];
86                 ok(!-d $d, "$base not created");
87         }
88         is($out, '', 'nothing in stdout on init failure');
89 };
90
91 my $test_config = sub {
92         $cleanup->();
93         ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
94         is($out.$err, '', 'no output on var set');
95         ok($lei->([qw(config -l)], undef, $opt), 'config -l');
96         is($err, '', 'no errors on listing');
97         is($out, "a.b=c\n", 'got expected output');
98         ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
99                         'config set var with -f fails');
100         like($err, qr/not supported/, 'not supported noted');
101         ok(!-f "$home/config/f", 'no file created');
102 };
103
104 my $test_lei_common = sub {
105         $test_help->();
106         $test_config->();
107         $test_init->();
108 };
109
110 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
111 SKIP: {
112         last SKIP if $test_lei_oneshot;
113         require_mods('IO::FDPass', 16);
114         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
115
116         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
117         is($err, '', 'no error from daemon-pid');
118         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
119         chomp(my $pid = $out);
120         ok(kill(0, $pid), 'pid is valid');
121         ok(-S $sock, 'sock created');
122
123         $test_lei_common->();
124
125         $out = '';
126         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
127         chomp(my $pid_again = $out);
128         is($pid, $pid_again, 'daemon-pid idempotent');
129
130         $out = '';
131         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
132         is($err, '', 'no errors in env dump');
133         my @env = split(/\0/, $out);
134         is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
135         is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
136         is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
137
138         $out = '';
139         ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
140         is($out.$err, '', 'no output for unset');
141         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
142         is($err, '', 'no errors in env dump');
143         @env = split(/\0/, $out);
144         is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
145
146         $out = '';
147         ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
148                         undef, $opt), 'unset multiple');
149         is($out.$err, '', 'no errors output for unset');
150         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
151         is($err, '', 'no errors in env dump');
152         @env = split(/\0/, $out);
153         is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
154         $out = '';
155         ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
156         is($out.$err, '', 'no output');
157         ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
158         is($out, '', 'env cleared');
159
160         ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
161         is($out, '', 'no output from daemon-stop');
162         is($err, '', 'no error from daemon-stop');
163         for (0..100) {
164                 kill(0, $pid) or last;
165                 tick();
166         }
167         ok(!-S $sock, 'sock gone');
168         ok(!kill(0, $pid), 'pid gone after stop');
169
170         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
171         chomp(my $new_pid = $out);
172         ok(kill(0, $new_pid), 'new pid is running');
173         ok(-S $sock, 'sock exists again');
174         unlink $sock or BAIL_OUT "unlink $!";
175         for (0..100) {
176                 kill('CHLD', $new_pid) or last;
177                 tick();
178         }
179         ok(!kill(0, $new_pid), 'daemon exits after unlink');
180         # success over socket, can't test without
181         $test_lei_common = undef;
182 };
183
184 require_ok 'PublicInbox::LEI';
185 $LEI = 'lei-oneshot' if $test_lei_oneshot;
186 $test_lei_common->() if $test_lei_common;
187
188 done_testing;