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{GIT_COMMITTER_EMAIL} = 'lei@example.com';
27 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
28 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
29 local $ENV{HOME} = $home;
30 local $ENV{FOO} = 'BAR';
31 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
32 my $home_trash = [ "$home/.local", "$home/.config" ];
33 my $cleanup = sub { rmtree([@$home_trash, @_]) };
34 my $config_file = "$home/.config/lei/config";
35 my $store_dir = "$home/.local/share/lei";
38 ok(!$lei->([], undef, $opt), 'no args fails');
39 is($? >> 8, 1, '$? is 1');
40 is($out, '', 'nothing in stdout');
41 like($err, qr/^usage:/sm, 'usage in stderr');
43 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
45 ok($lei->($arg, undef, $opt), "lei @$arg");
46 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
47 is($err, '', "nothing in stderr (@$arg)");
50 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
52 ok(!$lei->($arg, undef, $opt), "lei @$arg");
53 is($? >> 8, 1, '$? set correctly');
54 isnt($err, '', 'something in stderr');
55 is($out, '', 'nothing in stdout');
57 ok($lei->(qw(init -h), undef, $opt), 'init -h');
58 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
59 'actual path shown in init -h');
60 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
61 'init with XDG_DATA_HOME');
62 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
63 is($err, '', 'no errors from init -h');
65 ok($lei->(qw(config -h), undef, $opt), 'config-h');
66 like($out, qr! \Q$home\E/\.config/lei/config\b!,
67 'actual path shown in config -h');
68 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
69 'config with XDG_CONFIG_HOME');
70 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
71 is($err, '', 'no errors from config -h');
74 my $ok_err_info = sub {
76 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
77 diag "$msg: err=$err";
83 ok($lei->(['init'], undef, $opt), 'init w/o args');
84 $ok_err_info->('after init w/o args');
85 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
86 $ok_err_info->('after idempotent init w/o args');
88 ok(!$lei->(['init', "$home/x"], undef, $opt),
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"], undef, $opt), 'init conflict resolved');
95 $ok_err_info->('init w/ arg');
96 ok($lei->(['init', "$home/x"], undef, $opt), '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" ], undef, $opt),
102 '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)], undef, $opt), 'config set var');
115 is($out.$err, '', 'no output on var set');
116 ok($lei->([qw(config -l)], undef, $opt), '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)], undef, $opt),
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 -Lmedium), "-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');
185 my $test_lei_common = sub {
192 if ($ENV{TEST_LEI_ONESHOT}) {
193 require_ok 'PublicInbox::LEI';
194 # force sun_path[108] overflow, "IO::FDPass" avoids warning
195 local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
196 $test_lei_common->();
199 SKIP: { # real socket
200 require_mods(qw(IO::FDPass Cwd), 46);
201 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
202 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
204 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
205 is($err, '', 'no error from daemon-pid');
206 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
207 chomp(my $pid = $out);
208 ok(kill(0, $pid), 'pid is valid');
209 ok(-S $sock, 'sock created');
211 $test_lei_common->();
214 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
215 chomp(my $pid_again = $out);
216 is($pid, $pid_again, 'daemon-pid idempotent');
219 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
220 is($err, '', 'no errors in env dump');
221 my @env = split(/\0/, $out);
222 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
223 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
224 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
227 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
228 is($out.$err, '', 'no output for unset');
229 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
230 is($err, '', 'no errors in env dump');
231 @env = split(/\0/, $out);
232 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
235 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
236 undef, $opt), 'unset multiple');
237 is($out.$err, '', 'no errors output for unset');
238 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
239 is($err, '', 'no errors in env dump');
240 @env = split(/\0/, $out);
241 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
243 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
244 is($out.$err, '', 'no output');
245 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
246 is($out, '', 'env cleared');
248 ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
249 is($out, '', 'no output from daemon-kill');
250 is($err, '', 'no error from daemon-kill');
252 kill(0, $pid) or last;
255 ok(!-S $sock, 'sock gone');
256 ok(!kill(0, $pid), 'pid gone after stop');
258 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
259 chomp(my $new_pid = $out);
260 ok(kill(0, $new_pid), 'new pid is running');
261 ok(-S $sock, 'sock exists again');
264 for my $sig (qw(-0 -CHLD)) {
265 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
268 is($out.$err, '', 'no output on innocuous signals');
269 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
271 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
273 if ('socket inaccessible') {
274 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
276 ok(run_script([qw(lei help)], undef, $opt),
277 'connect fail, one-shot fallback works');
278 like($err, qr/\bconnect\(/, 'connect error noted');
279 like($out, qr/^usage: /, 'help output works');
280 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
282 if ('oneshot on cwd gone') {
283 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
284 my $d = "$home/to-be-removed";
285 my $lei_path = 'lei';
286 # we chdir, so we need an abs_path fur run_script
287 if (($ENV{TEST_RUN_MODE}//2) != 2) {
288 $lei_path = PublicInbox::TestCommon::key2script('lei');
289 $lei_path = Cwd::abs_path($lei_path);
291 mkdir $d or BAIL_OUT "mkdir($d) $!";
292 chdir $d or BAIL_OUT "chdir($d) $!";
295 ok(run_script([$lei_path, 'help'], undef, $opt),
296 'cwd fail, one-shot fallback works');
300 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
301 like($err, qr/cwd\(/, 'cwd error noted');
302 like($out, qr/^usage: /, 'help output still works');
305 unlink $sock or BAIL_OUT "unlink($sock) $!";
307 kill('CHLD', $new_pid) or last;
310 ok(!kill(0, $new_pid), 'daemon exits after unlink');
311 # success over socket, can't test without