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>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 use File::Path qw(rmtree);
10 use Fcntl qw(SEEK_SET);
12 require_mods(qw(json DBD::SQLite Search::Xapian));
13 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my ($home, $for_destroy) = tmpdir();
17 my ($cmd, $env, $xopt) = @_;
20 ($env, $xopt) = grep { (!defined) || ref } @_;
21 $cmd = [ grep { defined && !ref } @_ ];
23 my $res = run_script(['lei', @$cmd], $env, $xopt // $opt);
25 $err = join('', grep(!/$err_filter/, split(/^/m, $err)));
29 delete local $ENV{XDG_DATA_HOME};
30 delete local $ENV{XDG_CONFIG_HOME};
31 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
32 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
33 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
34 local $ENV{HOME} = $home;
35 local $ENV{FOO} = 'BAR';
36 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
37 my $home_trash = [ "$home/.local", "$home/.config" ];
38 my $cleanup = sub { rmtree([@$home_trash, @_]) };
39 my $config_file = "$home/.config/lei/config";
40 my $store_dir = "$home/.local/share/lei";
43 ok(!$lei->(), 'no args fails');
44 is($? >> 8, 1, '$? is 1');
45 is($out, '', 'nothing in stdout');
46 like($err, qr/^usage:/sm, 'usage in stderr');
48 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
49 ok($lei->($arg), "lei @$arg");
50 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
51 is($err, '', "nothing in stderr (@$arg)");
54 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
55 ok(!$lei->($arg), "lei @$arg");
56 is($? >> 8, 1, '$? set correctly');
57 isnt($err, '', 'something in stderr');
58 is($out, '', 'nothing in stdout');
60 ok($lei->(qw(init -h)), 'init -h');
61 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
62 'actual path shown in init -h');
63 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
64 'init with XDG_DATA_HOME');
65 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
66 is($err, '', 'no errors from init -h');
68 ok($lei->(qw(config -h)), 'config-h');
69 like($out, qr! \Q$home\E/\.config/lei/config\b!,
70 'actual path shown in config -h');
71 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
72 'config with XDG_CONFIG_HOME');
73 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
74 is($err, '', 'no errors from config -h');
77 my $ok_err_info = sub {
79 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
80 diag "$msg: err=$err";
85 ok($lei->('init'), 'init w/o args');
86 $ok_err_info->('after init w/o args');
87 ok($lei->('init'), 'idempotent init w/o args');
88 $ok_err_info->('after idempotent init w/o args');
90 ok(!$lei->('init', "$home/x"), 'init conflict');
91 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
92 ok(!-e "$home/x", 'nothing created on conflict');
95 ok($lei->('init', "$home/x"), 'init conflict resolved');
96 $ok_err_info->('init w/ arg');
97 ok($lei->('init', "$home/x"), 'init idempotent w/ path');
98 $ok_err_info->('init idempotent w/ arg');
99 ok(-d "$home/x", 'created dir');
100 $cleanup->("$home/x");
102 ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
103 like($err, qr/too many/, 'noted excessive');
104 ok(!-e "$home/x", 'x not created on excessive');
105 for my $d (@$home_trash) {
106 my $base = (split(m!/!, $d))[-1];
107 ok(!-d $d, "$base not created");
109 is($out, '', 'nothing in stdout on init failure');
112 my $test_config = sub {
114 ok($lei->(qw(config a.b c)), 'config set var');
115 is($out.$err, '', 'no output on var set');
116 ok($lei->(qw(config -l)), 'config -l');
117 is($err, '', 'no errors on listing');
118 is($out, "a.b=c\n", 'got expected output');
119 ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
120 'config set var with -f fails');
121 like($err, qr/not supported/, 'not supported noted');
122 ok(!-f "$home/config/f", 'no file created');
125 my $setup_publicinboxes = sub {
127 return if $done eq $home;
128 use PublicInbox::InboxWritable;
130 run_script([qw(-init), "-V$V", "t$V",
131 '--newsgroup', "t.$V",
132 "$home/t$V", "http://example.com/t$V",
133 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
135 my $cfg = PublicInbox::Config->new;
137 $cfg->each_inbox(sub {
139 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
140 my $V = $ibx->version;
141 my @eml = glob('t/*.eml');
142 push(@eml, 't/data/0001.patch') if $V == 2;
144 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
145 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
150 run_script(['-index', $ibx->{inboxdir}]) or
155 $seen || BAIL_OUT 'no imports';
158 my $test_external = sub {
159 $setup_publicinboxes->();
161 $lei->('ls-external');
162 is($out.$err, '', 'ls-external no output, yet');
163 ok(!-e $config_file && !-e $store_dir,
164 'nothing created by ls-external');
166 my $cfg = PublicInbox::Config->new;
167 $cfg->each_inbox(sub {
169 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
171 is($out.$err, '', 'no output');
173 ok(-s $config_file && -e $store_dir,
174 'add-external created config + store');
175 my $lcfg = PublicInbox::Config->new($config_file);
176 $cfg->each_inbox(sub {
178 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
179 "configured boost on $ibx->{name}");
181 $lei->('ls-external');
182 like($out, qr/boost=0\n/s, 'ls-external has output');
184 # note, on a Bourne shell users should be able to use either:
185 # s:"use boolean prefix"
186 # "s:use boolean prefix"
187 # or use single quotes, it should not matter. Users only need
188 # to know shell quoting rules, not Xapian quoting rules.
189 # No double-quoting should be imposed on users on the CLI
190 $lei->('q', 's:use boolean prefix');
191 like($out, qr/search: use boolean prefix/, 'phrase search got result');
192 require IO::Uncompress::Gunzip;
193 for my $sfx ('', '.gz') {
194 my $f = "$home/mbox$sfx";
195 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
196 my $cat = $sfx eq '' ? sub {
197 open my $mb, '<', $f or fail "no mbox: $!";
200 my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
203 my @s = grep(/^Subject:/, $cat->());
204 is(scalar(@s), 1, "1 result in mbox$sfx");
205 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
206 is($err, '', 'no errors from augment');
207 @s = grep(/^Subject:/, my @wtf = $cat->());
208 is(scalar(@s), 2, "2 results in mbox$sfx");
210 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
211 is($err, '', "no errors on no results ($sfx)");
213 my @s2 = grep(/^Subject:/, $cat->());
215 "same 2 old results w/ --augment and bad search $sfx");
217 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
219 is_deeply(\@res, [], "clobber w/o --augment $sfx");
223 my $test_lei_common = sub {
230 if ($ENV{TEST_LEI_ONESHOT}) {
231 require_ok 'PublicInbox::LEI';
232 # force sun_path[108] overflow, ($lei->() filters out this path)
233 my $xrd = "$home/1shot-test".('.sun_path' x 108);
234 local $ENV{XDG_RUNTIME_DIR} = $xrd;
235 $err_filter = qr!\Q$xrd!;
236 $test_lei_common->();
239 SKIP: { # real socket
240 require_mods(qw(Cwd), my $nr = 105);
241 my $nfd = eval { require Socket::MsgHdr; 5 } // do {
242 require PublicInbox::Spawn;
243 PublicInbox::Spawn->can('send_cmd4') ? 5 : undef;
245 skip 'Socket::MsgHdr or Inline::C missing or unconfigured', $nr;
247 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
248 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.seq.sock";
250 ok($lei->('daemon-pid'), 'daemon-pid');
251 is($err, '', 'no error from daemon-pid');
252 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
253 chomp(my $pid = $out);
254 ok(kill(0, $pid), 'pid is valid');
255 ok(-S $sock, 'sock created');
257 $test_lei_common->();
259 ok($lei->('daemon-pid'), 'daemon-pid');
260 chomp(my $pid_again = $out);
261 is($pid, $pid_again, 'daemon-pid idempotent');
263 ok($lei->(qw(daemon-kill)), 'daemon-kill');
264 is($out, '', 'no output from daemon-kill');
265 is($err, '', 'no error from daemon-kill');
267 kill(0, $pid) or last;
270 ok(-S $sock, 'sock still exists');
271 ok(!kill(0, $pid), 'pid gone after stop');
273 ok($lei->(qw(daemon-pid)), 'daemon-pid');
274 chomp(my $new_pid = $out);
275 ok(kill(0, $new_pid), 'new pid is running');
276 ok(-S $sock, 'sock still exists');
278 for my $sig (qw(-0 -CHLD)) {
279 ok($lei->('daemon-kill', $sig), "handles $sig");
281 is($out.$err, '', 'no output on innocuous signals');
282 ok($lei->('daemon-pid'), 'daemon-pid');
284 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
286 if ('socket inaccessible') {
287 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
288 ok($lei->('help'), 'connect fail, one-shot fallback works');
289 like($err, qr/\bconnect\(/, 'connect error noted');
290 like($out, qr/^usage: /, 'help output works');
291 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
293 unlink $sock or BAIL_OUT "unlink($sock) $!";
295 kill('CHLD', $new_pid) or last;
298 ok(!kill(0, $new_pid), 'daemon exits after unlink');
299 # success over socket, can't test without