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>
7 use PublicInbox::TestCommon;
8 use PublicInbox::Config;
9 use File::Path qw(rmtree);
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
14 my ($cmd, $env, $xopt) = @_;
17 ($env, $xopt) = grep { (!defined) || ref } @_;
18 $cmd = [ grep { defined && !ref } @_ ];
20 run_script(['lei', @$cmd], $env, $xopt // $opt);
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";
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');
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)");
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');
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');
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');
72 my $ok_err_info = sub {
74 is(grep(!/^I:/, split(/^/, $err)), 0, $msg) or
75 diag "$msg: err=$err";
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');
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');
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");
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");
104 is($out, '', 'nothing in stdout on init failure');
107 my $test_config = sub {
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');
120 my $setup_publicinboxes = sub {
122 return if $done eq $home;
123 use PublicInbox::InboxWritable;
125 run_script([qw(-init), "-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";
130 my $cfg = PublicInbox::Config->new;
132 $cfg->each_inbox(sub {
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;
139 next if $_ eq 't/psgi_v2-old.eml'; # dup mid
140 $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
145 run_script(['-index', $ibx->{inboxdir}]) or
150 $seen || BAIL_OUT 'no imports';
153 my $test_external = sub {
154 $setup_publicinboxes->();
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');
161 my $cfg = PublicInbox::Config->new;
162 $cfg->each_inbox(sub {
164 ok($lei->(qw(add-external -q), $ibx->{inboxdir}),
166 is($out.$err, '', 'no output');
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 {
173 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
174 "configured boost on $ibx->{name}");
176 $lei->('ls-external');
177 like($out, qr/boost=0\n/s, 'ls-external has output');
179 # note, on a Bourne shell users should be able to use either:
180 # s:"use boolean prefix"
181 # "s:use boolean prefix"
182 # or use single quotes, it should not matter. Users only need
183 # to know shell quoting rules, not Xapian quoting rules.
184 # No double-quoting should be imposed on users on the CLI
185 $lei->('q', 's:use boolean prefix');
186 like($out, qr/search: use boolean prefix/, 'phrase search got result');
189 my $test_lei_common = sub {
196 if ($ENV{TEST_LEI_ONESHOT}) {
197 require_ok 'PublicInbox::LEI';
198 # force sun_path[108] overflow, "IO::FDPass" avoids warning
199 local $ENV{XDG_RUNTIME_DIR} = "$home/IO::FDPass".('.sun_path' x 108);
200 $test_lei_common->();
203 SKIP: { # real socket
204 require_mods(qw(Cwd), my $nr = 105);
205 my $nfd = eval { require IO::FDPass; 1 } // do {
206 require PublicInbox::Spawn;
207 PublicInbox::Spawn->can('send_3fds') ? 3 : undef;
209 skip 'IO::FDPass missing or Inline::C not installed/configured', $nr;
211 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
212 my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/$nfd.sock";
214 ok($lei->('daemon-pid'), 'daemon-pid');
215 is($err, '', 'no error from daemon-pid');
216 like($out, qr/\A[0-9]+\n\z/s, 'pid returned') or BAIL_OUT;
217 chomp(my $pid = $out);
218 ok(kill(0, $pid), 'pid is valid');
219 ok(-S $sock, 'sock created');
221 $test_lei_common->();
223 ok($lei->('daemon-pid'), 'daemon-pid');
224 chomp(my $pid_again = $out);
225 is($pid, $pid_again, 'daemon-pid idempotent');
227 ok($lei->(qw(daemon-kill)), 'daemon-kill');
228 is($out, '', 'no output from daemon-kill');
229 is($err, '', 'no error from daemon-kill');
231 kill(0, $pid) or last;
234 ok(!-S $sock, 'sock gone');
235 ok(!kill(0, $pid), 'pid gone after stop');
237 ok($lei->(qw(daemon-pid)), 'daemon-pid');
238 chomp(my $new_pid = $out);
239 ok(kill(0, $new_pid), 'new pid is running');
240 ok(-S $sock, 'sock exists again');
242 for my $sig (qw(-0 -CHLD)) {
243 ok($lei->('daemon-kill', $sig), "handles $sig");
245 is($out.$err, '', 'no output on innocuous signals');
246 ok($lei->('daemon-pid'), 'daemon-pid');
248 is($out, $new_pid, 'PID unchanged after -0/-CHLD');
250 if ('socket inaccessible') {
251 chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
252 ok($lei->('help'), 'connect fail, one-shot fallback works');
253 like($err, qr/\bconnect\(/, 'connect error noted');
254 like($out, qr/^usage: /, 'help output works');
255 chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
257 if ('oneshot on cwd gone') {
258 my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
259 my $d = "$home/to-be-removed";
260 my $lei_path = 'lei';
261 # we chdir, so we need an abs_path fur run_script
262 if (($ENV{TEST_RUN_MODE}//2) != 2) {
263 $lei_path = PublicInbox::TestCommon::key2script('lei');
264 $lei_path = Cwd::abs_path($lei_path);
266 mkdir $d or BAIL_OUT "mkdir($d) $!";
267 chdir $d or BAIL_OUT "chdir($d) $!";
270 ok(run_script([$lei_path, 'help'], undef, $opt),
271 'cwd fail, one-shot fallback works');
275 chdir $cwd or BAIL_OUT "chdir($cwd) $!";
276 like($err, qr/cwd\(/, 'cwd error noted');
277 like($out, qr/^usage: /, 'help output still works');
280 unlink $sock or BAIL_OUT "unlink($sock) $!";
282 kill('CHLD', $new_pid) or last;
285 ok(!kill(0, $new_pid), 'daemon exits after unlink');
286 # success over socket, can't test without