]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-externals.t
lei add-external: do not initialize writable store
[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, 'add-external created config');
97         my $lcfg = PublicInbox::Config->new($config_file);
98         $cfg->each_inbox(sub {
99                 my ($ibx) = @_;
100                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
101                         "configured boost on $ibx->{name}");
102         });
103         lei_ok 'ls-external';
104         like($lei_out, qr/boost=0\n/s, 'ls-external has output');
105         lei_ok qw(add-external -q https://EXAMPLE.com/ibx), \'add remote';
106         is($lei_err, '', 'no warnings after add-external');
107
108         {
109                 lei_ok qw(ls-external --remote);
110                 my $r_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
111                 lei_ok qw(ls-external --local);
112                 my $l_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
113                 lei_ok 'ls-external';
114                 is_deeply([grep { $l_only->{$_} } keys %$r_only], [],
115                         'no locals in --remote');
116                 is_deeply([grep { $r_only->{$_} } keys %$l_only], [],
117                         'no remotes in --local');
118                 my $all = +{ map { $_ => 1 } split(/^/m, $lei_out) };
119                 is_deeply($all, { %$r_only, %$l_only },
120                                 'default output combines remote + local');
121                 lei_ok qw(ls-external --remote --local);
122                 my $both = +{ map { $_ => 1 } split(/^/m, $lei_out) };
123                 is_deeply($all, $both, '--remote --local == no args');
124         }
125
126         lei_ok qw(_complete lei forget-external), \'complete for externals';
127         my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
128         ok($comp{'https://example.com/ibx/'}, 'forget external completion');
129         my @dirs;
130         $cfg->each_inbox(sub {
131                 my ($ibx) = @_;
132                 push @dirs, $ibx->{inboxdir};
133                 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
134         });
135         for my $u (qw(h http https https: https:/ https:// https://e
136                         https://example https://example. https://example.co
137                         https://example.com https://example.com/
138                         https://example.com/i https://example.com/ibx)) {
139                 lei_ok(qw(_complete lei forget-external), $u,
140                         \"partial completion for URL $u");
141                 is($lei_out, "https://example.com/ibx/\n",
142                         "completed partial URL $u");
143                 for my $qo (qw(-I --include --exclude --only)) {
144                         lei_ok(qw(_complete lei q), $qo, $u,
145                                 \"partial completion for URL q $qo $u");
146                         is($lei_out, "https://example.com/ibx/\n",
147                                 "completed partial URL $u on q $qo");
148                 }
149         }
150         lei_ok(qw(_complete lei add-external), 'https://',
151                 \'add-external hostname completion');
152         is($lei_out, "https://example.com/\n", 'completed up to hostname');
153
154         lei_ok('ls-external');
155         like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
156         is($lei_err, '', 'no warnings on ls-external');
157         lei_ok(qw(forget-external -q https://EXAMPLE.com/ibx));
158         lei_ok('ls-external');
159         unlike($lei_out, qr!https://example\.com/ibx/!s,
160                 'removed canonical URL');
161
162         # do some queries
163         ok(!lei(qw(q s:prefix -o maildir:/dev/null)), 'bad maildir');
164         like($lei_err, qr!/dev/null exists and is not a directory!,
165                 'error shown');
166         is($? >> 8, 1, 'errored out with exit 1');
167
168         ok(!lei(qw(q s:prefix -o), "mboxcl2:$home"), 'bad mbox');
169         like($lei_err, qr!\Q$home\E exists and is not a writable file!,
170                 'error shown');
171         is($? >> 8, 1, 'errored out with exit 1');
172
173         ok(!lei(qw(q s:prefix -o Mbox2:/dev/stdout)), 'bad format');
174         like($lei_err, qr/bad mbox format: mbox2/, 'error shown');
175         is($? >> 8, 1, 'errored out with exit 1');
176
177         # note, on a Bourne shell users should be able to use either:
178         #       s:"use boolean prefix"
179         #       "s:use boolean prefix"
180         # or use single quotes, it should not matter.  Users only need
181         # to know shell quoting rules, not Xapian quoting rules.
182         # No double-quoting should be imposed on users on the CLI
183         lei_ok('q', 's:use boolean prefix');
184         like($lei_out, qr/search: use boolean prefix/,
185                 'phrase search got result');
186         my $res = json_utf8->decode($lei_out);
187         is(scalar(@$res), 2, 'only 2 element array (1 result)');
188         is($res->[1], undef, 'final element is undef'); # XXX should this be?
189         is(ref($res->[0]), 'HASH', 'first element is hashref');
190         lei_ok('q', '--pretty', 's:use boolean prefix');
191         my $pretty = json_utf8->decode($lei_out);
192         is_deeply($res, $pretty, '--pretty is identical after decode');
193
194         {
195                 open my $fh, '+>', undef or BAIL_OUT $!;
196                 $fh->autoflush(1);
197                 print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!;
198                 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
199                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh },
200                                 \'--stdin on regular file works');
201                 like($lei_out, qr/use boolean/, '--stdin on regular file');
202         }
203         {
204                 pipe(my ($r, $w)) or BAIL_OUT $!;
205                 print $w 's:use' or BAIL_OUT $!;
206                 close $w or BAIL_OUT $!;
207                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r },
208                                 \'--stdin on pipe file works');
209                 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
210         }
211         ok(!lei(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
212
213         for my $fmt (qw(ldjson ndjson jsonl)) {
214                 lei_ok('q', '-f', $fmt, 's:use boolean prefix');
215                 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
216         }
217
218         require IO::Uncompress::Gunzip;
219         for my $sfx ('', '.gz') {
220                 my $f = "$home/mbox$sfx";
221                 lei_ok('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
222                 my $cat = $sfx eq '' ? sub {
223                         open my $mb, '<', $f or fail "no mbox: $!";
224                         <$mb>
225                 } : sub {
226                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
227                         <$z>;
228                 };
229                 my @s = grep(/^Subject:/, $cat->());
230                 is(scalar(@s), 1, "1 result in mbox$sfx");
231                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
232                 is(grep(!/^#/, $lei_err), 0, 'no errors from augment') or
233                         diag $lei_err;
234                 @s = grep(/^Subject:/, my @wtf = $cat->());
235                 is(scalar(@s), 2, "2 results in mbox$sfx");
236
237                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
238                 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)")
239                         or diag $lei_err;
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;