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