]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: help: show actual paths being operated on
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
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 $LEI = 'lei';
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 my $lei = sub {
14         my ($cmd, $env, $opt) = @_;
15         $out = $err = '';
16         if (!ref($cmd)) {
17                 ($env, $opt) = grep { (!defined) || ref } @_;
18                 $cmd = [ grep { defined } @_ ];
19         }
20         run_script([$LEI, @$cmd], $env, $opt);
21 };
22
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, @_]) };
32
33 my $test_help = sub {
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');
38
39         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
40                 $out = $err = '';
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)");
44         }
45
46         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
47                 $out = $err = '';
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');
52         }
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');
60
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');
68 };
69
70 my $ok_err_info = sub {
71         my ($msg) = @_;
72         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
73                 diag "$msg: err=$err";
74         $err = '';
75 };
76
77 my $test_init = sub {
78         $cleanup->();
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');
83
84         ok(!$lei->(['init', "$home/x"], undef, $opt),
85                 'init conflict');
86         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
87         ok(!-e "$home/x", 'nothing created on conflict');
88         $cleanup->();
89
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");
96
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");
104         }
105         is($out, '', 'nothing in stdout on init failure');
106 };
107
108 my $test_config = sub {
109         $cleanup->();
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');
119 };
120
121 my $test_lei_common = sub {
122         $test_help->();
123         $test_config->();
124         $test_init->();
125 };
126
127 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
128 SKIP: {
129         last SKIP if $test_lei_oneshot;
130         require_mods('IO::FDPass', 16);
131         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
132
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');
139
140         $test_lei_common->();
141
142         $out = '';
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');
146
147         $out = '';
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');
154
155         $out = '';
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');
162
163         $out = '';
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@');
171         $out = '';
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');
176
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');
180         for (0..100) {
181                 kill(0, $pid) or last;
182                 tick();
183         }
184         ok(!-S $sock, 'sock gone');
185         ok(!kill(0, $pid), 'pid gone after stop');
186
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');
191         unlink $sock or BAIL_OUT "unlink $!";
192         for (0..100) {
193                 kill('CHLD', $new_pid) or last;
194                 tick();
195         }
196         ok(!kill(0, $new_pid), 'daemon exits after unlink');
197         # success over socket, can't test without
198         $test_lei_common = undef;
199 };
200
201 require_ok 'PublicInbox::LEI';
202 $LEI = 'lei-oneshot' if $test_lei_oneshot;
203 $test_lei_common->() if $test_lei_common;
204
205 done_testing;