2 # Copyright (C) 2020 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));
13 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
15 my ($cmd, $env, $xopt) = @_;
18 ($env, $xopt) = grep { (!defined) || ref } @_;
19 $cmd = [ grep { defined && !ref } @_ ];
21 run_script([$LEI, @$cmd], $env, $xopt // $opt);
24 my ($home, $for_destroy) = tmpdir();
25 delete local $ENV{XDG_DATA_HOME};
26 delete local $ENV{XDG_CONFIG_HOME};
27 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
28 local $ENV{HOME} = $home;
29 local $ENV{FOO} = 'BAR';
30 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
31 my $home_trash = [ "$home/.local", "$home/.config" ];
32 my $cleanup = sub { rmtree([@$home_trash, @_]) };
33 my $config_file = "$home/.config/lei/config";
34 my $store_dir = "$home/.local/share/lei";
37 ok(!$lei->([], undef, $opt), 'no args fails');
38 is($? >> 8, 1, '$? is 1');
39 is($out, '', 'nothing in stdout');
40 like($err, qr/^usage:/sm, 'usage in stderr');
42 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
44 ok($lei->($arg, undef, $opt), "lei @$arg");
45 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
46 is($err, '', "nothing in stderr (@$arg)");
49 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
51 ok(!$lei->($arg, undef, $opt), "lei @$arg");
52 is($? >> 8, 1, '$? set correctly');
53 isnt($err, '', 'something in stderr');
54 is($out, '', 'nothing in stdout');
56 ok($lei->(qw(init -h), undef, $opt), 'init -h');
57 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
58 'actual path shown in init -h');
59 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
60 'init with XDG_DATA_HOME');
61 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
62 is($err, '', 'no errors from init -h');
64 ok($lei->(qw(config -h), undef, $opt), 'config-h');
65 like($out, qr! \Q$home\E/\.config/lei/config\b!,
66 'actual path shown in config -h');
67 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
68 'config with XDG_CONFIG_HOME');
69 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
70 is($err, '', 'no errors from config -h');
73 my $ok_err_info = sub {
75 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
76 diag "$msg: err=$err";
82 ok($lei->(['init'], undef, $opt), 'init w/o args');
83 $ok_err_info->('after init w/o args');
84 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
85 $ok_err_info->('after idempotent init w/o args');
87 ok(!$lei->(['init', "$home/x"], undef, $opt),
89 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
90 ok(!-e "$home/x", 'nothing created on conflict');
93 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
94 $ok_err_info->('init w/ arg');
95 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
96 $ok_err_info->('init idempotent w/ arg');
97 ok(-d "$home/x", 'created dir');
98 $cleanup->("$home/x");
100 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
101 '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)], undef, $opt), 'config set var');
114 is($out.$err, '', 'no output on var set');
115 ok($lei->([qw(config -l)], undef, $opt), '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)], undef, $opt),
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 -Lmedium), "-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_extinbox = sub {
158 $setup_publicinboxes->();
160 $lei->('ls-extinbox');
161 is($out.$err, '', 'ls-extinbox no output, yet');
162 ok(!-e $config_file && !-e $store_dir,
163 'nothing created by ls-extinbox');
165 my $cfg = PublicInbox::Config->new;
166 $cfg->each_inbox(sub {
168 ok($lei->(qw(add-extinbox -q), $ibx->{inboxdir}),
170 is($out.$err, '', 'no output');
172 ok(-s $config_file && -e $store_dir,
173 'add-extinbox created config + store');
174 my $lcfg = PublicInbox::Config->new($config_file);
175 $cfg->each_inbox(sub {
177 is($lcfg->{"extinbox.$ibx->{inboxdir}.boost"}, 0,
178 "configured boost on $ibx->{name}");
180 $lei->('ls-extinbox');
181 like($out, qr/boost=0\n/s, 'ls-extinbox has output');
184 my $test_lei_common = sub {
191 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
193 last SKIP if $test_lei_oneshot;
194 require_mods(qw(IO::FDPass Cwd), 46);
195 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
197 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
198 is($err, '', 'no error from daemon-pid');
199 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
200 chomp(my $pid = $out);
201 ok(kill(0, $pid), 'pid is valid');
202 ok(-S $sock, 'sock created');
204 $test_lei_common->();
207 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
208 chomp(my $pid_again = $out);
209 is($pid, $pid_again, 'daemon-pid idempotent');
212 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
213 is($err, '', 'no errors in env dump');
214 my @env = split(/\0/, $out);
215 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
216 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
217 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
220 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
221 is($out.$err, '', 'no output for unset');
222 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
223 is($err, '', 'no errors in env dump');
224 @env = split(/\0/, $out);
225 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
228 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
229 undef, $opt), 'unset multiple');
230 is($out.$err, '', 'no errors output for unset');
231 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
232 is($err, '', 'no errors in env dump');
233 @env = split(/\0/, $out);
234 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
236 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
237 is($out.$err, '', 'no output');
238 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
239 is($out, '', 'env cleared');
241 ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
242 is($out, '', 'no output from daemon-kill');
243 is($err, '', 'no error from daemon-kill');
245 kill(0, $pid) or last;
248 ok(!-S $sock, 'sock gone');
249 ok(!kill(0, $pid), 'pid gone after stop');
251 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
252 chomp(my $new_pid = $out);
253 ok(kill(0, $new_pid), 'new pid is running');
254 ok(-S $sock, 'sock exists again');
257 for my $sig (qw(-0 -CHLD)) {
258 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
261 is($out.$err, '', 'no output on innocuous signals');
262 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
264 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
266 if ('socket inaccessible') {
267 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
269 ok(run_script([qw(lei help)], undef, $opt),
270 'connect fail, one-shot fallback works');
271 like($err, qr/\bconnect\(/, 'connect error noted');
272 like($out, qr/^usage: /, 'help output works');
273 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
275 if ('oneshot on cwd gone') {
276 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
277 my $d = "$home/to-be-removed";
278 mkdir $d or BAIL_OUT "mkdir($d) $!";
279 chdir $d or BAIL_OUT "chdir($d) $!";
282 ok(run_script([qw(lei help)], undef, $opt),
283 'cwd fail, one-shot fallback works');
287 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
288 like($err, qr/cwd\(/, 'cwd error noted');
289 like($out, qr/^usage: /, 'help output still works');
292 unlink $sock or BAIL_OUT "unlink($sock) $!";
294 kill('CHLD', $new_pid) or last;
297 ok(!kill(0, $new_pid), 'daemon exits after unlink');
298 # success over socket, can't test without
299 $test_lei_common = undef;
302 require_ok 'PublicInbox::LEI';
303 $LEI = 'lei-oneshot' if $test_lei_oneshot;
304 $test_lei_common->() if $test_lei_common;