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 @onions = qw(http://hjrcffqmbrq6wope.onion/meta/
18 http://czquwvybam4bgbro.onion/meta/
19 http://ou63pmih66umazou.onion/meta/);
20 my $json = ref(PublicInbox::Config->json)->new->utf8->canonical;
22 my ($cmd, $env, $xopt) = @_;
25 ($env, $xopt) = grep { (!defined) || ref } @_;
26 $cmd = [ grep { defined && !ref } @_ ];
28 my $res = run_script(['lei', @$cmd], $env, $xopt // $opt);
30 $err = join('', grep(!/$err_filter/, split(/^/m, $err)));
34 delete local $ENV{XDG_DATA_HOME};
35 delete local $ENV{XDG_CONFIG_HOME};
36 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
37 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
38 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
39 local $ENV{HOME} = $home;
40 local $ENV{FOO} = 'BAR';
41 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
42 my $home_trash = [ "$home/.local", "$home/.config" ];
43 my $cleanup = sub { rmtree([@$home_trash, @_]) };
44 my $config_file = "$home/.config/lei/config";
45 my $store_dir = "$home/.local/share/lei";
48 ok(!$lei->(), 'no args fails');
49 is($? >> 8, 1, '$? is 1');
50 is($out, '', 'nothing in stdout');
51 like($err, qr/^usage:/sm, 'usage in stderr');
53 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
54 ok($lei->($arg), "lei @$arg");
55 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
56 is($err, '', "nothing in stderr (@$arg)");
59 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
60 ok(!$lei->($arg), "lei @$arg");
61 is($? >> 8, 1, '$? set correctly');
62 isnt($err, '', 'something in stderr');
63 is($out, '', 'nothing in stdout');
65 ok($lei->(qw(init -h)), 'init -h');
66 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
67 'actual path shown in init -h');
68 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
69 'init with XDG_DATA_HOME');
70 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
71 is($err, '', 'no errors from init -h');
73 ok($lei->(qw(config -h)), 'config-h');
74 like($out, qr! \Q$home\E/\.config/lei/config\b!,
75 'actual path shown in config -h');
76 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
77 'config with XDG_CONFIG_HOME');
78 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
79 is($err, '', 'no errors from config -h');
82 my $ok_err_info = sub {
84 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
85 diag "$msg: err=$err";
90 ok($lei->('init'), 'init w/o args');
91 $ok_err_info->('after init w/o args');
92 ok($lei->('init'), 'idempotent init w/o args');
93 $ok_err_info->('after idempotent init w/o args');
95 ok(!$lei->('init', "$home/x"), 'init conflict');
96 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
97 ok(!-e "$home/x", 'nothing created on conflict');
100 ok($lei->('init', "$home/x"), 'init conflict resolved');
101 $ok_err_info->('init w/ arg');
102 ok($lei->('init', "$home/x"), 'init idempotent w/ path');
103 $ok_err_info->('init idempotent w/ arg');
104 ok(-d "$home/x", 'created dir');
105 $cleanup->("$home/x");
107 ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
108 like($err, qr/too many/, 'noted excessive');
109 ok(!-e "$home/x", 'x not created on excessive');
110 for my $d (@$home_trash) {
111 my $base = (split(m!/!, $d))[-1];
112 ok(!-d $d, "$base not created");
114 is($out, '', 'nothing in stdout on init failure');
117 my $test_config = sub {
119 ok($lei->(qw(config a.b c)), 'config set var');
120 is($out.$err, '', 'no output on var set');
121 ok($lei->(qw(config -l)), 'config -l');
122 is($err, '', 'no errors on listing');
123 is($out, "a.b=c\n", 'got expected output');
124 ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
125 'config set var with -f fails');
126 like($err, qr/not supported/, 'not supported noted');
127 ok(!-f "$home/config/f", 'no file created');
130 my $setup_publicinboxes = sub {
132 return if $done eq $home;
133 use PublicInbox::InboxWritable;
135 run_script([qw(-init), "-V$V", "t$V",
136 '--newsgroup', "t.$V",
137 "$home/t$V", "http://example.com/t$V",
138 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
140 my $cfg = PublicInbox::Config->new;
142 $cfg->each_inbox(sub {
144 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
145 my $V = $ibx->version;
146 my @eml = (glob('t/*.eml'), 't/data/0001.patch');
148 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
149 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
154 run_script(['-index', $ibx->{inboxdir}]) or
159 $seen || BAIL_OUT 'no imports';
162 my $test_external_remote = sub {
166 skip "$k unset", $nr if !$url;
167 which('curl') or skip 'no curl', $nr;
168 which('torsocks') or skip 'no torsocks', $nr if $url =~ m!\.onion/!;
169 $lei->('ls-external');
170 for my $e (split(/^/ms, $out)) {
171 $e =~ s/\s+boost.*//s;
172 $lei->('forget-external', '-q', $e) or
173 fail "error forgetting $e: $err"
175 $lei->('add-external', $url);
176 my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
177 ok($lei->('q', '-q', "m:$mid"), "query $url");
178 is($err, '', "no errors on $url");
179 my $res = $json->decode($out);
180 is($res->[0]->{'m'}, "<$mid>", "got expected mid from $url");
181 ok($lei->('q', '-q', "m:$mid", 'd:..20101002'), 'no results, no error');
182 is($err, '', 'no output on 404, matching local FS behavior');
183 is($out, "[null]\n", 'got null results');
184 $lei->('forget-external', $url);
188 my $test_external = sub {
189 $setup_publicinboxes->();
191 $lei->('ls-external');
192 is($out.$err, '', 'ls-external no output, yet');
193 ok(!-e $config_file && !-e $store_dir,
194 'nothing created by ls-external');
196 ok(!$lei->('add-external', "$home/nonexistent"),
197 "fails on non-existent dir");
198 $lei->('ls-external');
199 is($out.$err, '', 'ls-external still has no output');
200 my $cfg = PublicInbox::Config->new;
201 $cfg->each_inbox(sub {
203 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
205 is($out.$err, '', 'no output');
207 ok(-s $config_file && -e $store_dir,
208 'add-external created config + store');
209 my $lcfg = PublicInbox::Config->new($config_file);
210 $cfg->each_inbox(sub {
212 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
213 "configured boost on $ibx->{name}");
215 $lei->('ls-external');
216 like($out, qr/boost=0\n/s, 'ls-external has output');
217 ok($lei->(qw(add-external -q https://EXAMPLE.com/ibx)), 'add remote');
218 is($err, '', 'no warnings after add-external');
220 ok($lei->(qw(_complete lei forget-external)), 'complete for externals');
221 my %comp = map { $_ => 1 } split(/\s+/, $out);
222 ok($comp{'https://example.com/ibx/'}, 'forget external completion');
223 $cfg->each_inbox(sub {
225 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
227 for my $u (qw(h http https https: https:/ https:// https://e
228 https://example https://example. https://example.co
229 https://example.com https://example.com/
230 https://example.com/i https://example.com/ibx)) {
231 ok($lei->(qw(_complete lei forget-external), $u),
232 "partial completion for URL $u");
233 is($out, "https://example.com/ibx/\n",
234 "completed partial URL $u");
235 for my $qo (qw(-I --include --exclude --only)) {
236 ok($lei->(qw(_complete lei q), $qo, $u),
237 "partial completion for URL q $qo $u");
238 is($out, "https://example.com/ibx/\n",
239 "completed partial URL $u on q $qo");
243 $lei->('ls-external');
244 like($out, qr!https://example\.com/ibx/!s, 'added canonical URL');
245 is($err, '', 'no warnings on ls-external');
246 ok($lei->(qw(forget-external -q https://EXAMPLE.com/ibx)),
248 $lei->('ls-external');
249 unlike($out, qr!https://example\.com/ibx/!s, 'removed canonical URL');
251 ok(!$lei->(qw(q s:prefix -o /dev/null -f maildir)), 'bad maildir');
252 like($err, qr!/dev/null exists and is not a directory!,
254 is($? >> 8, 1, 'errored out with exit 1');
256 ok(!$lei->(qw(q s:prefix -f mboxcl2 -o), $home), 'bad mbox');
257 like($err, qr!\Q$home\E exists and is not a writable file!,
259 is($? >> 8, 1, 'errored out with exit 1');
261 ok(!$lei->(qw(q s:prefix -o /dev/stdout -f Mbox2)), 'bad format');
262 like($err, qr/bad mbox --format=mbox2/, 'error shown');
263 is($? >> 8, 1, 'errored out with exit 1');
265 # note, on a Bourne shell users should be able to use either:
266 # s:"use boolean prefix"
267 # "s:use boolean prefix"
268 # or use single quotes, it should not matter. Users only need
269 # to know shell quoting rules, not Xapian quoting rules.
270 # No double-quoting should be imposed on users on the CLI
271 $lei->('q', 's:use boolean prefix');
272 like($out, qr/search: use boolean prefix/, 'phrase search got result');
273 my $res = $json->decode($out);
274 is(scalar(@$res), 2, 'only 2 element array (1 result)');
275 is($res->[1], undef, 'final element is undef'); # XXX should this be?
276 is(ref($res->[0]), 'HASH', 'first element is hashref');
277 $lei->('q', '--pretty', 's:use boolean prefix');
278 my $pretty = $json->decode($out);
279 is_deeply($res, $pretty, '--pretty is identical after decode');
281 for my $fmt (qw(ldjson ndjson jsonl)) {
282 $lei->('q', '-f', $fmt, 's:use boolean prefix');
283 is($out, $json->encode($pretty->[0])."\n", "-f $fmt");
286 require IO::Uncompress::Gunzip;
287 for my $sfx ('', '.gz') {
288 my $f = "$home/mbox$sfx";
289 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
290 my $cat = $sfx eq '' ? sub {
291 open my $mb, '<', $f or fail "no mbox: $!";
294 my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
297 my @s = grep(/^Subject:/, $cat->());
298 is(scalar(@s), 1, "1 result in mbox$sfx");
299 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
300 is(grep(!/^#/, $err), 0, 'no errors from augment');
301 @s = grep(/^Subject:/, my @wtf = $cat->());
302 is(scalar(@s), 2, "2 results in mbox$sfx");
304 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
305 is(grep(!/^#/, $err), 0, "no errors on no results ($sfx)");
307 my @s2 = grep(/^Subject:/, $cat->());
309 "same 2 old results w/ --augment and bad search $sfx");
311 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
313 is_deeply(\@res, [], "clobber w/o --augment $sfx");
315 ok(!$lei->('q', '-o', "$home/mbox", 's:nope'),
316 'fails if mbox format unspecified');
317 ok(!$lei->(qw(q --no-local s:see)), '--no-local');
318 is($? >> 8, 1, 'proper exit code');
319 like($err, qr/no local or remote.+? to search/, 'no inbox');
321 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
322 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
324 for my $k (keys %e) {
325 my $url = $ENV{$k} // '';
326 $url = $e{$k} if $url eq '1';
327 $test_external_remote->($url, $k);
331 my $test_completion = sub {
332 ok($lei->(qw(_complete lei)), 'no errors on complete');
333 my %out = map { $_ => 1 } split(/\s+/s, $out);
334 ok($out{'q'}, "`lei q' offered as completion");
335 ok($out{'add-external'}, "`lei add-external' offered as completion");
337 ok($lei->(qw(_complete lei q)), 'complete q (no args)');
338 %out = map { $_ => 1 } split(/\s+/s, $out);
339 for my $sw (qw(-f --format -o --output --mfolder --augment -a
340 --mua --mua-cmd --no-local --local --verbose -v
341 --save-as --no-remote --remote --torsocks
343 ok($out{$sw}, "$sw offered as completion");
346 ok($lei->(qw(_complete lei q --form)), 'complete q --format');
347 is($out, "--format\n", 'complete lei q --format');
348 for my $sw (qw(-f --format)) {
349 ok($lei->(qw(_complete lei q), $sw), "complete q $sw ARG");
350 %out = map { $_ => 1 } split(/\s+/s, $out);
351 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
352 concatjson maildir)) {
353 ok($out{$f}, "got $sw $f as output format");
358 my $test_lei_common = sub {
363 $test_completion->();
366 if ($ENV{TEST_LEI_ONESHOT}) {
367 require_ok 'PublicInbox::LEI';
368 # force sun_path[108] overflow, ($lei->() filters out this path)
369 my $xrd = "$home/1shot-test".('.sun_path' x 108);
370 local $ENV{XDG_RUNTIME_DIR} = $xrd;
371 $err_filter = qr!\Q$xrd!;
372 $test_lei_common->();
374 SKIP: { # real socket
375 eval { require Socket::MsgHdr; 1 } // do {
376 require PublicInbox::Spawn;
377 PublicInbox::Spawn->can('send_cmd4');
378 } // skip 'Socket::MsgHdr or Inline::C missing or unconfigured', 115;
379 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
380 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/5.seq.sock";
381 my $err_log = "$ENV{XDG_RUNTIME_DIR}/lei/errors.log";
383 ok($lei->('daemon-pid'), 'daemon-pid');
384 is($err, '', 'no error from daemon-pid');
385 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
386 chomp(my $pid = $out);
387 ok(kill(0, $pid), 'pid is valid');
388 ok(-S $sock, 'sock created');
390 $test_lei_common->();
391 is(-s $err_log, 0, 'nothing in errors.log');
392 open my $efh, '>>', $err_log or BAIL_OUT $!;
393 print $efh "phail\n" or BAIL_OUT $!;
394 close $efh or BAIL_OUT $!;
396 ok($lei->('daemon-pid'), 'daemon-pid');
397 chomp(my $pid_again = $out);
398 is($pid, $pid_again, 'daemon-pid idempotent');
399 like($err, qr/phail/, 'got mock "phail" error previous run');
401 ok($lei->(qw(daemon-kill)), 'daemon-kill');
402 is($out, '', 'no output from daemon-kill');
403 is($err, '', 'no error from daemon-kill');
405 kill(0, $pid) or last;
408 ok(-S $sock, 'sock still exists');
409 ok(!kill(0, $pid), 'pid gone after stop');
411 ok($lei->(qw(daemon-pid)), 'daemon-pid');
412 chomp(my $new_pid = $out);
413 ok(kill(0, $new_pid), 'new pid is running');
414 ok(-S $sock, 'sock still exists');
416 for my $sig (qw(-0 -CHLD)) {
417 ok($lei->('daemon-kill', $sig), "handles $sig");
419 is($out.$err, '', 'no output on innocuous signals');
420 ok($lei->('daemon-pid'), 'daemon-pid');
422 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
424 if ('socket inaccessible') {
425 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
426 ok($lei->('help'), 'connect fail, one-shot fallback works');
427 like($err, qr/\bconnect\(/, 'connect error noted');
428 like($out, qr/^usage: /, 'help output works');
429 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
431 unlink $sock or BAIL_OUT "unlink($sock) $!";
433 kill('CHLD', $new_pid) or last;
436 ok(!kill(0, $new_pid), 'daemon exits after unlink');
437 # success over socket, can't test without