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: $!";
26 my $test_lei_common = sub {
27 ok(!$lei->([], undef, $opt), 'no args fails');
28 is($? >> 8, 1, '$? is 1');
29 is($out, '', 'nothing in stdout');
30 like($err, qr/^usage:/sm, 'usage in stderr');
32 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
34 ok($lei->($arg, undef, $opt), "lei @$arg");
35 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
36 is($err, '', "nothing in stderr (@$arg)");
39 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
41 ok(!$lei->($arg, undef, $opt), "lei @$arg");
42 is($? >> 8, 1, '$? set correctly');
43 isnt($err, '', 'something in stderr');
44 is($out, '', 'nothing in stdout');
49 my $ok_err_info = sub {
51 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
52 diag "$msg: err=$err";
55 my $home_trash = [ "$home/.local", "$home/.config" ];
57 ok($lei->(['init'], undef, $opt), 'init w/o args');
58 $ok_err_info->('after init w/o args');
59 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
60 $ok_err_info->('after idempotent init w/o args');
62 ok(!$lei->(['init', "$home/x"], undef, $opt),
64 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
65 ok(!-e "$home/x", 'nothing created on conflict');
69 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
70 $ok_err_info->('init w/ arg');
71 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
72 $ok_err_info->('init idempotent w/ arg');
73 ok(-d "$home/x", 'created dir');
74 rmtree([ "$home/x", @$home_trash ]);
77 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
78 'too many args fails');
79 like($err, qr/too many/, 'noted excessive');
80 ok(!-e "$home/x", 'x not created on excessive');
81 for my $d (@$home_trash) {
82 my $base = (split(m!/!, $d))[-1];
83 ok(!-d $d, "$base not created");
85 is($out, '', 'nothing in stdout');
88 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
90 last SKIP if $test_lei_oneshot;
91 require_mods('IO::FDPass', 16);
92 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
94 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
95 is($err, '', 'no error from daemon-pid');
96 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
97 chomp(my $pid = $out);
98 ok(kill(0, $pid), 'pid is valid');
99 ok(-S $sock, 'sock created');
101 $test_lei_common->();
104 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
105 chomp(my $pid_again = $out);
106 is($pid, $pid_again, 'daemon-pid idempotent');
109 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
110 is($err, '', 'no errors in env dump');
111 my @env = split(/\0/, $out);
112 is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
113 is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
114 is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
117 ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
118 is($out.$err, '', 'no output for unset');
119 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
120 is($err, '', 'no errors in env dump');
121 @env = split(/\0/, $out);
122 is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
125 ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
126 undef, $opt), 'unset multiple');
127 is($out.$err, '', 'no errors output for unset');
128 ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
129 is($err, '', 'no errors in env dump');
130 @env = split(/\0/, $out);
131 is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
133 ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
134 is($out.$err, '', 'no output');
135 ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
136 is($out, '', 'env cleared');
138 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
139 is($out, '', 'no output from daemon-stop');
140 is($err, '', 'no error from daemon-stop');
142 kill(0, $pid) or last;
145 ok(!-S $sock, 'sock gone');
146 ok(!kill(0, $pid), 'pid gone after stop');
148 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
149 chomp(my $new_pid = $out);
150 ok(kill(0, $new_pid), 'new pid is running');
151 ok(-S $sock, 'sock exists again');
152 unlink $sock or BAIL_OUT "unlink $!";
154 kill('CHLD', $new_pid) or last;
157 ok(!kill(0, $new_pid), 'daemon exits after unlink');
158 # success over socket, can't test without
159 $test_lei_common = undef;
162 require_ok 'PublicInbox::LeiDaemon';
163 $LEI = 'lei-oneshot' if $test_lei_oneshot;
164 $test_lei_common->() if $test_lei_common;