]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-externals.t
lei: support "-C" to chdir in all sub commands
[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(DBD::SQLite Search::Xapian));
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         my $nr = 5;
19         skip "$k unset", $nr if !$url;
20         which('curl') or skip 'no curl', $nr;
21         which('torsocks') or skip 'no torsocks', $nr 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");
28         lei_ok(@cmd, 'd:..20101002', \'no results, no error');
29         is($lei_err, '', 'no output on 404, matching local FS behavior');
30         is($lei_out, "[null]\n", 'got null results');
31 } # /SKIP
32 }; # /sub
33
34 my ($ro_home, $cfg_path) = setup_public_inboxes;
35 test_lei(sub {
36         my $home = $ENV{HOME};
37         my $config_file = "$home/.config/lei/config";
38         my $store_dir = "$home/.local/share/lei";
39         lei_ok 'ls-external', \'ls-external on fresh install';
40         is($lei_out.$lei_err, '', 'ls-external no output, yet');
41         ok(!-e $config_file && !-e $store_dir,
42                 'nothing created by ls-external');
43
44         ok(!lei('add-external', "$home/nonexistent",
45                 "fails on non-existent dir"));
46         lei_ok('ls-external', \'ls-external works after add failure');
47         is($lei_out.$lei_err, '', 'ls-external still has no output');
48         my $cfg = PublicInbox::Config->new($cfg_path);
49         $cfg->each_inbox(sub {
50                 my ($ibx) = @_;
51                 lei_ok(qw(add-external -q), $ibx->{inboxdir},
52                                 \'added external');
53                 is($lei_out.$lei_err, '', 'no output');
54         });
55         ok(-s $config_file && -e $store_dir,
56                 'add-external created config + store');
57         my $lcfg = PublicInbox::Config->new($config_file);
58         $cfg->each_inbox(sub {
59                 my ($ibx) = @_;
60                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
61                         "configured boost on $ibx->{name}");
62         });
63         lei_ok 'ls-external';
64         like($lei_out, qr/boost=0\n/s, 'ls-external has output');
65         lei_ok qw(add-external -q https://EXAMPLE.com/ibx), \'add remote';
66         is($lei_err, '', 'no warnings after add-external');
67
68         {
69                 lei_ok qw(ls-external --remote);
70                 my $r_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
71                 lei_ok qw(ls-external --local);
72                 my $l_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
73                 lei_ok 'ls-external';
74                 is_deeply([grep { $l_only->{$_} } keys %$r_only], [],
75                         'no locals in --remote');
76                 is_deeply([grep { $r_only->{$_} } keys %$l_only], [],
77                         'no remotes in --local');
78                 my $all = +{ map { $_ => 1 } split(/^/m, $lei_out) };
79                 is_deeply($all, { %$r_only, %$l_only },
80                                 'default output combines remote + local');
81                 lei_ok qw(ls-external --remote --local);
82                 my $both = +{ map { $_ => 1 } split(/^/m, $lei_out) };
83                 is_deeply($all, $both, '--remote --local == no args');
84         }
85
86         lei_ok qw(_complete lei forget-external), \'complete for externals';
87         my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
88         ok($comp{'https://example.com/ibx/'}, 'forget external completion');
89         $cfg->each_inbox(sub {
90                 my ($ibx) = @_;
91                 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
92         });
93         for my $u (qw(h http https https: https:/ https:// https://e
94                         https://example https://example. https://example.co
95                         https://example.com https://example.com/
96                         https://example.com/i https://example.com/ibx)) {
97                 lei_ok(qw(_complete lei forget-external), $u,
98                         \"partial completion for URL $u");
99                 is($lei_out, "https://example.com/ibx/\n",
100                         "completed partial URL $u");
101                 for my $qo (qw(-I --include --exclude --only)) {
102                         lei_ok(qw(_complete lei q), $qo, $u,
103                                 \"partial completion for URL q $qo $u");
104                         is($lei_out, "https://example.com/ibx/\n",
105                                 "completed partial URL $u on q $qo");
106                 }
107         }
108         lei_ok(qw(_complete lei add-external), 'https://',
109                 \'add-external hostname completion');
110         is($lei_out, "https://example.com/\n", 'completed up to hostname');
111
112         lei_ok('ls-external');
113         like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
114         is($lei_err, '', 'no warnings on ls-external');
115         lei_ok(qw(forget-external -q https://EXAMPLE.com/ibx));
116         lei_ok('ls-external');
117         unlike($lei_out, qr!https://example\.com/ibx/!s,
118                 'removed canonical URL');
119 SKIP: {
120         ok(!lei(qw(q s:prefix -o maildir:/dev/null)), 'bad maildir');
121         like($lei_err, qr!/dev/null exists and is not a directory!,
122                 'error shown');
123         is($? >> 8, 1, 'errored out with exit 1');
124
125         ok(!lei(qw(q s:prefix -o), "mboxcl2:$home"), 'bad mbox');
126         like($lei_err, qr!\Q$home\E exists and is not a writable file!,
127                 'error shown');
128         is($? >> 8, 1, 'errored out with exit 1');
129
130         ok(!lei(qw(q s:prefix -o Mbox2:/dev/stdout)), 'bad format');
131         like($lei_err, qr/bad mbox format: mbox2/, 'error shown');
132         is($? >> 8, 1, 'errored out with exit 1');
133
134         # note, on a Bourne shell users should be able to use either:
135         #       s:"use boolean prefix"
136         #       "s:use boolean prefix"
137         # or use single quotes, it should not matter.  Users only need
138         # to know shell quoting rules, not Xapian quoting rules.
139         # No double-quoting should be imposed on users on the CLI
140         lei_ok('q', 's:use boolean prefix');
141         like($lei_out, qr/search: use boolean prefix/,
142                 'phrase search got result');
143         my $res = json_utf8->decode($lei_out);
144         is(scalar(@$res), 2, 'only 2 element array (1 result)');
145         is($res->[1], undef, 'final element is undef'); # XXX should this be?
146         is(ref($res->[0]), 'HASH', 'first element is hashref');
147         lei_ok('q', '--pretty', 's:use boolean prefix');
148         my $pretty = json_utf8->decode($lei_out);
149         is_deeply($res, $pretty, '--pretty is identical after decode');
150
151         {
152                 open my $fh, '+>', undef or BAIL_OUT $!;
153                 $fh->autoflush(1);
154                 print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!;
155                 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
156                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh },
157                                 \'--stdin on regular file works');
158                 like($lei_out, qr/use boolean/, '--stdin on regular file');
159         }
160         {
161                 pipe(my ($r, $w)) or BAIL_OUT $!;
162                 print $w 's:use' or BAIL_OUT $!;
163                 close $w or BAIL_OUT $!;
164                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r },
165                                 \'--stdin on pipe file works');
166                 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
167         }
168         ok(!lei(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
169
170         for my $fmt (qw(ldjson ndjson jsonl)) {
171                 lei_ok('q', '-f', $fmt, 's:use boolean prefix');
172                 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
173         }
174
175         require IO::Uncompress::Gunzip;
176         for my $sfx ('', '.gz') {
177                 my $f = "$home/mbox$sfx";
178                 lei_ok('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
179                 my $cat = $sfx eq '' ? sub {
180                         open my $mb, '<', $f or fail "no mbox: $!";
181                         <$mb>
182                 } : sub {
183                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
184                         <$z>;
185                 };
186                 my @s = grep(/^Subject:/, $cat->());
187                 is(scalar(@s), 1, "1 result in mbox$sfx");
188                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
189                 is(grep(!/^#/, $lei_err), 0, 'no errors from augment') or
190                         diag $lei_err;
191                 @s = grep(/^Subject:/, my @wtf = $cat->());
192                 is(scalar(@s), 2, "2 results in mbox$sfx");
193
194                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
195                 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)");
196
197                 my @s2 = grep(/^Subject:/, $cat->());
198                 is_deeply(\@s2, \@s,
199                         "same 2 old results w/ --augment and bad search $sfx");
200
201                 lei_ok('q', '-o', "mboxcl2:$f", 's:nonexistent');
202                 my @res = $cat->();
203                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
204         }
205         ok(!lei('q', '-o', "$home/mbox", 's:nope'),
206                         'fails if mbox format unspecified');
207         ok(!lei(qw(q --no-local s:see)), '--no-local');
208         is($? >> 8, 1, 'proper exit code');
209         like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
210
211         {
212                 opendir my $dh, '.' or BAIL_OUT "opendir(.) $!";
213                 my $od = PublicInbox::OnDestroy->new($$, sub {
214                         chdir $dh or BAIL_OUT "chdir: $!"
215                 });
216                 my @q = qw(q -o mboxcl2:rel.mboxcl2 bye);
217                 lei_ok('-C', $home, @q);
218                 is(unlink("$home/rel.mboxcl2"), 1, '-C works before q');
219
220                 # we are more flexible than git, here:
221                 lei_ok(@q, '-C', $home);
222                 is(unlink("$home/rel.mboxcl2"), 1, '-C works after q');
223                 mkdir "$home/deep" or BAIL_OUT $!;
224                 lei_ok('-C', $home, @q, '-C', 'deep');
225                 is(unlink("$home/deep/rel.mboxcl2"), 1, 'multiple -C works');
226
227                 lei_ok('-C', '', '-C', $home, @q, '-C', 'deep', '-C', '');
228                 is(unlink("$home/deep/rel.mboxcl2"), 1, "-C '' accepted");
229                 ok(!-f "$home/rel.mboxcl2", 'wrong path not created');
230         }
231         my %e = (
232                 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
233                 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
234         );
235         for my $k (keys %e) {
236                 my $url = $ENV{$k} // '';
237                 $url = $e{$k} if $url eq '1';
238                 $test_external_remote->($url, $k);
239         }
240         }; # /SKIP
241 }); # test_lei
242 done_testing;