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 = '') };
14 my ($cmd, $env, $xopt) = @_;
17 ($env, $xopt) = grep { (!defined) || ref } @_;
18 $cmd = [ grep { defined && !ref } @_ ];
20 run_script(['lei', @$cmd], $env, $xopt // $opt);
23 my ($home, $for_destroy) = tmpdir();
24 delete local $ENV{XDG_DATA_HOME};
25 delete local $ENV{XDG_CONFIG_HOME};
26 local $ENV{HOME} = $home;
27 local $ENV{FOO} = 'BAR';
28 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
29 my $home_trash = [ "$home/.local", "$home/.config" ];
30 my $cleanup = sub { rmtree([@$home_trash, @_]) };
31 my $config_file = "$home/.config/lei/config";
32 my $store_dir = "$home/.local/share/lei";
35 ok(!$lei->([], undef, $opt), 'no args fails');
36 is($? >> 8, 1, '$? is 1');
37 is($out, '', 'nothing in stdout');
38 like($err, qr/^usage:/sm, 'usage in stderr');
40 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
42 ok($lei->($arg, undef, $opt), "lei @$arg");
43 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
44 is($err, '', "nothing in stderr (@$arg)");
47 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
49 ok(!$lei->($arg, undef, $opt), "lei @$arg");
50 is($? >> 8, 1, '$? set correctly');
51 isnt($err, '', 'something in stderr');
52 is($out, '', 'nothing in stdout');
54 ok($lei->(qw(init -h), undef, $opt), 'init -h');
55 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
56 'actual path shown in init -h');
57 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
58 'init with XDG_DATA_HOME');
59 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
60 is($err, '', 'no errors from init -h');
62 ok($lei->(qw(config -h), undef, $opt), 'config-h');
63 like($out, qr! \Q$home\E/\.config/lei/config\b!,
64 'actual path shown in config -h');
65 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
66 'config with XDG_CONFIG_HOME');
67 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
68 is($err, '', 'no errors from config -h');
71 my $ok_err_info = sub {
73 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
74 diag "$msg: err=$err";
80 ok($lei->(['init'], undef, $opt), 'init w/o args');
81 $ok_err_info->('after init w/o args');
82 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
83 $ok_err_info->('after idempotent init w/o args');
85 ok(!$lei->(['init', "$home/x"], undef, $opt),
87 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
88 ok(!-e "$home/x", 'nothing created on conflict');
91 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
92 $ok_err_info->('init w/ arg');
93 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
94 $ok_err_info->('init idempotent w/ arg');
95 ok(-d "$home/x", 'created dir');
96 $cleanup->("$home/x");
98 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
99 'too many args fails');
100 like($err, qr/too many/, 'noted excessive');
101 ok(!-e "$home/x", 'x not created on excessive');
102 for my $d (@$home_trash) {
103 my $base = (split(m!/!, $d))[-1];
104 ok(!-d $d, "$base not created");
106 is($out, '', 'nothing in stdout on init failure');
109 my $test_config = sub {
111 ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
112 is($out.$err, '', 'no output on var set');
113 ok($lei->([qw(config -l)], undef, $opt), 'config -l');
114 is($err, '', 'no errors on listing');
115 is($out, "a.b=c\n", 'got expected output');
116 ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
117 'config set var with -f fails');
118 like($err, qr/not supported/, 'not supported noted');
119 ok(!-f "$home/config/f", 'no file created');
122 my $setup_publicinboxes = sub {
124 return if $done eq $home;
125 use PublicInbox::InboxWritable;
127 run_script([qw(-init -Lmedium), "-V$V", "t$V",
128 '--newsgroup', "t.$V",
129 "$home/t$V", "http://example.com/t$V",
130 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
132 my $cfg = PublicInbox::Config->new;
134 $cfg->each_inbox(sub {
136 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
137 my $V = $ibx->version;
138 my @eml = glob('t/*.eml');
139 push(@eml, 't/data/0001.patch') if $V == 2;
141 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
142 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
147 run_script(['-index', $ibx->{inboxdir}]) or
152 $seen || BAIL_OUT 'no imports';
155 my $test_external = sub {
156 $setup_publicinboxes->();
158 $lei->('ls-external');
159 is($out.$err, '', 'ls-external no output, yet');
160 ok(!-e $config_file && !-e $store_dir,
161 'nothing created by ls-external');
163 my $cfg = PublicInbox::Config->new;
164 $cfg->each_inbox(sub {
166 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
168 is($out.$err, '', 'no output');
170 ok(-s $config_file && -e $store_dir,
171 'add-external created config + store');
172 my $lcfg = PublicInbox::Config->new($config_file);
173 $cfg->each_inbox(sub {
175 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
176 "configured boost on $ibx->{name}");
178 $lei->('ls-external');
179 like($out, qr/boost=0\n/s, 'ls-external has output');
182 my $test_lei_common = sub {
189 if ($ENV{TEST_LEI_ONESHOT}) {
190 require_ok 'PublicInbox::LEI';
191 # force sun_path[108] overflow, "IO::FDPass" avoids warning
192 local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
193 $test_lei_common->();
196 SKIP: { # real socket
197 require_mods(qw(IO::FDPass Cwd), 46);
198 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
199 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
201 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
202 is($err, '', 'no error from daemon-pid');
203 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
204 chomp(my $pid = $out);
205 ok(kill(0, $pid), 'pid is valid');
206 ok(-S $sock, 'sock created');
208 $test_lei_common->();
211 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
212 chomp(my $pid_again = $out);
213 is($pid, $pid_again, 'daemon-pid idempotent');
216 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
217 is($err, '', 'no errors in env dump');
218 my @env = split(/\0/, $out);
219 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
220 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
221 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
224 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
225 is($out.$err, '', 'no output for unset');
226 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
227 is($err, '', 'no errors in env dump');
228 @env = split(/\0/, $out);
229 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
232 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
233 undef, $opt), 'unset multiple');
234 is($out.$err, '', 'no errors output for unset');
235 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
236 is($err, '', 'no errors in env dump');
237 @env = split(/\0/, $out);
238 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
240 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
241 is($out.$err, '', 'no output');
242 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
243 is($out, '', 'env cleared');
245 ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
246 is($out, '', 'no output from daemon-kill');
247 is($err, '', 'no error from daemon-kill');
249 kill(0, $pid) or last;
252 ok(!-S $sock, 'sock gone');
253 ok(!kill(0, $pid), 'pid gone after stop');
255 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
256 chomp(my $new_pid = $out);
257 ok(kill(0, $new_pid), 'new pid is running');
258 ok(-S $sock, 'sock exists again');
261 for my $sig (qw(-0 -CHLD)) {
262 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
265 is($out.$err, '', 'no output on innocuous signals');
266 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
268 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
270 if ('socket inaccessible') {
271 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
273 ok(run_script([qw(lei help)], undef, $opt),
274 'connect fail, one-shot fallback works');
275 like($err, qr/\bconnect\(/, 'connect error noted');
276 like($out, qr/^usage: /, 'help output works');
277 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
279 if ('oneshot on cwd gone') {
280 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
281 my $d = "$home/to-be-removed";
282 my $lei_path = 'lei';
283 # we chdir, so we need an abs_path fur run_script
284 if (($ENV{TEST_RUN_MODE}//2) != 2) {
285 $lei_path = PublicInbox::TestCommon::key2script('lei');
286 $lei_path = Cwd::abs_path($lei_path);
288 mkdir $d or BAIL_OUT "mkdir($d) $!";
289 chdir $d or BAIL_OUT "chdir($d) $!";
292 ok(run_script([$lei_path, 'help'], undef, $opt),
293 'cwd fail, one-shot fallback works');
297 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
298 like($err, qr/cwd\(/, 'cwd error noted');
299 like($out, qr/^usage: /, 'help output still works');
302 unlink $sock or BAIL_OUT "unlink($sock) $!";
304 kill('CHLD', $new_pid) or last;
307 ok(!kill(0, $new_pid), 'daemon exits after unlink');
308 # success over socket, can't test without