]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-externals.t
16241e02c30d18e35a2a70829a4aad5ddc5ebf87
[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 require_git 2.6;
8 require_mods(qw(json DBD::SQLite Search::Xapian));
9 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
10
11 my @onions = qw(http://hjrcffqmbrq6wope.onion/meta/
12         http://czquwvybam4bgbro.onion/meta/
13         http://ou63pmih66umazou.onion/meta/);
14
15 my $test_external_remote = sub {
16         my ($url, $k) = @_;
17 SKIP: {
18         skip "$k unset", 1 if !$url;
19         state $curl = which('curl');
20         $curl or skip 'no curl', 1;
21         which('torsocks') or skip 'no torsocks', 1 if $url =~ m!\.onion/!;
22         my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
23         my @cmd = ('q', '--only', $url, '-q', "m:$mid");
24         lei_ok(@cmd, \"query $url");
25         is($lei_err, '', "no errors on $url");
26         my $res = json_utf8->decode($lei_out);
27         is($res->[0]->{'m'}, $mid, "got expected mid from $url") or
28                 skip 'further remote tests', 1;
29         lei_ok(@cmd, 'd:..20101002', \'no results, no error');
30         is($lei_err, '', 'no output on 404, matching local FS behavior');
31         is($lei_out, "[null]\n", 'got null results');
32         my ($pid_before, $pid_after);
33         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
34                 lei_ok 'daemon-pid';
35                 chomp($pid_before = $lei_out);
36                 ok($pid_before, 'daemon is live');
37         }
38         for my $out ([], [qw(-f mboxcl2)]) {
39                 pipe(my ($r, $w)) or BAIL_OUT $!;
40                 open my $err, '+>', undef or BAIL_OUT $!;
41                 my $opt = { run_mode => 0, 1 => $w, 2 => $err };
42                 my $cmd = [qw(lei q -qt), @$out, 'z:1..'];
43                 my $tp = start_script($cmd, undef, $opt);
44                 close $w;
45                 sysread($r, my $buf, 1);
46                 close $r; # trigger SIGPIPE
47                 $tp->join;
48                 ok(WIFSIGNALED($?), "signaled @$out");
49                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
50                 seek($err, 0, 0);
51                 my @err = <$err>;
52                 is_deeply(\@err, [], "no errors @$out");
53         }
54         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
55                 lei_ok 'daemon-pid';
56                 chomp(my $pid_after = $lei_out);
57                 is($pid_after, $pid_before, 'pid unchanged') or
58                         skip 'daemon died', 1;
59                 skip 'not killing persistent lei-daemon', 2 if
60                                 $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
61                 lei_ok 'daemon-kill';
62                 my $alive = 1;
63                 for (1..100) {
64                         $alive = kill(0, $pid_after) or last;
65                         tick();
66                 }
67                 ok(!$alive, 'daemon-kill worked');
68         }
69 } # /SKIP
70 }; # /sub
71
72 my ($ro_home, $cfg_path) = setup_public_inboxes;
73 test_lei(sub {
74         my $home = $ENV{HOME};
75         my $config_file = "$home/.config/lei/config";
76         my $store_dir = "$home/.local/share/lei";
77         lei_ok 'ls-external', \'ls-external on fresh install';
78         is($lei_out.$lei_err, '', 'ls-external no output, yet');
79         ok(!-e $config_file && !-e $store_dir,
80                 'nothing created by ls-external');
81
82         ok(!lei('add-external', "$home/nonexistent"),
83                 "fails on non-existent dir");
84         like($lei_err, qr/not a directory/, 'noted non-existence');
85         mkdir "$home/new\nline" or BAIL_OUT "mkdir: $!";
86         ok(!lei('add-external', "$home/new\nline"), "fails on newline");
87         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
88         lei_ok('ls-external', \'ls-external works after add failure');
89         is($lei_out.$lei_err, '', 'ls-external still has no output');
90         my $cfg = PublicInbox::Config->new($cfg_path);
91         $cfg->each_inbox(sub {
92                 my ($ibx) = @_;
93                 lei_ok(qw(add-external -q), $ibx->{inboxdir},
94                                 \'added external');
95                 is($lei_out.$lei_err, '', 'no output');
96         });
97         ok(-s $config_file, 'add-external created config');
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                         or diag $lei_err;
241
242                 my @s2 = grep(/^Subject:/, $cat->());
243                 is_deeply(\@s2, \@s,
244                         "same 2 old results w/ --augment and bad search $sfx");
245
246                 lei_ok('q', '-o', "mboxcl2:$f", 's:nonexistent');
247                 my @res = $cat->();
248                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
249         }
250         ok(!lei('q', '-o', "$home/mbox", 's:nope'),
251                         'fails if mbox format unspecified');
252         ok(!lei(qw(q --no-local s:see)), '--no-local');
253         is($? >> 8, 1, 'proper exit code');
254         like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
255
256         for my $no (['--no-local'], ['--no-external'],
257                         [qw(--no-local --no-external)]) {
258                 lei_ok(qw(q mid:testmessage@example.com), @$no,
259                         '-I', $dirs[0], \"-I and @$no combine");
260                 $res = json_utf8->decode($lei_out);
261                 is($res->[0]->{'m'}, 'testmessage@example.com',
262                         "-I \$DIR got results regardless of @$no");
263         }
264
265         {
266                 skip 'TEST_LEI_DAEMON_PERSIST_DIR in use', 1 if
267                                         $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
268                 my @q = qw(q -o mboxcl2:rel.mboxcl2 bye);
269                 lei_ok('-C', $home, @q);
270                 is(unlink("$home/rel.mboxcl2"), 1, '-C works before q');
271
272                 # we are more flexible than git, here:
273                 lei_ok(@q, '-C', $home);
274                 is(unlink("$home/rel.mboxcl2"), 1, '-C works after q');
275                 mkdir "$home/deep" or BAIL_OUT $!;
276                 lei_ok('-C', $home, @q, '-C', 'deep');
277                 is(unlink("$home/deep/rel.mboxcl2"), 1, 'multiple -C works');
278
279                 lei_ok('-C', '', '-C', $home, @q, '-C', 'deep', '-C', '');
280                 is(unlink("$home/deep/rel.mboxcl2"), 1, "-C '' accepted");
281                 ok(!-f "$home/rel.mboxcl2", 'wrong path not created');
282         }
283         my %e = (
284                 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
285                 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
286         );
287         for my $k (keys %e) {
288                 my $url = $ENV{$k} // '';
289                 $url = $e{$k} if $url eq '1';
290                 $test_external_remote->($url, $k);
291         }
292 }); # test_lei
293 done_testing;