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