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 ok(!$lei->(qw(q s:prefix -o /dev/null -f maildir)), 'bad maildir');
185 like($err, qr!/dev/null exists and is not a directory!,
187 is($? >> 8, 1, 'errored out with exit 1');
189 ok(!$lei->(qw(q s:prefix -f mboxcl2 -o), $home), 'bad mbox');
190 like($err, qr!\Q$home\E exists and is not a regular file!,
192 is($? >> 8, 1, 'errored out with exit 1');
194 ok(!$lei->(qw(q s:prefix -o /dev/stdout -f Mbox2)), 'bad format');
195 like($err, qr/bad mbox --format=mbox2/, 'error shown');
196 is($? >> 8, 1, 'errored out with exit 1');
198 # note, on a Bourne shell users should be able to use either:
199 # s:"use boolean prefix"
200 # "s:use boolean prefix"
201 # or use single quotes, it should not matter. Users only need
202 # to know shell quoting rules, not Xapian quoting rules.
203 # No double-quoting should be imposed on users on the CLI
204 $lei->('q', 's:use boolean prefix');
205 like($out, qr/search: use boolean prefix/, 'phrase search got result');
206 require IO::Uncompress::Gunzip;
207 for my $sfx ('', '.gz') {
208 my $f = "$home/mbox$sfx";
209 $lei->('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
210 my $cat = $sfx eq '' ? sub {
211 open my $mb, '<', $f or fail "no mbox: $!";
214 my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
217 my @s = grep(/^Subject:/, $cat->());
218 is(scalar(@s), 1, "1 result in mbox$sfx");
219 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
220 is($err, '', 'no errors from augment');
221 @s = grep(/^Subject:/, my @wtf = $cat->());
222 is(scalar(@s), 2, "2 results in mbox$sfx");
224 $lei->('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
225 is($err, '', "no errors on no results ($sfx)");
227 my @s2 = grep(/^Subject:/, $cat->());
229 "same 2 old results w/ --augment and bad search $sfx");
231 $lei->('q', '-o', "mboxcl2:$f", 's:nonexistent');
233 is_deeply(\@res, [], "clobber w/o --augment $sfx");
237 my $test_lei_common = sub {
244 if ($ENV{TEST_LEI_ONESHOT}) {
245 require_ok 'PublicInbox::LEI';
246 # force sun_path[108] overflow, ($lei->() filters out this path)
247 my $xrd = "$home/1shot-test".('.sun_path' x 108);
248 local $ENV{XDG_RUNTIME_DIR} = $xrd;
249 $err_filter = qr!\Q$xrd!;
250 $test_lei_common->();
252 SKIP: { # real socket
253 eval { require Socket::MsgHdr; 1 } // do {
254 require PublicInbox::Spawn;
255 PublicInbox::Spawn->can('send_cmd4');
256 } // skip 'Socket::MsgHdr or Inline::C missing or unconfigured', 115;
257 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
258 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/5.seq.sock";
260 ok($lei->('daemon-pid'), 'daemon-pid');
261 is($err, '', 'no error from daemon-pid');
262 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
263 chomp(my $pid = $out);
264 ok(kill(0, $pid), 'pid is valid');
265 ok(-S $sock, 'sock created');
267 $test_lei_common->();
269 ok($lei->('daemon-pid'), 'daemon-pid');
270 chomp(my $pid_again = $out);
271 is($pid, $pid_again, 'daemon-pid idempotent');
273 ok($lei->(qw(daemon-kill)), 'daemon-kill');
274 is($out, '', 'no output from daemon-kill');
275 is($err, '', 'no error from daemon-kill');
277 kill(0, $pid) or last;
280 ok(-S $sock, 'sock still exists');
281 ok(!kill(0, $pid), 'pid gone after stop');
283 ok($lei->(qw(daemon-pid)), 'daemon-pid');
284 chomp(my $new_pid = $out);
285 ok(kill(0, $new_pid), 'new pid is running');
286 ok(-S $sock, 'sock still exists');
288 for my $sig (qw(-0 -CHLD)) {
289 ok($lei->('daemon-kill', $sig), "handles $sig");
291 is($out.$err, '', 'no output on innocuous signals');
292 ok($lei->('daemon-pid'), 'daemon-pid');
294 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
296 if ('socket inaccessible') {
297 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
298 ok($lei->('help'), 'connect fail, one-shot fallback works');
299 like($err, qr/\bconnect\(/, 'connect error noted');
300 like($out, qr/^usage: /, 'help output works');
301 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
303 unlink $sock or BAIL_OUT "unlink($sock) $!";
305 kill('CHLD', $new_pid) or last;
308 ok(!kill(0, $new_pid), 'daemon exits after unlink');
309 # success over socket, can't test without