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 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
25 my $test_lei_common = sub {
26 ok(!$lei->([], undef, $opt), 'no args fails');
27 is($? >> 8, 1, '$? is 1');
28 is($out, '', 'nothing in stdout');
29 like($err, qr/^usage:/sm, 'usage in stderr');
31 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
33 ok($lei->($arg, undef, $opt), "lei @$arg");
34 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
35 is($err, '', "nothing in stderr (@$arg)");
38 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
40 ok(!$lei->($arg, undef, $opt), "lei @$arg");
41 is($? >> 8, 1, '$? set correctly');
42 isnt($err, '', 'something in stderr');
43 is($out, '', 'nothing in stdout');
48 my $ok_err_info = sub {
50 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
51 diag "$msg: err=$err";
54 my $home_trash = [ "$home/.local", "$home/.config" ];
56 ok($lei->(['init'], undef, $opt), 'init w/o args');
57 $ok_err_info->('after init w/o args');
58 ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
59 $ok_err_info->('after idempotent init w/o args');
61 ok(!$lei->(['init', "$home/x"], undef, $opt),
63 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
64 ok(!-e "$home/x", 'nothing created on conflict');
68 ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
69 $ok_err_info->('init w/ arg');
70 ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
71 $ok_err_info->('init idempotent w/ arg');
72 ok(-d "$home/x", 'created dir');
73 rmtree([ "$home/x", @$home_trash ]);
76 ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
77 'too many args fails');
78 like($err, qr/too many/, 'noted excessive');
79 ok(!-e "$home/x", 'x not created on excessive');
80 for my $d (@$home_trash) {
81 my $base = (split(m!/!, $d))[-1];
82 ok(!-d $d, "$base not created");
84 is($out, '', 'nothing in stdout');
87 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
89 last SKIP if $test_lei_oneshot;
90 require_mods('IO::FDPass', 16);
91 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
93 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
94 is($err, '', 'no error from daemon-pid');
95 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
96 chomp(my $pid = $out);
97 ok(kill(0, $pid), 'pid is valid');
98 ok(-S $sock, 'sock created');
100 $test_lei_common->();
103 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
104 chomp(my $pid_again = $out);
105 is($pid, $pid_again, 'daemon-pid idempotent');
107 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
108 is($out, '', 'no output from daemon-stop');
109 is($err, '', 'no error from daemon-stop');
111 kill(0, $pid) or last;
114 ok(!-S $sock, 'sock gone');
115 ok(!kill(0, $pid), 'pid gone after stop');
117 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
118 chomp(my $new_pid = $out);
119 ok(kill(0, $new_pid), 'new pid is running');
120 ok(-S $sock, 'sock exists again');
121 unlink $sock or BAIL_OUT "unlink $!";
123 kill('CHLD', $new_pid) or last;
126 ok(!kill(0, $new_pid), 'daemon exits after unlink');
127 # success over socket, can't test without
128 $test_lei_common = undef;
131 require_ok 'PublicInbox::LeiDaemon';
132 $LEI = 'lei-oneshot' if $test_lei_oneshot;
133 $test_lei_common->() if $test_lei_common;