]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei query + pagination sorta working
[public-inbox.git] / t / lei.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;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 use File::Path qw(rmtree);
10 require_git 2.6;
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 my $lei = sub {
14         my ($cmd, $env, $xopt) = @_;
15         $out = $err = '';
16         if (!ref($cmd)) {
17                 ($env, $xopt) = grep { (!defined) || ref } @_;
18                 $cmd = [ grep { defined && !ref } @_ ];
19         }
20         run_script(['lei', @$cmd], $env, $xopt // $opt);
21 };
22
23 my ($home, $for_destroy) = tmpdir();
24 delete local $ENV{XDG_DATA_HOME};
25 delete local $ENV{XDG_CONFIG_HOME};
26 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
27 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
28 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
29 local $ENV{HOME} = $home;
30 local $ENV{FOO} = 'BAR';
31 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
32 my $home_trash = [ "$home/.local", "$home/.config" ];
33 my $cleanup = sub { rmtree([@$home_trash, @_]) };
34 my $config_file = "$home/.config/lei/config";
35 my $store_dir = "$home/.local/share/lei";
36
37 my $test_help = sub {
38         ok(!$lei->(), 'no args fails');
39         is($? >> 8, 1, '$? is 1');
40         is($out, '', 'nothing in stdout');
41         like($err, qr/^usage:/sm, 'usage in stderr');
42
43         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
44                 ok($lei->($arg), "lei @$arg");
45                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
46                 is($err, '', "nothing in stderr (@$arg)");
47         }
48
49         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
50                 ok(!$lei->($arg), "lei @$arg");
51                 is($? >> 8, 1, '$? set correctly');
52                 isnt($err, '', 'something in stderr');
53                 is($out, '', 'nothing in stdout');
54         }
55         ok($lei->(qw(init -h)), 'init -h');
56         like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
57                 'actual path shown in init -h');
58         ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
59                 'init with XDG_DATA_HOME');
60         like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
61         is($err, '', 'no errors from init -h');
62
63         ok($lei->(qw(config -h)), 'config-h');
64         like($out, qr! \Q$home\E/\.config/lei/config\b!,
65                 'actual path shown in config -h');
66         ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
67                 'config with XDG_CONFIG_HOME');
68         like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
69         is($err, '', 'no errors from config -h');
70 };
71
72 my $ok_err_info = sub {
73         my ($msg) = @_;
74         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
75                 diag "$msg: err=$err";
76 };
77
78 my $test_init = sub {
79         $cleanup->();
80         ok($lei->('init'), 'init w/o args');
81         $ok_err_info->('after init w/o args');
82         ok($lei->('init'), 'idempotent init w/o args');
83         $ok_err_info->('after idempotent init w/o args');
84
85         ok(!$lei->('init', "$home/x"), 'init conflict');
86         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
87         ok(!-e "$home/x", 'nothing created on conflict');
88         $cleanup->();
89
90         ok($lei->('init', "$home/x"), 'init conflict resolved');
91         $ok_err_info->('init w/ arg');
92         ok($lei->('init', "$home/x"), 'init idempotent w/ path');
93         $ok_err_info->('init idempotent w/ arg');
94         ok(-d "$home/x", 'created dir');
95         $cleanup->("$home/x");
96
97         ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
98         like($err, qr/too many/, 'noted excessive');
99         ok(!-e "$home/x", 'x not created on excessive');
100         for my $d (@$home_trash) {
101                 my $base = (split(m!/!, $d))[-1];
102                 ok(!-d $d, "$base not created");
103         }
104         is($out, '', 'nothing in stdout on init failure');
105 };
106
107 my $test_config = sub {
108         $cleanup->();
109         ok($lei->(qw(config a.b c)), 'config set var');
110         is($out.$err, '', 'no output on var set');
111         ok($lei->(qw(config -l)), 'config -l');
112         is($err, '', 'no errors on listing');
113         is($out, "a.b=c\n", 'got expected output');
114         ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
115                         'config set var with -f fails');
116         like($err, qr/not supported/, 'not supported noted');
117         ok(!-f "$home/config/f", 'no file created');
118 };
119
120 my $setup_publicinboxes = sub {
121         state $done = '';
122         return if $done eq $home;
123         use PublicInbox::InboxWritable;
124         for my $V (1, 2) {
125                 run_script([qw(-init), "-V$V", "t$V",
126                                 '--newsgroup', "t.$V",
127                                 "$home/t$V", "http://example.com/t$V",
128                                 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
129         }
130         my $cfg = PublicInbox::Config->new;
131         my $seen = 0;
132         $cfg->each_inbox(sub {
133                 my ($ibx) = @_;
134                 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
135                 my $V = $ibx->version;
136                 my @eml = glob('t/*.eml');
137                 push(@eml, 't/data/0001.patch') if $V == 2;
138                 for (@eml) {
139                         next if $_ eq 't/psgi_v2-old.eml'; # dup mid
140                         $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
141                         $seen++;
142                 }
143                 $im->done;
144                 if ($V == 1) {
145                         run_script(['-index', $ibx->{inboxdir}]) or
146                                 BAIL_OUT 'index v1';
147                 }
148         });
149         $done = $home;
150         $seen || BAIL_OUT 'no imports';
151 };
152
153 my $test_external = sub {
154         $setup_publicinboxes->();
155         $cleanup->();
156         $lei->('ls-external');
157         is($out.$err, '', 'ls-external no output, yet');
158         ok(!-e $config_file && !-e $store_dir,
159                 'nothing created by ls-external');
160
161         my $cfg = PublicInbox::Config->new;
162         $cfg->each_inbox(sub {
163                 my ($ibx) = @_;
164                 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
165                         'added external');
166                 is($out.$err, '', 'no output');
167         });
168         ok(-s $config_file && -e $store_dir,
169                 'add-external created config + store');
170         my $lcfg = PublicInbox::Config->new($config_file);
171         $cfg->each_inbox(sub {
172                 my ($ibx) = @_;
173                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
174                         "configured boost on $ibx->{name}");
175         });
176         $lei->('ls-external');
177         like($out, qr/boost=0\n/s, 'ls-external has output');
178
179         # note, on a Bourne shell users should be able to use either:
180         #       s:"use boolean prefix"
181         #       "s:use boolean prefix"
182         # or use single quotes, it should not matter.  Users only need
183         # to know shell quoting rules, not Xapian quoting rules.
184         # No double-quoting should be imposed on users on the CLI
185         $lei->('q', 's:use boolean prefix');
186         like($out, qr/search: use boolean prefix/, 'phrase search got result');
187 };
188
189 my $test_lei_common = sub {
190         $test_help->();
191         $test_config->();
192         $test_init->();
193         $test_external->();
194 };
195
196 if ($ENV{TEST_LEI_ONESHOT}) {
197         require_ok 'PublicInbox::LEI';
198         # force sun_path[108] overflow, "IO::FDPass" avoids warning
199         local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
200         $test_lei_common->();
201 }
202
203 SKIP: { # real socket
204         require_mods(qw(Cwd), my $nr = 105);
205         my $nfd = eval { require IO::FDPass; 1 } // do {
206                 require PublicInbox::Spawn;
207                 PublicInbox::Spawn->can('send_3fds') ? 3 : undef;
208         } //
209         skip 'IO::FDPass missing or Inline::C not installed/configured', $nr;
210
211         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
212         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.sock";
213
214         ok($lei->('daemon-pid'), 'daemon-pid');
215         is($err, '', 'no error from daemon-pid');
216         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
217         chomp(my $pid = $out);
218         ok(kill(0, $pid), 'pid is valid');
219         ok(-S $sock, 'sock created');
220
221         $test_lei_common->();
222
223         ok($lei->('daemon-pid'), 'daemon-pid');
224         chomp(my $pid_again = $out);
225         is($pid, $pid_again, 'daemon-pid idempotent');
226
227         ok($lei->(qw(daemon-kill)), 'daemon-kill');
228         is($out, '', 'no output from daemon-kill');
229         is($err, '', 'no error from daemon-kill');
230         for (0..100) {
231                 kill(0, $pid) or last;
232                 tick();
233         }
234         ok(!-S $sock, 'sock gone');
235         ok(!kill(0, $pid), 'pid gone after stop');
236
237         ok($lei->(qw(daemon-pid)), 'daemon-pid');
238         chomp(my $new_pid = $out);
239         ok(kill(0, $new_pid), 'new pid is running');
240         ok(-S $sock, 'sock exists again');
241
242         for my $sig (qw(-0 -CHLD)) {
243                 ok($lei->('daemon-kill', $sig), "handles $sig");
244         }
245         is($out.$err, '', 'no output on innocuous signals');
246         ok($lei->('daemon-pid'), 'daemon-pid');
247         chomp $out;
248         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
249
250         if ('socket inaccessible') {
251                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
252                 ok($lei->('help'), 'connect fail, one-shot fallback works');
253                 like($err, qr/\bconnect\(/, 'connect error noted');
254                 like($out, qr/^usage: /, 'help output works');
255                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
256         }
257         if ('oneshot on cwd gone') {
258                 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
259                 my $d = "$home/to-be-removed";
260                 my $lei_path = 'lei';
261                 # we chdir, so we need an abs_path fur run_script
262                 if (($ENV{TEST_RUN_MODE}//2) != 2) {
263                         $lei_path = PublicInbox::TestCommon::key2script('lei');
264                         $lei_path = Cwd::abs_path($lei_path);
265                 }
266                 mkdir $d or BAIL_OUT "mkdir($d) $!";
267                 chdir $d or BAIL_OUT "chdir($d) $!";
268                 if (rmdir($d)) {
269                         $out = $err = '';
270                         ok(run_script([$lei_path, 'help'], undef, $opt),
271                                 'cwd fail, one-shot fallback works');
272                 } else {
273                         $err = "rmdir=$!";
274                 }
275                 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
276                 like($err, qr/cwd\(/, 'cwd error noted');
277                 like($out, qr/^usage: /, 'help output still works');
278         }
279
280         unlink $sock or BAIL_OUT "unlink($sock) $!";
281         for (0..100) {
282                 kill('CHLD', $new_pid) or last;
283                 tick();
284         }
285         ok(!kill(0, $new_pid), 'daemon exits after unlink');
286         # success over socket, can't test without
287 };
288
289 done_testing;