]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-externals.t
lei q: -I/--include overrides --no-(external|local|remote)
[public-inbox.git] / t / lei-externals.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; use v5.10.1; use PublicInbox::TestCommon;
5 use Fcntl qw(SEEK_SET);
6 use PublicInbox::Spawn qw(which);
7 use PublicInbox::OnDestroy;
8 require_git 2.6;
9 require_mods(qw(json DBD::SQLite Search::Xapian));
10 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
11
12 my @onions = qw(http://hjrcffqmbrq6wope.onion/meta/
13         http://czquwvybam4bgbro.onion/meta/
14         http://ou63pmih66umazou.onion/meta/);
15
16 my $test_external_remote = sub {
17         my ($url, $k) = @_;
18 SKIP: {
19         skip "$k unset", 1 if !$url;
20         state $curl = which('curl');
21         $curl or skip 'no curl', 1;
22         which('torsocks') or skip 'no torsocks', 1 if $url =~ m!\.onion/!;
23         my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
24         my @cmd = ('q', '--only', $url, '-q', "m:$mid");
25         lei_ok(@cmd, \"query $url");
26         is($lei_err, '', "no errors on $url");
27         my $res = json_utf8->decode($lei_out);
28         is($res->[0]->{'m'}, $mid, "got expected mid from $url") or
29                 skip 'further remote tests', 1;
30         lei_ok(@cmd, 'd:..20101002', \'no results, no error');
31         is($lei_err, '', 'no output on 404, matching local FS behavior');
32         is($lei_out, "[null]\n", 'got null results');
33         my ($pid_before, $pid_after);
34         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
35                 lei_ok 'daemon-pid';
36                 chomp($pid_before = $lei_out);
37                 ok($pid_before, 'daemon is live');
38         }
39         for my $out ([], [qw(-f mboxcl2)]) {
40                 pipe(my ($r, $w)) or BAIL_OUT $!;
41                 open my $err, '+>', undef or BAIL_OUT $!;
42                 my $opt = { run_mode => 0, 1 => $w, 2 => $err };
43                 my $cmd = [qw(lei q -qt), @$out, 'z:1..'];
44                 my $tp = start_script($cmd, undef, $opt);
45                 close $w;
46                 sysread($r, my $buf, 1);
47                 close $r; # trigger SIGPIPE
48                 $tp->join;
49                 ok(WIFSIGNALED($?), "signaled @$out");
50                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
51                 seek($err, 0, 0);
52                 my @err = grep(!m{mkdir .*sun_path\b}, <$err>);
53                 is_deeply(\@err, [], "no errors @$out");
54         }
55         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
56                 lei_ok 'daemon-pid';
57                 chomp(my $pid_after = $lei_out);
58                 is($pid_after, $pid_before, 'pid unchanged') or
59                         skip 'daemon died', 1;
60                 lei_ok 'daemon-kill';
61                 my $alive = 1;
62                 for (1..100) {
63                         $alive = kill(0, $pid_after) or last;
64                         tick();
65                 }
66                 ok(!$alive, 'daemon-kill worked');
67         }
68 } # /SKIP
69 }; # /sub
70
71 my ($ro_home, $cfg_path) = setup_public_inboxes;
72 test_lei(sub {
73         my $home = $ENV{HOME};
74         my $config_file = "$home/.config/lei/config";
75         my $store_dir = "$home/.local/share/lei";
76         lei_ok 'ls-external', \'ls-external on fresh install';
77         is($lei_out.$lei_err, '', 'ls-external no output, yet');
78         ok(!-e $config_file && !-e $store_dir,
79                 'nothing created by ls-external');
80
81         ok(!lei('add-external', "$home/nonexistent"),
82                 "fails on non-existent dir");
83         like($lei_err, qr/not a directory/, 'noted non-existence');
84         mkdir "$home/new\nline" or BAIL_OUT "mkdir: $!";
85         ok(!lei('add-external', "$home/new\nline"), "fails on newline");
86         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
87         lei_ok('ls-external', \'ls-external works after add failure');
88         is($lei_out.$lei_err, '', 'ls-external still has no output');
89         my $cfg = PublicInbox::Config->new($cfg_path);
90         $cfg->each_inbox(sub {
91                 my ($ibx) = @_;
92                 lei_ok(qw(add-external -q), $ibx->{inboxdir},
93                                 \'added external');
94                 is($lei_out.$lei_err, '', 'no output');
95         });
96         ok(-s $config_file && -e $store_dir,
97                 'add-external created config + store');
98         my $lcfg = PublicInbox::Config->new($config_file);
99         $cfg->each_inbox(sub {
100                 my ($ibx) = @_;
101                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
102                         "configured boost on $ibx->{name}");
103         });
104         lei_ok 'ls-external';
105         like($lei_out, qr/boost=0\n/s, 'ls-external has output');
106         lei_ok qw(add-external -q https://EXAMPLE.com/ibx), \'add remote';
107         is($lei_err, '', 'no warnings after add-external');
108
109         {
110                 lei_ok qw(ls-external --remote);
111                 my $r_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
112                 lei_ok qw(ls-external --local);
113                 my $l_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
114                 lei_ok 'ls-external';
115                 is_deeply([grep { $l_only->{$_} } keys %$r_only], [],
116                         'no locals in --remote');
117                 is_deeply([grep { $r_only->{$_} } keys %$l_only], [],
118                         'no remotes in --local');
119                 my $all = +{ map { $_ => 1 } split(/^/m, $lei_out) };
120                 is_deeply($all, { %$r_only, %$l_only },
121                                 'default output combines remote + local');
122                 lei_ok qw(ls-external --remote --local);
123                 my $both = +{ map { $_ => 1 } split(/^/m, $lei_out) };
124                 is_deeply($all, $both, '--remote --local == no args');
125         }
126
127         lei_ok qw(_complete lei forget-external), \'complete for externals';
128         my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
129         ok($comp{'https://example.com/ibx/'}, 'forget external completion');
130         my @dirs;
131         $cfg->each_inbox(sub {
132                 my ($ibx) = @_;
133                 push @dirs, $ibx->{inboxdir};
134                 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
135         });
136         for my $u (qw(h http https https: https:/ https:// https://e
137                         https://example https://example. https://example.co
138                         https://example.com https://example.com/
139                         https://example.com/i https://example.com/ibx)) {
140                 lei_ok(qw(_complete lei forget-external), $u,
141                         \"partial completion for URL $u");
142                 is($lei_out, "https://example.com/ibx/\n",
143                         "completed partial URL $u");
144                 for my $qo (qw(-I --include --exclude --only)) {
145                         lei_ok(qw(_complete lei q), $qo, $u,
146                                 \"partial completion for URL q $qo $u");
147                         is($lei_out, "https://example.com/ibx/\n",
148                                 "completed partial URL $u on q $qo");
149                 }
150         }
151         lei_ok(qw(_complete lei add-external), 'https://',
152                 \'add-external hostname completion');
153         is($lei_out, "https://example.com/\n", 'completed up to hostname');
154
155         lei_ok('ls-external');
156         like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
157         is($lei_err, '', 'no warnings on ls-external');
158         lei_ok(qw(forget-external -q https://EXAMPLE.com/ibx));
159         lei_ok('ls-external');
160         unlike($lei_out, qr!https://example\.com/ibx/!s,
161                 'removed canonical URL');
162
163         # do some queries
164         ok(!lei(qw(q s:prefix -o maildir:/dev/null)), 'bad maildir');
165         like($lei_err, qr!/dev/null exists and is not a directory!,
166                 'error shown');
167         is($? >> 8, 1, 'errored out with exit 1');
168
169         ok(!lei(qw(q s:prefix -o), "mboxcl2:$home"), 'bad mbox');
170         like($lei_err, qr!\Q$home\E exists and is not a writable file!,
171                 'error shown');
172         is($? >> 8, 1, 'errored out with exit 1');
173
174         ok(!lei(qw(q s:prefix -o Mbox2:/dev/stdout)), 'bad format');
175         like($lei_err, qr/bad mbox format: mbox2/, 'error shown');
176         is($? >> 8, 1, 'errored out with exit 1');
177
178         # note, on a Bourne shell users should be able to use either:
179         #       s:"use boolean prefix"
180         #       "s:use boolean prefix"
181         # or use single quotes, it should not matter.  Users only need
182         # to know shell quoting rules, not Xapian quoting rules.
183         # No double-quoting should be imposed on users on the CLI
184         lei_ok('q', 's:use boolean prefix');
185         like($lei_out, qr/search: use boolean prefix/,
186                 'phrase search got result');
187         my $res = json_utf8->decode($lei_out);
188         is(scalar(@$res), 2, 'only 2 element array (1 result)');
189         is($res->[1], undef, 'final element is undef'); # XXX should this be?
190         is(ref($res->[0]), 'HASH', 'first element is hashref');
191         lei_ok('q', '--pretty', 's:use boolean prefix');
192         my $pretty = json_utf8->decode($lei_out);
193         is_deeply($res, $pretty, '--pretty is identical after decode');
194
195         {
196                 open my $fh, '+>', undef or BAIL_OUT $!;
197                 $fh->autoflush(1);
198                 print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!;
199                 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
200                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh },
201                                 \'--stdin on regular file works');
202                 like($lei_out, qr/use boolean/, '--stdin on regular file');
203         }
204         {
205                 pipe(my ($r, $w)) or BAIL_OUT $!;
206                 print $w 's:use' or BAIL_OUT $!;
207                 close $w or BAIL_OUT $!;
208                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r },
209                                 \'--stdin on pipe file works');
210                 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
211         }
212         ok(!lei(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
213
214         for my $fmt (qw(ldjson ndjson jsonl)) {
215                 lei_ok('q', '-f', $fmt, 's:use boolean prefix');
216                 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
217         }
218
219         require IO::Uncompress::Gunzip;
220         for my $sfx ('', '.gz') {
221                 my $f = "$home/mbox$sfx";
222                 lei_ok('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
223                 my $cat = $sfx eq '' ? sub {
224                         open my $mb, '<', $f or fail "no mbox: $!";
225                         <$mb>
226                 } : sub {
227                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
228                         <$z>;
229                 };
230                 my @s = grep(/^Subject:/, $cat->());
231                 is(scalar(@s), 1, "1 result in mbox$sfx");
232                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
233                 is(grep(!/^#/, $lei_err), 0, 'no errors from augment') or
234                         diag $lei_err;
235                 @s = grep(/^Subject:/, my @wtf = $cat->());
236                 is(scalar(@s), 2, "2 results in mbox$sfx");
237
238                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
239                 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)");
240
241                 my @s2 = grep(/^Subject:/, $cat->());
242                 is_deeply(\@s2, \@s,
243                         "same 2 old results w/ --augment and bad search $sfx");
244
245                 lei_ok('q', '-o', "mboxcl2:$f", 's:nonexistent');
246                 my @res = $cat->();
247                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
248         }
249         ok(!lei('q', '-o', "$home/mbox", 's:nope'),
250                         'fails if mbox format unspecified');
251         ok(!lei(qw(q --no-local s:see)), '--no-local');
252         is($? >> 8, 1, 'proper exit code');
253         like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
254
255         for my $no (['--no-local'], ['--no-external'],
256                         [qw(--no-local --no-external)]) {
257                 lei_ok(qw(q mid:testmessage@example.com), @$no,
258                         '-I', $dirs[0], \"-I and @$no combine");
259                 $res = json_utf8->decode($lei_out);
260                 is($res->[0]->{'m'}, 'testmessage@example.com',
261                         "-I \$DIR got results regardless of @$no");
262         }
263
264         {
265                 opendir my $dh, '.' or BAIL_OUT "opendir(.) $!";
266                 my $od = PublicInbox::OnDestroy->new($$, sub {
267                         chdir $dh or BAIL_OUT "chdir: $!"
268                 });
269                 my @q = qw(q -o mboxcl2:rel.mboxcl2 bye);
270                 lei_ok('-C', $home, @q);
271                 is(unlink("$home/rel.mboxcl2"), 1, '-C works before q');
272
273                 # we are more flexible than git, here:
274                 lei_ok(@q, '-C', $home);
275                 is(unlink("$home/rel.mboxcl2"), 1, '-C works after q');
276                 mkdir "$home/deep" or BAIL_OUT $!;
277                 lei_ok('-C', $home, @q, '-C', 'deep');
278                 is(unlink("$home/deep/rel.mboxcl2"), 1, 'multiple -C works');
279
280                 lei_ok('-C', '', '-C', $home, @q, '-C', 'deep', '-C', '');
281                 is(unlink("$home/deep/rel.mboxcl2"), 1, "-C '' accepted");
282                 ok(!-f "$home/rel.mboxcl2", 'wrong path not created');
283         }
284         my %e = (
285                 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
286                 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
287         );
288         for my $k (keys %e) {
289                 my $url = $ENV{$k} // '';
290                 $url = $e{$k} if $url eq '1';
291                 $test_external_remote->($url, $k);
292         }
293 }); # test_lei
294 done_testing;