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>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 use File::Path qw(rmtree);
10 use Fcntl qw(SEEK_SET);
11 use PublicInbox::Spawn qw(which);
13 require_mods(qw(json DBD::SQLite Search::Xapian));
14 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
15 my ($home, $for_destroy) = tmpdir();
17 my $curl = which('curl');
18 my @onions = qw(http://hjrcffqmbrq6wope.onion/meta/
19 http://czquwvybam4bgbro.onion/meta/
20 http://ou63pmih66umazou.onion/meta/);
21 my $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
23 my ($cmd, $env, $xopt) = @_;
26 ($env, $xopt) = grep { (!defined) || ref } @_;
27 $cmd = [ grep { defined && !ref } @_ ];
29 my $res = run_script(['lei', @$cmd], $env, $xopt // $opt);
31 $err = join('', grep(!/$err_filter/, split(/^/m, $err)));
35 delete local $ENV{XDG_DATA_HOME};
36 delete local $ENV{XDG_CONFIG_HOME};
37 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
38 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
39 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
40 local $ENV{HOME} = $home;
41 local $ENV{FOO} = 'BAR';
42 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
43 my $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
44 my $cleanup = sub { rmtree([@$home_trash, @_]) };
45 my $config_file = "$home/.config/lei/config";
46 my $store_dir = "$home/.local/share/lei";
49 ok(!$lei->(), 'no args fails');
50 is($? >> 8, 1, '$? is 1');
51 is($out, '', 'nothing in stdout');
52 like($err, qr/^usage:/sm, 'usage in stderr');
54 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
55 ok($lei->($arg), "lei @$arg");
56 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
57 is($err, '', "nothing in stderr (@$arg)");
60 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
61 ok(!$lei->($arg), "lei @$arg");
62 is($? >> 8, 1, '$? set correctly');
63 isnt($err, '', 'something in stderr');
64 is($out, '', 'nothing in stdout');
66 ok($lei->(qw(init -h)), 'init -h');
67 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
68 'actual path shown in init -h');
69 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
70 'init with XDG_DATA_HOME');
71 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
72 is($err, '', 'no errors from init -h');
74 ok($lei->(qw(config -h)), 'config-h');
75 like($out, qr! \Q$home\E/\.config/lei/config\b!,
76 'actual path shown in config -h');
77 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
78 'config with XDG_CONFIG_HOME');
79 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
80 is($err, '', 'no errors from config -h');
83 my $ok_err_info = sub {
85 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
86 diag "$msg: err=$err";
91 ok($lei->('init'), 'init w/o args');
92 $ok_err_info->('after init w/o args');
93 ok($lei->('init'), 'idempotent init w/o args');
94 $ok_err_info->('after idempotent init w/o args');
96 ok(!$lei->('init', "$home/x"), 'init conflict');
97 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
98 ok(!-e "$home/x", 'nothing created on conflict');
101 ok($lei->('init', "$home/x"), 'init conflict resolved');
102 $ok_err_info->('init w/ arg');
103 ok($lei->('init', "$home/x"), 'init idempotent w/ path');
104 $ok_err_info->('init idempotent w/ arg');
105 ok(-d "$home/x", 'created dir');
106 $cleanup->("$home/x");
108 ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
109 like($err, qr/too many/, 'noted excessive');
110 ok(!-e "$home/x", 'x not created on excessive');
111 for my $d (@$home_trash) {
112 my $base = (split(m!/!, $d))[-1];
113 ok(!-d $d, "$base not created");
115 is($out, '', 'nothing in stdout on init failure');
118 my $test_config = sub {
120 ok($lei->(qw(config a.b c)), 'config set var');
121 is($out.$err, '', 'no output on var set');
122 ok($lei->(qw(config -l)), 'config -l');
123 is($err, '', 'no errors on listing');
124 is($out, "a.b=c\n", 'got expected output');
125 ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
126 'config set var with -f fails');
127 like($err, qr/not supported/, 'not supported noted');
128 ok(!-f "$home/config/f", 'no file created');
131 my $setup_publicinboxes = sub {
133 return if $done eq $home;
134 use PublicInbox::InboxWritable;
136 run_script([qw(-init), "-V$V", "t$V",
137 '--newsgroup', "t.$V",
138 "$home/t$V", "http://example.com/t$V",
139 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
141 my $cfg = PublicInbox::Config->new;
143 $cfg->each_inbox(sub {
145 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
146 my $V = $ibx->version;
147 my @eml = (glob('t/*.eml'), 't/data/0001.patch');
149 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
150 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
155 run_script(['-index', $ibx->{inboxdir}]) or
160 $seen || BAIL_OUT 'no imports';
163 my $test_external_remote = sub {
167 skip "$k unset", $nr if !$url;
168 $curl or skip 'no curl', $nr;
169 which('torsocks') or skip 'no torsocks', $nr if $url =~ m!\.onion/!;
170 my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
171 my @cmd = ('q', '--only', $url, '-q', "m:$mid");
172 ok($lei->(@cmd), "query $url");
173 is($err, '', "no errors on $url");
174 my $res = $json->decode($out);
175 is($res->[0]->{'m'}, "<$mid>", "got expected mid from $url");
176 ok($lei->(@cmd, 'd:..20101002'), 'no results, no error');
177 is($err, '', 'no output on 404, matching local FS behavior');
178 is($out, "[null]\n", 'got null results');
182 my $test_external = sub {
183 $setup_publicinboxes->();
185 $lei->('ls-external');
186 is($out.$err, '', 'ls-external no output, yet');
187 ok(!-e $config_file && !-e $store_dir,
188 'nothing created by ls-external');
190 ok(!$lei->('add-external', "$home/nonexistent"),
191 "fails on non-existent dir");
192 $lei->('ls-external');
193 is($out.$err, '', 'ls-external still has no output');
194 my $cfg = PublicInbox::Config->new;
195 $cfg->each_inbox(sub {
197 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
199 is($out.$err, '', 'no output');
201 ok(-s $config_file && -e $store_dir,
202 'add-external created config + store');
203 my $lcfg = PublicInbox::Config->new($config_file);
204 $cfg->each_inbox(sub {
206 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
207 "configured boost on $ibx->{name}");
209 $lei->('ls-external');
210 like($out, qr/boost=0\n/s, 'ls-external has output');
211 ok($lei->(qw(add-external -q https://EXAMPLE.com/ibx)), 'add remote');
212 is($err, '', 'no warnings after add-external');
214 ok($lei->(qw(_complete lei forget-external)), 'complete for externals');
215 my %comp = map { $_ => 1 } split(/\s+/, $out);
216 ok($comp{'https://example.com/ibx/'}, 'forget external completion');
217 $cfg->each_inbox(sub {
219 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
221 for my $u (qw(h http https https: https:/ https:// https://e
222 https://example https://example. https://example.co
223 https://example.com https://example.com/
224 https://example.com/i https://example.com/ibx)) {
225 ok($lei->(qw(_complete lei forget-external), $u),
226 "partial completion for URL $u");
227 is($out, "https://example.com/ibx/\n",
228 "completed partial URL $u");
229 for my $qo (qw(-I --include --exclude --only)) {
230 ok($lei->(qw(_complete lei q), $qo, $u),
231 "partial completion for URL q $qo $u");
232 is($out, "https://example.com/ibx/\n",
233 "completed partial URL $u on q $qo");
237 $lei->('ls-external');
238 like($out, qr!https://example\.com/ibx/!s, 'added canonical URL');
239 is($err, '', 'no warnings on ls-external');
240 ok($lei->(qw(forget-external -q https://EXAMPLE.com/ibx)),
242 $lei->('ls-external');
243 unlike($out, qr!https://example\.com/ibx/!s, 'removed canonical URL');
245 ok(!$lei->(qw(q s:prefix -o /dev/null -f maildir)), 'bad maildir');
246 like($err, qr!/dev/null exists and is not a directory!,
248 is($? >> 8, 1, 'errored out with exit 1');
250 ok(!$lei->(qw(q s:prefix -f mboxcl2 -o), $home), 'bad mbox');
251 like($err, qr!\Q$home\E exists and is not a writable file!,
253 is($? >> 8, 1, 'errored out with exit 1');
255 ok(!$lei->(qw(q s:prefix -o /dev/stdout -f Mbox2)), 'bad format');
256 like($err, qr/bad mbox --format=mbox2/, 'error shown');
257 is($? >> 8, 1, 'errored out with exit 1');
259 # note, on a Bourne shell users should be able to use either:
260 # s:"use boolean prefix"
261 # "s:use boolean prefix"
262 # or use single quotes, it should not matter. Users only need
263 # to know shell quoting rules, not Xapian quoting rules.
264 # No double-quoting should be imposed on users on the CLI
265 $lei->('q', 's:use boolean prefix');
266 like($out, qr/search: use boolean prefix/, 'phrase search got result');
267 my $res = $json->decode($out);
268 is(scalar(@$res), 2, 'only 2 element array (1 result)');
269 is($res->[1], undef, 'final element is undef'); # XXX should this be?
270 is(ref($res->[0]), 'HASH', 'first element is hashref');
271 $lei->('q', '--pretty', 's:use boolean prefix');
272 my $pretty = $json->decode($out);
273 is_deeply($res, $pretty, '--pretty is identical after decode');
275 for my $fmt (qw(ldjson ndjson jsonl)) {
276 $lei->('q', '-f', $fmt, 's:use boolean prefix');
277 is($out, $json->encode($pretty->[0])."\n", "-f $fmt");
280 require IO::Uncompress::Gunzip;
281 for my $sfx ('', '.gz') {
282 my $f = "$home/mbox$sfx";
283 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
284 my $cat = $sfx eq '' ? sub {
285 open my $mb, '<', $f or fail "no mbox: $!";
288 my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
291 my @s = grep(/^Subject:/, $cat->());
292 is(scalar(@s), 1, "1 result in mbox$sfx");
293 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
294 is(grep(!/^#/, $err), 0, 'no errors from augment');
295 @s = grep(/^Subject:/, my @wtf = $cat->());
296 is(scalar(@s), 2, "2 results in mbox$sfx");
298 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
299 is(grep(!/^#/, $err), 0, "no errors on no results ($sfx)");
301 my @s2 = grep(/^Subject:/, $cat->());
303 "same 2 old results w/ --augment and bad search $sfx");
305 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
307 is_deeply(\@res, [], "clobber w/o --augment $sfx");
309 ok(!$lei->('q', '-o', "$home/mbox", 's:nope'),
310 'fails if mbox format unspecified');
311 ok(!$lei->(qw(q --no-local s:see)), '--no-local');
312 is($? >> 8, 1, 'proper exit code');
313 like($err, qr/no local or remote.+? to search/, 'no inbox');
315 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
316 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
318 for my $k (keys %e) {
319 my $url = $ENV{$k} // '';
320 $url = $e{$k} if $url eq '1';
321 $test_external_remote->($url, $k);
325 my $test_completion = sub {
326 ok($lei->(qw(_complete lei)), 'no errors on complete');
327 my %out = map { $_ => 1 } split(/\s+/s, $out);
328 ok($out{'q'}, "`lei q' offered as completion");
329 ok($out{'add-external'}, "`lei add-external' offered as completion");
331 ok($lei->(qw(_complete lei q)), 'complete q (no args)');
332 %out = map { $_ => 1 } split(/\s+/s, $out);
333 for my $sw (qw(-f --format -o --output --mfolder --augment -a
334 --mua --mua-cmd --no-local --local --verbose -v
335 --save-as --no-remote --remote --torsocks
337 ok($out{$sw}, "$sw offered as completion");
340 ok($lei->(qw(_complete lei q --form)), 'complete q --format');
341 is($out, "--format\n", 'complete lei q --format');
342 for my $sw (qw(-f --format)) {
343 ok($lei->(qw(_complete lei q), $sw), "complete q $sw ARG");
344 %out = map { $_ => 1 } split(/\s+/s, $out);
345 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
346 concatjson maildir)) {
347 ok($out{$f}, "got $sw $f as output format");
352 my $test_fail = sub {
353 $lei->(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
354 is($? >> 8, 3, 'got curl exit for bogus URL');
355 $lei->(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
356 is($? >> 8, 3, 'got curl exit for bogus URL with Maildir');
357 is($out, '', 'no output');
360 my $test_lei_common = sub {
365 $test_completion->();
369 if ($ENV{TEST_LEI_ONESHOT}) {
370 require_ok 'PublicInbox::LEI';
371 # force sun_path[108] overflow, ($lei->() filters out this path)
372 my $xrd = "$home/1shot-test".('.sun_path' x 108);
373 local $ENV{XDG_RUNTIME_DIR} = $xrd;
374 $err_filter = qr!\Q$xrd!;
375 $test_lei_common->();
377 SKIP: { # real socket
378 eval { require Socket::MsgHdr; 1 } // do {
379 require PublicInbox::Spawn;
380 PublicInbox::Spawn->can('send_cmd4');
381 } // skip 'Socket::MsgHdr or Inline::C missing or unconfigured', 115;
382 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
383 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/5.seq.sock";
384 my $err_log = "$ENV{XDG_RUNTIME_DIR}/lei/errors.log";
386 ok($lei->('daemon-pid'), 'daemon-pid');
387 is($err, '', 'no error from daemon-pid');
388 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
389 chomp(my $pid = $out);
390 ok(kill(0, $pid), 'pid is valid');
391 ok(-S $sock, 'sock created');
393 $test_lei_common->();
394 is(-s $err_log, 0, 'nothing in errors.log');
395 open my $efh, '>>', $err_log or BAIL_OUT $!;
396 print $efh "phail\n" or BAIL_OUT $!;
397 close $efh or BAIL_OUT $!;
399 ok($lei->('daemon-pid'), 'daemon-pid');
400 chomp(my $pid_again = $out);
401 is($pid, $pid_again, 'daemon-pid idempotent');
402 like($err, qr/phail/, 'got mock "phail" error previous run');
404 ok($lei->(qw(daemon-kill)), 'daemon-kill');
405 is($out, '', 'no output from daemon-kill');
406 is($err, '', 'no error from daemon-kill');
408 kill(0, $pid) or last;
411 ok(-S $sock, 'sock still exists');
412 ok(!kill(0, $pid), 'pid gone after stop');
414 ok($lei->(qw(daemon-pid)), 'daemon-pid');
415 chomp(my $new_pid = $out);
416 ok(kill(0, $new_pid), 'new pid is running');
417 ok(-S $sock, 'sock still exists');
419 for my $sig (qw(-0 -CHLD)) {
420 ok($lei->('daemon-kill', $sig), "handles $sig");
422 is($out.$err, '', 'no output on innocuous signals');
423 ok($lei->('daemon-pid'), 'daemon-pid');
425 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
427 if ('socket inaccessible') {
428 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
429 ok($lei->('help'), 'connect fail, one-shot fallback works');
430 like($err, qr/\bconnect\(/, 'connect error noted');
431 like($out, qr/^usage: /, 'help output works');
432 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
434 unlink $sock or BAIL_OUT "unlink($sock) $!";
436 kill('CHLD', $new_pid) or last;
439 ok(!kill(0, $new_pid), 'daemon exits after unlink');
440 # success over socket, can't test without