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);
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 my ($home, $for_destroy) = tmpdir();
16 my ($cmd, $env, $xopt) = @_;
19 ($env, $xopt) = grep { (!defined) || ref } @_;
20 $cmd = [ grep { defined && !ref } @_ ];
22 my $res = run_script(['lei', @$cmd], $env, $xopt // $opt);
24 $err = join('', grep(!/$err_filter/, split(/^/m, $err)));
28 delete local $ENV{XDG_DATA_HOME};
29 delete local $ENV{XDG_CONFIG_HOME};
30 local $ENV{GIT_COMMITTER_EMAIL} = 'lei@example.com';
31 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
32 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
33 local $ENV{HOME} = $home;
34 local $ENV{FOO} = 'BAR';
35 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
36 my $home_trash = [ "$home/.local", "$home/.config" ];
37 my $cleanup = sub { rmtree([@$home_trash, @_]) };
38 my $config_file = "$home/.config/lei/config";
39 my $store_dir = "$home/.local/share/lei";
42 ok(!$lei->(), 'no args fails');
43 is($? >> 8, 1, '$? is 1');
44 is($out, '', 'nothing in stdout');
45 like($err, qr/^usage:/sm, 'usage in stderr');
47 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
48 ok($lei->($arg), "lei @$arg");
49 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
50 is($err, '', "nothing in stderr (@$arg)");
53 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
54 ok(!$lei->($arg), "lei @$arg");
55 is($? >> 8, 1, '$? set correctly');
56 isnt($err, '', 'something in stderr');
57 is($out, '', 'nothing in stdout');
59 ok($lei->(qw(init -h)), 'init -h');
60 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
61 'actual path shown in init -h');
62 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }),
63 'init with XDG_DATA_HOME');
64 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
65 is($err, '', 'no errors from init -h');
67 ok($lei->(qw(config -h)), 'config-h');
68 like($out, qr! \Q$home\E/\.config/lei/config\b!,
69 'actual path shown in config -h');
70 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }),
71 'config with XDG_CONFIG_HOME');
72 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
73 is($err, '', 'no errors from config -h');
76 my $ok_err_info = sub {
78 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
79 diag "$msg: err=$err";
84 ok($lei->('init'), 'init w/o args');
85 $ok_err_info->('after init w/o args');
86 ok($lei->('init'), 'idempotent init w/o args');
87 $ok_err_info->('after idempotent init w/o args');
89 ok(!$lei->('init', "$home/x"), 'init conflict');
90 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
91 ok(!-e "$home/x", 'nothing created on conflict');
94 ok($lei->('init', "$home/x"), 'init conflict resolved');
95 $ok_err_info->('init w/ arg');
96 ok($lei->('init', "$home/x"), 'init idempotent w/ path');
97 $ok_err_info->('init idempotent w/ arg');
98 ok(-d "$home/x", 'created dir');
99 $cleanup->("$home/x");
101 ok(!$lei->('init', "$home/x", "$home/2"), 'too many args fails');
102 like($err, qr/too many/, 'noted excessive');
103 ok(!-e "$home/x", 'x not created on excessive');
104 for my $d (@$home_trash) {
105 my $base = (split(m!/!, $d))[-1];
106 ok(!-d $d, "$base not created");
108 is($out, '', 'nothing in stdout on init failure');
111 my $test_config = sub {
113 ok($lei->(qw(config a.b c)), 'config set var');
114 is($out.$err, '', 'no output on var set');
115 ok($lei->(qw(config -l)), 'config -l');
116 is($err, '', 'no errors on listing');
117 is($out, "a.b=c\n", 'got expected output');
118 ok(!$lei->(qw(config -f), "$home/.config/f", qw(x.y z)),
119 'config set var with -f fails');
120 like($err, qr/not supported/, 'not supported noted');
121 ok(!-f "$home/config/f", 'no file created');
124 my $setup_publicinboxes = sub {
126 return if $done eq $home;
127 use PublicInbox::InboxWritable;
129 run_script([qw(-init), "-V$V", "t$V",
130 '--newsgroup', "t.$V",
131 "$home/t$V", "http://example.com/t$V",
132 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
134 my $cfg = PublicInbox::Config->new;
136 $cfg->each_inbox(sub {
138 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
139 my $V = $ibx->version;
140 my @eml = glob('t/*.eml');
141 push(@eml, 't/data/0001.patch') if $V == 2;
143 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
144 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
149 run_script(['-index', $ibx->{inboxdir}]) or
154 $seen || BAIL_OUT 'no imports';
157 my $test_external = sub {
158 $setup_publicinboxes->();
160 $lei->('ls-external');
161 is($out.$err, '', 'ls-external no output, yet');
162 ok(!-e $config_file && !-e $store_dir,
163 'nothing created by ls-external');
165 my $cfg = PublicInbox::Config->new;
166 $cfg->each_inbox(sub {
168 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
170 is($out.$err, '', 'no output');
172 ok(-s $config_file && -e $store_dir,
173 'add-external created config + store');
174 my $lcfg = PublicInbox::Config->new($config_file);
175 $cfg->each_inbox(sub {
177 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
178 "configured boost on $ibx->{name}");
180 $lei->('ls-external');
181 like($out, qr/boost=0\n/s, 'ls-external has output');
183 # note, on a Bourne shell users should be able to use either:
184 # s:"use boolean prefix"
185 # "s:use boolean prefix"
186 # or use single quotes, it should not matter. Users only need
187 # to know shell quoting rules, not Xapian quoting rules.
188 # No double-quoting should be imposed on users on the CLI
189 $lei->('q', 's:use boolean prefix');
190 like($out, qr/search: use boolean prefix/, 'phrase search got result');
193 my $test_lei_common = sub {
200 if ($ENV{TEST_LEI_ONESHOT}) {
201 require_ok 'PublicInbox::LEI';
202 # force sun_path[108] overflow, ($lei->() filters out this path)
203 my $xrd = "$home/1shot-test".('.sun_path' x 108);
204 local $ENV{XDG_RUNTIME_DIR} = $xrd;
205 $err_filter = qr!\Q$xrd!;
206 $test_lei_common->();
209 SKIP: { # real socket
210 require_mods(qw(Cwd), my $nr = 105);
211 my $nfd = eval { require Socket::MsgHdr; 5 } // do {
212 require PublicInbox::Spawn;
213 PublicInbox::Spawn->can('send_cmd4') ? 5 : undef;
215 skip 'Socket::MsgHdr or Inline::C missing or unconfigured', $nr;
217 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
218 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.seq.sock";
220 ok($lei->('daemon-pid'), 'daemon-pid');
221 is($err, '', 'no error from daemon-pid');
222 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
223 chomp(my $pid = $out);
224 ok(kill(0, $pid), 'pid is valid');
225 ok(-S $sock, 'sock created');
227 $test_lei_common->();
229 ok($lei->('daemon-pid'), 'daemon-pid');
230 chomp(my $pid_again = $out);
231 is($pid, $pid_again, 'daemon-pid idempotent');
233 ok($lei->(qw(daemon-kill)), 'daemon-kill');
234 is($out, '', 'no output from daemon-kill');
235 is($err, '', 'no error from daemon-kill');
237 kill(0, $pid) or last;
240 ok(-S $sock, 'sock still exists');
241 ok(!kill(0, $pid), 'pid gone after stop');
243 ok($lei->(qw(daemon-pid)), 'daemon-pid');
244 chomp(my $new_pid = $out);
245 ok(kill(0, $new_pid), 'new pid is running');
246 ok(-S $sock, 'sock still exists');
248 for my $sig (qw(-0 -CHLD)) {
249 ok($lei->('daemon-kill', $sig), "handles $sig");
251 is($out.$err, '', 'no output on innocuous signals');
252 ok($lei->('daemon-pid'), 'daemon-pid');
254 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
256 if ('socket inaccessible') {
257 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
258 ok($lei->('help'), 'connect fail, one-shot fallback works');
259 like($err, qr/\bconnect\(/, 'connect error noted');
260 like($out, qr/^usage: /, 'help output works');
261 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
263 unlink $sock or BAIL_OUT "unlink($sock) $!";
265 kill('CHLD', $new_pid) or last;
268 ok(!kill(0, $new_pid), 'daemon exits after unlink');
269 # success over socket, can't test without