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));
11 my ($home, $for_destroy) = tmpdir();
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 delete local $ENV{XDG_DATA_HOME};
14 delete local $ENV{XDG_CONFIG_HOME};
15 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
16 local $ENV{HOME} = $home;
17 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
19 my $test_lei_common = sub {
20 ok(!run_script([qw(lei)], undef, $opt), 'no args fails');
21 is($? >> 8, 1, '$? is 1');
22 is($out, '', 'nothing in stdout');
23 like($err, qr/^usage:/sm, 'usage in stderr');
25 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
27 ok(run_script(['lei', @$arg], undef, $opt), "lei @$arg");
28 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
29 is($err, '', "nothing in stderr (@$arg)");
32 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
34 ok(!run_script(['lei', @$arg], undef, $opt), "lei @$arg");
35 is($? >> 8, 1, '$? set correctly');
36 isnt($err, '', 'something in stderr');
37 is($out, '', 'nothing in stdout');
42 my $ok_err_info = sub {
44 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
45 diag "$msg: err=$err";
48 my $home_trash = [ "$home/.local", "$home/.config" ];
50 ok(run_script([qw(lei init)], undef, $opt), 'init w/o args');
51 $ok_err_info->('after init w/o args');
52 ok(run_script([qw(lei init)], undef, $opt), 'idempotent init w/o args');
53 $ok_err_info->('after idempotent init w/o args');
55 ok(!run_script([qw(lei init), "$home/x"], undef, $opt),
57 is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
58 ok(!-e "$home/x", 'nothing created on conflict');
62 ok(run_script([qw(lei init), "$home/x"], undef, $opt),
63 'init conflict resolved');
64 $ok_err_info->('init w/ arg');
65 ok(run_script([qw(lei init), "$home/x"], undef, $opt),
66 'init idempotent with path');
67 $ok_err_info->('init idempotent w/ arg');
68 ok(-d "$home/x", 'created dir');
69 rmtree([ "$home/x", @$home_trash ]);
72 ok(!run_script([qw(lei init), "$home/x", "$home/2" ], undef, $opt),
73 'too many args fails');
74 like($err, qr/too many/, 'noted excessive');
75 ok(!-e "$home/x", 'x not created on excessive');
76 for my $d (@$home_trash) {
77 my $base = (split(m!/!, $d))[-1];
78 ok(!-d $d, "$base not created");
80 is($out, '', 'nothing in stdout');
84 require_mods('IO::FDPass', 16);
85 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
87 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
88 is($err, '', 'no error from daemon-pid');
89 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
90 chomp(my $pid = $out);
91 ok(kill(0, $pid), 'pid is valid');
92 ok(-S $sock, 'sock created');
97 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
98 chomp(my $pid_again = $out);
99 is($pid, $pid_again, 'daemon-pid idempotent');
101 ok(run_script([qw(lei daemon-stop)], undef, $opt), 'daemon-stop');
102 is($out, '', 'no output from daemon-stop');
103 is($err, '', 'no error from daemon-stop');
105 kill(0, $pid) or last;
108 ok(!-S $sock, 'sock gone');
109 ok(!kill(0, $pid), 'pid gone after stop');
111 ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
112 chomp(my $new_pid = $out);
113 ok(kill(0, $new_pid), 'new pid is running');
114 ok(-S $sock, 'sock exists again');
115 unlink $sock or BAIL_OUT "unlink $!";
117 kill('CHLD', $new_pid) or last;
120 ok(!kill(0, $new_pid), 'daemon exits after unlink');
121 $test_lei_common = undef; # success over socket, can't test without
124 require_ok 'PublicInbox::LeiDaemon';
125 $test_lei_common->() if $test_lei_common;