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));
13 my ($cmd, $env, $opt) = @_;
14 run_script([$LEI, @$cmd], $env, $opt);
17 my ($home, $for_destroy) = tmpdir();
18 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
19 delete local $ENV{XDG_DATA_HOME};
20 delete local $ENV{XDG_CONFIG_HOME};
21 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
22 local $ENV{HOME} = $home;
23 local $ENV{FOO} = 'BAR';
24 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
25 my $home_trash = [ "$home/.local", "$home/.config" ];
27 rmtree([@$home_trash, @_]);
32 ok(!$lei->([], undef, $opt), 'no args fails');
33 is($? >> 8, 1, '$? is 1');
34 is($out, '', 'nothing in stdout');
35 like($err, qr/^usage:/sm, 'usage in stderr');
37 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
39 ok($lei->($arg, undef, $opt), "lei @$arg");
40 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
41 is($err, '', "nothing in stderr (@$arg)");
44 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
46 ok(!$lei->($arg, undef, $opt), "lei @$arg");
47 is($? >> 8, 1, '$? set correctly');
48 isnt($err, '', 'something in stderr');
49 is($out, '', 'nothing in stdout');
53 my $ok_err_info = sub {
55 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
56 diag "$msg: err=$err";
62 ok($lei->(['init'], undef, $opt), 'init w/o args');
63 $ok_err_info->('after init w/o args');
64 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
65 $ok_err_info->('after idempotent init w/o args');
67 ok(!$lei->(['init', "$home/x"], undef, $opt),
69 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
70 ok(!-e "$home/x", 'nothing created on conflict');
73 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
74 $ok_err_info->('init w/ arg');
75 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
76 $ok_err_info->('init idempotent w/ arg');
77 ok(-d "$home/x", 'created dir');
78 $cleanup->("$home/x");
80 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
81 'too many args fails');
82 like($err, qr/too many/, 'noted excessive');
83 ok(!-e "$home/x", 'x not created on excessive');
84 for my $d (@$home_trash) {
85 my $base = (split(m!/!, $d))[-1];
86 ok(!-d $d, "$base not created");
88 is($out, '', 'nothing in stdout on init failure');
91 my $test_config = sub {
93 ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
94 is($out.$err, '', 'no output on var set');
95 ok($lei->([qw(config -l)], undef, $opt), 'config -l');
96 is($err, '', 'no errors on listing');
97 is($out, "a.b=c\n", 'got expected output');
98 ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
99 'config set var with -f fails');
100 like($err, qr/not supported/, 'not supported noted');
101 ok(!-f "$home/config/f", 'no file created');
104 my $test_lei_common = sub {
110 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
112 last SKIP if $test_lei_oneshot;
113 require_mods('IO::FDPass', 16);
114 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
116 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
117 is($err, '', 'no error from daemon-pid');
118 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
119 chomp(my $pid = $out);
120 ok(kill(0, $pid), 'pid is valid');
121 ok(-S $sock, 'sock created');
123 $test_lei_common->();
126 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
127 chomp(my $pid_again = $out);
128 is($pid, $pid_again, 'daemon-pid idempotent');
131 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
132 is($err, '', 'no errors in env dump');
133 my @env = split(/\0/, $out);
134 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
135 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
136 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
139 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
140 is($out.$err, '', 'no output for unset');
141 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
142 is($err, '', 'no errors in env dump');
143 @env = split(/\0/, $out);
144 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
147 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
148 undef, $opt), 'unset multiple');
149 is($out.$err, '', 'no errors output for unset');
150 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
151 is($err, '', 'no errors in env dump');
152 @env = split(/\0/, $out);
153 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
155 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
156 is($out.$err, '', 'no output');
157 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
158 is($out, '', 'env cleared');
160 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
161 is($out, '', 'no output from daemon-stop');
162 is($err, '', 'no error from daemon-stop');
164 kill(0, $pid) or last;
167 ok(!-S $sock, 'sock gone');
168 ok(!kill(0, $pid), 'pid gone after stop');
170 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
171 chomp(my $new_pid = $out);
172 ok(kill(0, $new_pid), 'new pid is running');
173 ok(-S $sock, 'sock exists again');
174 unlink $sock or BAIL_OUT "unlink $!";
176 kill('CHLD', $new_pid) or last;
179 ok(!kill(0, $new_pid), 'daemon exits after unlink');
180 # success over socket, can't test without
181 $test_lei_common = undef;
184 require_ok 'PublicInbox::LEI';
185 $LEI = 'lei-oneshot' if $test_lei_oneshot;
186 $test_lei_common->() if $test_lei_common;