]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
update copyrights for 2021
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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_git 2.6;
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $LEI = 'lei';
13 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my $lei = sub {
15         my ($cmd, $env, $xopt) = @_;
16         $out = $err = '';
17         if (!ref($cmd)) {
18                 ($env, $xopt) = grep { (!defined) || ref } @_;
19                 $cmd = [ grep { defined && !ref } @_ ];
20         }
21         run_script([$LEI, @$cmd], $env, $xopt // $opt);
22 };
23
24 my ($home, $for_destroy) = tmpdir();
25 delete local $ENV{XDG_DATA_HOME};
26 delete local $ENV{XDG_CONFIG_HOME};
27 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
28 local $ENV{HOME} = $home;
29 local $ENV{FOO} = 'BAR';
30 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
31 my $home_trash = [ "$home/.local", "$home/.config" ];
32 my $cleanup = sub { rmtree([@$home_trash, @_]) };
33 my $config_file = "$home/.config/lei/config";
34 my $store_dir = "$home/.local/share/lei";
35
36 my $test_help = sub {
37         ok(!$lei->([], undef, $opt), 'no args fails');
38         is($? >> 8, 1, '$? is 1');
39         is($out, '', 'nothing in stdout');
40         like($err, qr/^usage:/sm, 'usage in stderr');
41
42         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
43                 $out = $err = '';
44                 ok($lei->($arg, undef, $opt), "lei @$arg");
45                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
46                 is($err, '', "nothing in stderr (@$arg)");
47         }
48
49         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
50                 $out = $err = '';
51                 ok(!$lei->($arg, undef, $opt), "lei @$arg");
52                 is($? >> 8, 1, '$? set correctly');
53                 isnt($err, '', 'something in stderr');
54                 is($out, '', 'nothing in stdout');
55         }
56         ok($lei->(qw(init -h), undef, $opt), 'init -h');
57         like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
58                 'actual path shown in init -h');
59         ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
60                 'init with XDG_DATA_HOME');
61         like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
62         is($err, '', 'no errors from init -h');
63
64         ok($lei->(qw(config -h), undef, $opt), 'config-h');
65         like($out, qr! \Q$home\E/\.config/lei/config\b!,
66                 'actual path shown in config -h');
67         ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
68                 'config with XDG_CONFIG_HOME');
69         like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
70         is($err, '', 'no errors from config -h');
71 };
72
73 my $ok_err_info = sub {
74         my ($msg) = @_;
75         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
76                 diag "$msg: err=$err";
77         $err = '';
78 };
79
80 my $test_init = sub {
81         $cleanup->();
82         ok($lei->(['init'], undef, $opt), 'init w/o args');
83         $ok_err_info->('after init w/o args');
84         ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
85         $ok_err_info->('after idempotent init w/o args');
86
87         ok(!$lei->(['init', "$home/x"], undef, $opt),
88                 'init conflict');
89         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
90         ok(!-e "$home/x", 'nothing created on conflict');
91         $cleanup->();
92
93         ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
94         $ok_err_info->('init w/ arg');
95         ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
96         $ok_err_info->('init idempotent w/ arg');
97         ok(-d "$home/x", 'created dir');
98         $cleanup->("$home/x");
99
100         ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
101                 'too many args fails');
102         like($err, qr/too many/, 'noted excessive');
103         ok(!-e "$home/x", 'x not created on excessive');
104         for my $d (@$home_trash) {
105                 my $base = (split(m!/!, $d))[-1];
106                 ok(!-d $d, "$base not created");
107         }
108         is($out, '', 'nothing in stdout on init failure');
109 };
110
111 my $test_config = sub {
112         $cleanup->();
113         ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
114         is($out.$err, '', 'no output on var set');
115         ok($lei->([qw(config -l)], undef, $opt), 'config -l');
116         is($err, '', 'no errors on listing');
117         is($out, "a.b=c\n", 'got expected output');
118         ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
119                         'config set var with -f fails');
120         like($err, qr/not supported/, 'not supported noted');
121         ok(!-f "$home/config/f", 'no file created');
122 };
123
124 my $setup_publicinboxes = sub {
125         state $done = '';
126         return if $done eq $home;
127         use PublicInbox::InboxWritable;
128         for my $V (1, 2) {
129                 run_script([qw(-init -Lmedium), "-V$V", "t$V",
130                                 '--newsgroup', "t.$V",
131                                 "$home/t$V", "http://example.com/t$V",
132                                 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
133         }
134         my $cfg = PublicInbox::Config->new;
135         my $seen = 0;
136         $cfg->each_inbox(sub {
137                 my ($ibx) = @_;
138                 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
139                 my $V = $ibx->version;
140                 my @eml = glob('t/*.eml');
141                 push(@eml, 't/data/0001.patch') if $V == 2;
142                 for (@eml) {
143                         next if $_ eq 't/psgi_v2-old.eml'; # dup mid
144                         $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
145                         $seen++;
146                 }
147                 $im->done;
148                 if ($V == 1) {
149                         run_script(['-index', $ibx->{inboxdir}]) or
150                                 BAIL_OUT 'index v1';
151                 }
152         });
153         $done = $home;
154         $seen || BAIL_OUT 'no imports';
155 };
156
157 my $test_external = sub {
158         $setup_publicinboxes->();
159         $cleanup->();
160         $lei->('ls-external');
161         is($out.$err, '', 'ls-external no output, yet');
162         ok(!-e $config_file && !-e $store_dir,
163                 'nothing created by ls-external');
164
165         my $cfg = PublicInbox::Config->new;
166         $cfg->each_inbox(sub {
167                 my ($ibx) = @_;
168                 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
169                         'added external');
170                 is($out.$err, '', 'no output');
171         });
172         ok(-s $config_file && -e $store_dir,
173                 'add-external created config + store');
174         my $lcfg = PublicInbox::Config->new($config_file);
175         $cfg->each_inbox(sub {
176                 my ($ibx) = @_;
177                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
178                         "configured boost on $ibx->{name}");
179         });
180         $lei->('ls-external');
181         like($out, qr/boost=0\n/s, 'ls-external has output');
182 };
183
184 my $test_lei_common = sub {
185         $test_help->();
186         $test_config->();
187         $test_init->();
188         $test_external->();
189 };
190
191 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
192 SKIP: {
193         last SKIP if $test_lei_oneshot;
194         require_mods(qw(IO::FDPass Cwd), 46);
195         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
196
197         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
198         is($err, '', 'no error from daemon-pid');
199         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
200         chomp(my $pid = $out);
201         ok(kill(0, $pid), 'pid is valid');
202         ok(-S $sock, 'sock created');
203
204         $test_lei_common->();
205
206         $out = '';
207         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
208         chomp(my $pid_again = $out);
209         is($pid, $pid_again, 'daemon-pid idempotent');
210
211         $out = '';
212         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
213         is($err, '', 'no errors in env dump');
214         my @env = split(/\0/, $out);
215         is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
216         is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
217         is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
218
219         $out = '';
220         ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
221         is($out.$err, '', 'no output for unset');
222         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
223         is($err, '', 'no errors in env dump');
224         @env = split(/\0/, $out);
225         is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
226
227         $out = '';
228         ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
229                         undef, $opt), 'unset multiple');
230         is($out.$err, '', 'no errors output for unset');
231         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
232         is($err, '', 'no errors in env dump');
233         @env = split(/\0/, $out);
234         is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
235         $out = '';
236         ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
237         is($out.$err, '', 'no output');
238         ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
239         is($out, '', 'env cleared');
240
241         ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
242         is($out, '', 'no output from daemon-kill');
243         is($err, '', 'no error from daemon-kill');
244         for (0..100) {
245                 kill(0, $pid) or last;
246                 tick();
247         }
248         ok(!-S $sock, 'sock gone');
249         ok(!kill(0, $pid), 'pid gone after stop');
250
251         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
252         chomp(my $new_pid = $out);
253         ok(kill(0, $new_pid), 'new pid is running');
254         ok(-S $sock, 'sock exists again');
255
256         $out = $err = '';
257         for my $sig (qw(-0 -CHLD)) {
258                 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
259                                         "handles $sig");
260         }
261         is($out.$err, '', 'no output on innocuous signals');
262         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
263         chomp $out;
264         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
265
266         if ('socket inaccessible') {
267                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
268                 $out = $err = '';
269                 ok(run_script([qw(lei help)], undef, $opt),
270                         'connect fail, one-shot fallback works');
271                 like($err, qr/\bconnect\(/, 'connect error noted');
272                 like($out, qr/^usage: /, 'help output works');
273                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
274         }
275         if ('oneshot on cwd gone') {
276                 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
277                 my $d = "$home/to-be-removed";
278                 mkdir $d or BAIL_OUT "mkdir($d) $!";
279                 chdir $d or BAIL_OUT "chdir($d) $!";
280                 if (rmdir($d)) {
281                         $out = $err = '';
282                         ok(run_script([qw(lei help)], undef, $opt),
283                                 'cwd fail, one-shot fallback works');
284                 } else {
285                         $err = "rmdir=$!";
286                 }
287                 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
288                 like($err, qr/cwd\(/, 'cwd error noted');
289                 like($out, qr/^usage: /, 'help output still works');
290         }
291
292         unlink $sock or BAIL_OUT "unlink($sock) $!";
293         for (0..100) {
294                 kill('CHLD', $new_pid) or last;
295                 tick();
296         }
297         ok(!kill(0, $new_pid), 'daemon exits after unlink');
298         # success over socket, can't test without
299         $test_lei_common = undef;
300 };
301
302 require_ok 'PublicInbox::LEI';
303 $LEI = 'lei-oneshot' if $test_lei_oneshot;
304 $test_lei_common->() if $test_lei_common;
305
306 done_testing;