]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei_store: alternative unconfigured "git var" workaround
[public-inbox.git] / t / lei.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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_git 2.6;
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
13 my $lei = sub {
14         my ($cmd, $env, $xopt) = @_;
15         $out = $err = '';
16         if (!ref($cmd)) {
17                 ($env, $xopt) = grep { (!defined) || ref } @_;
18                 $cmd = [ grep { defined && !ref } @_ ];
19         }
20         run_script(['lei', @$cmd], $env, $xopt // $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{GIT_COMMITTER_EMAIL} = 'lei@example.com';
27 local $ENV{GIT_COMMITTER_NAME} = 'lei user';
28 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
29 local $ENV{HOME} = $home;
30 local $ENV{FOO} = 'BAR';
31 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
32 my $home_trash = [ "$home/.local", "$home/.config" ];
33 my $cleanup = sub { rmtree([@$home_trash, @_]) };
34 my $config_file = "$home/.config/lei/config";
35 my $store_dir = "$home/.local/share/lei";
36
37 my $test_help = sub {
38         ok(!$lei->([], undef, $opt), 'no args fails');
39         is($? >> 8, 1, '$? is 1');
40         is($out, '', 'nothing in stdout');
41         like($err, qr/^usage:/sm, 'usage in stderr');
42
43         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
44                 $out = $err = '';
45                 ok($lei->($arg, undef, $opt), "lei @$arg");
46                 like($out, qr/^usage:/sm, "usage in stdout (@$arg)");
47                 is($err, '', "nothing in stderr (@$arg)");
48         }
49
50         for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
51                 $out = $err = '';
52                 ok(!$lei->($arg, undef, $opt), "lei @$arg");
53                 is($? >> 8, 1, '$? set correctly');
54                 isnt($err, '', 'something in stderr');
55                 is($out, '', 'nothing in stdout');
56         }
57         ok($lei->(qw(init -h), undef, $opt), 'init -h');
58         like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
59                 'actual path shown in init -h');
60         ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
61                 'init with XDG_DATA_HOME');
62         like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
63         is($err, '', 'no errors from init -h');
64
65         ok($lei->(qw(config -h), undef, $opt), 'config-h');
66         like($out, qr! \Q$home\E/\.config/lei/config\b!,
67                 'actual path shown in config -h');
68         ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
69                 'config with XDG_CONFIG_HOME');
70         like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
71         is($err, '', 'no errors from config -h');
72 };
73
74 my $ok_err_info = sub {
75         my ($msg) = @_;
76         is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
77                 diag "$msg: err=$err";
78         $err = '';
79 };
80
81 my $test_init = sub {
82         $cleanup->();
83         ok($lei->(['init'], undef, $opt), 'init w/o args');
84         $ok_err_info->('after init w/o args');
85         ok($lei->(['init'], undef, $opt), 'idempotent init w/o args');
86         $ok_err_info->('after idempotent init w/o args');
87
88         ok(!$lei->(['init', "$home/x"], undef, $opt),
89                 'init conflict');
90         is(grep(/^E:/, split(/^/, $err)), 1, 'got error on conflict');
91         ok(!-e "$home/x", 'nothing created on conflict');
92         $cleanup->();
93
94         ok($lei->(['init', "$home/x"], undef, $opt), 'init conflict resolved');
95         $ok_err_info->('init w/ arg');
96         ok($lei->(['init', "$home/x"], undef, $opt), 'init idempotent w/ path');
97         $ok_err_info->('init idempotent w/ arg');
98         ok(-d "$home/x", 'created dir');
99         $cleanup->("$home/x");
100
101         ok(!$lei->(['init', "$home/x", "$home/2" ], undef, $opt),
102                 'too many args fails');
103         like($err, qr/too many/, 'noted excessive');
104         ok(!-e "$home/x", 'x not created on excessive');
105         for my $d (@$home_trash) {
106                 my $base = (split(m!/!, $d))[-1];
107                 ok(!-d $d, "$base not created");
108         }
109         is($out, '', 'nothing in stdout on init failure');
110 };
111
112 my $test_config = sub {
113         $cleanup->();
114         ok($lei->([qw(config a.b c)], undef, $opt), 'config set var');
115         is($out.$err, '', 'no output on var set');
116         ok($lei->([qw(config -l)], undef, $opt), 'config -l');
117         is($err, '', 'no errors on listing');
118         is($out, "a.b=c\n", 'got expected output');
119         ok(!$lei->([qw(config -f), "$home/.config/f", qw(x.y z)], undef, $opt),
120                         'config set var with -f fails');
121         like($err, qr/not supported/, 'not supported noted');
122         ok(!-f "$home/config/f", 'no file created');
123 };
124
125 my $setup_publicinboxes = sub {
126         state $done = '';
127         return if $done eq $home;
128         use PublicInbox::InboxWritable;
129         for my $V (1, 2) {
130                 run_script([qw(-init -Lmedium), "-V$V", "t$V",
131                                 '--newsgroup', "t.$V",
132                                 "$home/t$V", "http://example.com/t$V",
133                                 "t$V\@example.com" ]) or BAIL_OUT "init v$V";
134         }
135         my $cfg = PublicInbox::Config->new;
136         my $seen = 0;
137         $cfg->each_inbox(sub {
138                 my ($ibx) = @_;
139                 my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
140                 my $V = $ibx->version;
141                 my @eml = glob('t/*.eml');
142                 push(@eml, 't/data/0001.patch') if $V == 2;
143                 for (@eml) {
144                         next if $_ eq 't/psgi_v2-old.eml'; # dup mid
145                         $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
146                         $seen++;
147                 }
148                 $im->done;
149                 if ($V == 1) {
150                         run_script(['-index', $ibx->{inboxdir}]) or
151                                 BAIL_OUT 'index v1';
152                 }
153         });
154         $done = $home;
155         $seen || BAIL_OUT 'no imports';
156 };
157
158 my $test_external = sub {
159         $setup_publicinboxes->();
160         $cleanup->();
161         $lei->('ls-external');
162         is($out.$err, '', 'ls-external no output, yet');
163         ok(!-e $config_file && !-e $store_dir,
164                 'nothing created by ls-external');
165
166         my $cfg = PublicInbox::Config->new;
167         $cfg->each_inbox(sub {
168                 my ($ibx) = @_;
169                 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
170                         'added external');
171                 is($out.$err, '', 'no output');
172         });
173         ok(-s $config_file && -e $store_dir,
174                 'add-external created config + store');
175         my $lcfg = PublicInbox::Config->new($config_file);
176         $cfg->each_inbox(sub {
177                 my ($ibx) = @_;
178                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
179                         "configured boost on $ibx->{name}");
180         });
181         $lei->('ls-external');
182         like($out, qr/boost=0\n/s, 'ls-external has output');
183 };
184
185 my $test_lei_common = sub {
186         $test_help->();
187         $test_config->();
188         $test_init->();
189         $test_external->();
190 };
191
192 if ($ENV{TEST_LEI_ONESHOT}) {
193         require_ok 'PublicInbox::LEI';
194         # force sun_path[108] overflow, "IO::FDPass" avoids warning
195         local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
196         $test_lei_common->();
197 }
198
199 SKIP: { # real socket
200         require_mods(qw(IO::FDPass Cwd), 46);
201         local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
202         my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
203
204         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
205         is($err, '', 'no error from daemon-pid');
206         like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
207         chomp(my $pid = $out);
208         ok(kill(0, $pid), 'pid is valid');
209         ok(-S $sock, 'sock created');
210
211         $test_lei_common->();
212
213         $out = '';
214         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
215         chomp(my $pid_again = $out);
216         is($pid, $pid_again, 'daemon-pid idempotent');
217
218         $out = '';
219         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show env');
220         is($err, '', 'no errors in env dump');
221         my @env = split(/\0/, $out);
222         is(scalar grep(/\AHOME=\Q$home\E\z/, @env), 1, 'env has HOME');
223         is(scalar grep(/\AFOO=BAR\z/, @env), 1, 'env has FOO=BAR');
224         is(scalar grep(/\AXDG_RUNTIME_DIR=/, @env), 1, 'has XDG_RUNTIME_DIR');
225
226         $out = '';
227         ok(run_script([qw(lei daemon-env -u FOO)], undef, $opt), 'unset');
228         is($out.$err, '', 'no output for unset');
229         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
230         is($err, '', 'no errors in env dump');
231         @env = split(/\0/, $out);
232         is(scalar grep(/\AFOO=BAR\z/, @env), 0, 'env unset FOO');
233
234         $out = '';
235         ok(run_script([qw(lei daemon-env -u FOO -u HOME -u XDG_RUNTIME_DIR)],
236                         undef, $opt), 'unset multiple');
237         is($out.$err, '', 'no errors output for unset');
238         ok(run_script([qw(lei daemon-env -0)], undef, $opt), 'show again');
239         is($err, '', 'no errors in env dump');
240         @env = split(/\0/, $out);
241         is(scalar grep(/\A(?:HOME|XDG_RUNTIME_DIR)=\z/, @env), 0, 'env unset@');
242         $out = '';
243         ok(run_script([qw(lei daemon-env -)], undef, $opt), 'clear env');
244         is($out.$err, '', 'no output');
245         ok(run_script([qw(lei daemon-env)], undef, $opt), 'env is empty');
246         is($out, '', 'env cleared');
247
248         ok(run_script([qw(lei daemon-kill)], undef, $opt), 'daemon-kill');
249         is($out, '', 'no output from daemon-kill');
250         is($err, '', 'no error from daemon-kill');
251         for (0..100) {
252                 kill(0, $pid) or last;
253                 tick();
254         }
255         ok(!-S $sock, 'sock gone');
256         ok(!kill(0, $pid), 'pid gone after stop');
257
258         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
259         chomp(my $new_pid = $out);
260         ok(kill(0, $new_pid), 'new pid is running');
261         ok(-S $sock, 'sock exists again');
262
263         $out = $err = '';
264         for my $sig (qw(-0 -CHLD)) {
265                 ok(run_script([qw(lei daemon-kill), $sig ], undef, $opt),
266                                         "handles $sig");
267         }
268         is($out.$err, '', 'no output on innocuous signals');
269         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
270         chomp $out;
271         is($out, $new_pid, 'PID unchanged after -0/-CHLD');
272
273         if ('socket inaccessible') {
274                 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
275                 $out = $err = '';
276                 ok(run_script([qw(lei help)], undef, $opt),
277                         'connect fail, one-shot fallback works');
278                 like($err, qr/\bconnect\(/, 'connect error noted');
279                 like($out, qr/^usage: /, 'help output works');
280                 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
281         }
282         if ('oneshot on cwd gone') {
283                 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
284                 my $d = "$home/to-be-removed";
285                 my $lei_path = 'lei';
286                 # we chdir, so we need an abs_path fur run_script
287                 if (($ENV{TEST_RUN_MODE}//2) != 2) {
288                         $lei_path = PublicInbox::TestCommon::key2script('lei');
289                         $lei_path = Cwd::abs_path($lei_path);
290                 }
291                 mkdir $d or BAIL_OUT "mkdir($d) $!";
292                 chdir $d or BAIL_OUT "chdir($d) $!";
293                 if (rmdir($d)) {
294                         $out = $err = '';
295                         ok(run_script([$lei_path, 'help'], undef, $opt),
296                                 'cwd fail, one-shot fallback works');
297                 } else {
298                         $err = "rmdir=$!";
299                 }
300                 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
301                 like($err, qr/cwd\(/, 'cwd error noted');
302                 like($out, qr/^usage: /, 'help output still works');
303         }
304
305         unlink $sock or BAIL_OUT "unlink($sock) $!";
306         for (0..100) {
307                 kill('CHLD', $new_pid) or last;
308                 tick();
309         }
310         ok(!kill(0, $new_pid), 'daemon exits after unlink');
311         # success over socket, can't test without
312 };
313
314 done_testing;