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);
10 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my ($cmd, $env, $opt) = @_;
17 ($env, $opt) = grep { (!defined) || ref } @_;
18 $cmd = [ grep { defined } @_ ];
20 run_script([$LEI, @$cmd], $env, $opt);
23 my ($home, $for_destroy) = tmpdir();
24 delete local $ENV{XDG_DATA_HOME};
25 delete local $ENV{XDG_CONFIG_HOME};
26 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
27 local $ENV{HOME} = $home;
28 local $ENV{FOO} = 'BAR';
29 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
30 my $home_trash = [ "$home/.local", "$home/.config" ];
31 my $cleanup = sub { rmtree([@$home_trash, @_]) };
34 ok(!$lei->([], undef, $opt), 'no args fails');
35 is($? >> 8, 1, '$? is 1');
36 is($out, '', 'nothing in stdout');
37 like($err, qr/^usage:/sm, 'usage in stderr');
39 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
41 ok($lei->($arg, undef, $opt), "lei @$arg");
42 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
43 is($err, '', "nothing in stderr (@$arg)");
46 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
48 ok(!$lei->($arg, undef, $opt), "lei @$arg");
49 is($? >> 8, 1, '$? set correctly');
50 isnt($err, '', 'something in stderr');
51 is($out, '', 'nothing in stdout');
53 ok($lei->(qw(init -h), undef, $opt), 'init -h');
54 like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
55 'actual path shown in init -h');
56 ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
57 'init with XDG_DATA_HOME');
58 like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
59 is($err, '', 'no errors from init -h');
61 ok($lei->(qw(config -h), undef, $opt), 'config-h');
62 like($out, qr! \Q$home\E/\.config/lei/config\b!,
63 'actual path shown in config -h');
64 ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
65 'config with XDG_CONFIG_HOME');
66 like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
67 is($err, '', 'no errors from config -h');
70 my $ok_err_info = sub {
72 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
73 diag "$msg: err=$err";
79 ok($lei->(['init'], undef, $opt), 'init w/o args');
80 $ok_err_info->('after init w/o args');
81 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
82 $ok_err_info->('after idempotent init w/o args');
84 ok(!$lei->(['init', "$home/x"], undef, $opt),
86 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
87 ok(!-e "$home/x", 'nothing created on conflict');
90 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
91 $ok_err_info->('init w/ arg');
92 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
93 $ok_err_info->('init idempotent w/ arg');
94 ok(-d "$home/x", 'created dir');
95 $cleanup->("$home/x");
97 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
98 'too many args fails');
99 like($err, qr/too many/, 'noted excessive');
100 ok(!-e "$home/x", 'x not created on excessive');
101 for my $d (@$home_trash) {
102 my $base = (split(m!/!, $d))[-1];
103 ok(!-d $d, "$base not created");
105 is($out, '', 'nothing in stdout on init failure');
108 my $test_config = sub {
110 ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
111 is($out.$err, '', 'no output on var set');
112 ok($lei->([qw(config -l)], undef, $opt), 'config -l');
113 is($err, '', 'no errors on listing');
114 is($out, "a.b=c\n", 'got expected output');
115 ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
116 'config set var with -f fails');
117 like($err, qr/not supported/, 'not supported noted');
118 ok(!-f "$home/config/f", 'no file created');
121 my $test_lei_common = sub {
127 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
129 last SKIP if $test_lei_oneshot;
130 require_mods(qw(IO::FDPass Cwd), 41);
131 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
133 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
134 is($err, '', 'no error from daemon-pid');
135 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
136 chomp(my $pid = $out);
137 ok(kill(0, $pid), 'pid is valid');
138 ok(-S $sock, 'sock created');
140 $test_lei_common->();
143 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
144 chomp(my $pid_again = $out);
145 is($pid, $pid_again, 'daemon-pid idempotent');
148 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
149 is($err, '', 'no errors in env dump');
150 my @env = split(/\0/, $out);
151 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
152 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
153 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
156 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
157 is($out.$err, '', 'no output for unset');
158 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
159 is($err, '', 'no errors in env dump');
160 @env = split(/\0/, $out);
161 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
164 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
165 undef, $opt), 'unset multiple');
166 is($out.$err, '', 'no errors output for unset');
167 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
168 is($err, '', 'no errors in env dump');
169 @env = split(/\0/, $out);
170 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
172 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
173 is($out.$err, '', 'no output');
174 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
175 is($out, '', 'env cleared');
177 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
178 is($out, '', 'no output from daemon-stop');
179 is($err, '', 'no error from daemon-stop');
181 kill(0, $pid) or last;
184 ok(!-S $sock, 'sock gone');
185 ok(!kill(0, $pid), 'pid gone after stop');
187 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
188 chomp(my $new_pid = $out);
189 ok(kill(0, $new_pid), 'new pid is running');
190 ok(-S $sock, 'sock exists again');
192 if ('socket inaccessible') {
193 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
195 ok(run_script([qw(lei help)], undef, $opt),
196 'connect fail, one-shot fallback works');
197 like($err, qr/\bconnect\(/, 'connect error noted');
198 like($out, qr/^usage: /, 'help output works');
199 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
201 if ('oneshot on cwd gone') {
202 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
203 my $d = "$home/to-be-removed";
204 mkdir $d or BAIL_OUT "mkdir($d) $!";
205 chdir $d or BAIL_OUT "chdir($d) $!";
208 ok(run_script([qw(lei help)], undef, $opt),
209 'cwd fail, one-shot fallback works');
213 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
214 like($err, qr/cwd\(/, 'cwd error noted');
215 like($out, qr/^usage: /, 'help output still works');
218 unlink $sock or BAIL_OUT "unlink($sock) $!";
220 kill('CHLD', $new_pid) or last;
223 ok(!kill(0, $new_pid), 'daemon exits after unlink');
224 # success over socket, can't test without
225 $test_lei_common = undef;
228 require_ok 'PublicInbox::LEI';
229 $LEI = 'lei-oneshot' if $test_lei_oneshot;
230 $test_lei_common->() if $test_lei_common;