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