]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei q: fix augment of compressed mailboxes
[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         # note, on a Bourne shell users should be able to use either:
185         #       s:"use boolean prefix"
186         #       "s:use boolean prefix"
187         # or use single quotes, it should not matter.  Users only need
188         # to know shell quoting rules, not Xapian quoting rules.
189         # No double-quoting should be imposed on users on the CLI
190         $lei->('q', 's:use boolean prefix');
191         like($out, qr/search: use boolean prefix/, 'phrase search got result');
192         require IO::Uncompress::Gunzip;
193         for my $sfx ('', '.gz') {
194                 my $f = "$home/mbox$sfx";
195                 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
196                 my $cat = $sfx eq '' ? sub {
197                         open my $mb, '<', $f or fail "no mbox: $!";
198                         <$mb>
199                 } : sub {
200                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
201                         <$z>;
202                 };
203                 my @s = grep(/^Subject:/, $cat->());
204                 is(scalar(@s), 1, "1 result in mbox$sfx");
205                 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
206                 is($err, '', 'no errors from augment');
207                 @s = grep(/^Subject:/, my @wtf = $cat->());
208                 is(scalar(@s), 2, "2 results in mbox$sfx");
209
210                 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
211                 is($err, '', "no errors on no results ($sfx)");
212
213                 my @s2 = grep(/^Subject:/, $cat->());
214                 is_deeply(\@s2, \@s,
215                         "same 2 old results w/ --augment and bad search $sfx");
216
217                 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
218                 my @res = $cat->();
219                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
220         }
221 };
222
223 my $test_lei_common = sub {
224         $test_help->();
225         $test_config->();
226         $test_init->();
227         $test_external->();
228 };
229
230 if ($ENV{TEST_LEI_ONESHOT}) {
231         require_ok 'PublicInbox::LEI';
232         # force sun_path[108] overflow, ($lei->() filters out this path)
233         my $xrd = "$home/1shot-test".('.sun_path' x 108);
234         local $ENV{XDG_RUNTIME_DIR} = $xrd;
235         $err_filter = qr!\Q$xrd!;
236         $test_lei_common->();
237 }
238
239 SKIP: { # real socket
240         require_mods(qw(Cwd), my $nr = 105);
241         my $nfd = eval { require Socket::MsgHdr; 5 } // do {
242                 require PublicInbox::Spawn;
243                 PublicInbox::Spawn->can('send_cmd4') ? 5 : undef;
244         } //
245         skip 'Socket::MsgHdr or Inline::C missing or unconfigured', $nr;
246
247         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
248         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.seq.sock";
249
250         ok($lei->('daemon-pid'), 'daemon-pid');
251         is($err, '', 'no error from daemon-pid');
252         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
253         chomp(my $pid = $out);
254         ok(kill(0, $pid), 'pid is valid');
255         ok(-S $sock, 'sock created');
256
257         $test_lei_common->();
258
259         ok($lei->('daemon-pid'), 'daemon-pid');
260         chomp(my $pid_again = $out);
261         is($pid, $pid_again, 'daemon-pid idempotent');
262
263         ok($lei->(qw(daemon-kill)), 'daemon-kill');
264         is($out, '', 'no output from daemon-kill');
265         is($err, '', 'no error from daemon-kill');
266         for (0..100) {
267                 kill(0, $pid) or last;
268                 tick();
269         }
270         ok(-S $sock, 'sock still exists');
271         ok(!kill(0, $pid), 'pid gone after stop');
272
273         ok($lei->(qw(daemon-pid)), 'daemon-pid');
274         chomp(my $new_pid = $out);
275         ok(kill(0, $new_pid), 'new pid is running');
276         ok(-S $sock, 'sock still exists');
277
278         for my $sig (qw(-0 -CHLD)) {
279                 ok($lei->('daemon-kill', $sig), "handles $sig");
280         }
281         is($out.$err, '', 'no output on innocuous signals');
282         ok($lei->('daemon-pid'), 'daemon-pid');
283         chomp $out;
284         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
285
286         if ('socket inaccessible') {
287                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
288                 ok($lei->('help'), 'connect fail, one-shot fallback works');
289                 like($err, qr/\bconnect\(/, 'connect error noted');
290                 like($out, qr/^usage: /, 'help output works');
291                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
292         }
293         unlink $sock or BAIL_OUT "unlink($sock) $!";
294         for (0..100) {
295                 kill('CHLD', $new_pid) or last;
296                 tick();
297         }
298         ok(!kill(0, $new_pid), 'daemon exits after unlink');
299         # success over socket, can't test without
300 };
301
302 done_testing;