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; use v5.10.1; use PublicInbox::TestCommon;
5 use File::Path qw(rmtree);
6 use PublicInbox::Spawn qw(which);
8 # this only tests the basic help/config/init/completion bits of lei;
9 # actual functionality is tested in other t/lei-*.t tests
10 my $curl = which('curl');
13 my $cleanup = sub { rmtree([@$home_trash, @_]) };
16 ok(!lei([]), 'no args fails');
17 is($? >> 8, 1, '$? is 1');
18 is($lei_out, '', 'nothing in stdout');
19 like($lei_err, qr/^usage:/sm, 'usage in stderr');
21 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
23 like($lei_out, qr/^usage:/sm, "usage in stdout (@$arg)");
24 is($lei_err, '', "nothing in stderr (@$arg)");
27 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
28 ok(!lei($arg), "lei @$arg");
29 is($? >> 8, 1, '$? set correctly');
30 isnt($lei_err, '', 'something in stderr');
31 is($lei_out, '', 'nothing in stdout');
34 like($lei_out, qr! \Q$home\E/\.local/share/lei/store\b!,
35 'actual path shown in init -h');
36 lei_ok(qw(init -h), { XDG_DATA_HOME => '/XDH' },
37 \'init with XDG_DATA_HOME');
38 like($lei_out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
39 is($lei_err, '', 'no errors from init -h');
41 lei_ok(qw(config -h));
42 like($lei_out, qr! \Q$home\E/\.config/lei/config\b!,
43 'actual path shown in config -h');
44 lei_ok(qw(config -h), { XDG_CONFIG_HOME => '/XDC' },
45 \'config with XDG_CONFIG_HOME');
46 like($lei_out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
47 is($lei_err, '', 'no errors from config -h');
50 my $ok_err_info = sub {
52 is(grep(!/^#/, split(/^/, $lei_err)), 0, $msg) or
53 diag "$msg: err=$lei_err";
58 lei_ok('init', \'init w/o args');
59 $ok_err_info->('after init w/o args');
60 lei_ok('init', \'idempotent init w/o args');
61 $ok_err_info->('after idempotent init w/o args');
63 ok(!lei('init', "$home/x"), 'init conflict');
64 is(grep(/^E:/, split(/^/, $lei_err)), 1, 'got error on conflict');
65 ok(!-e "$home/x", 'nothing created on conflict');
68 lei_ok('init', "$home/x", \'init conflict resolved');
69 $ok_err_info->('init w/ arg');
70 lei_ok('init', "$home/x", \'init idempotent w/ path');
71 $ok_err_info->('init idempotent w/ arg');
72 ok(-d "$home/x", 'created dir');
73 $cleanup->("$home/x");
75 ok(!lei('init', "$home/x", "$home/2"), 'too many args fails');
76 like($lei_err, qr/too many/, 'noted excessive');
77 ok(!-e "$home/x", 'x not created on excessive');
78 for my $d (@$home_trash) {
79 my $base = (split(m!/!, $d))[-1];
80 ok(!-d $d, "$base not created");
82 is($lei_out, '', 'nothing in stdout on init failure');
85 my $test_config = sub {
87 lei_ok(qw(config a.b c), \'config set var');
88 is($lei_out.$lei_err, '', 'no output on var set');
89 lei_ok(qw(config -l), \'config -l');
90 is($lei_err, '', 'no errors on listing');
91 is($lei_out, "a.b=c\n", 'got expected output');
92 ok(!lei(qw(config -f), "$home/.config/f", qw(x.y z)),
93 'config set var with -f fails');
94 like($lei_err, qr/not supported/, 'not supported noted');
95 ok(!-f "$home/config/f", 'no file created');
98 my $test_completion = sub {
99 lei_ok(qw(_complete lei), \'no errors on complete');
100 my %out = map { $_ => 1 } split(/\s+/s, $lei_out);
101 ok($out{'q'}, "`lei q' offered as completion");
102 ok($out{'add-external'}, "`lei add-external' offered as completion");
104 lei_ok(qw(_complete lei q), \'complete q (no args)');
105 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
106 for my $sw (qw(-f --format -o --output --mfolder --augment -a
107 --mua --no-local --local --verbose -v
108 --save-as --no-remote --remote --torsocks
110 ok($out{$sw}, "$sw offered as `lei q' completion");
113 lei_ok(qw(_complete lei q --form), \'complete q --format');
114 is($lei_out, "--format\n", 'complete lei q --format');
115 for my $sw (qw(-f --format)) {
116 lei_ok(qw(_complete lei q), $sw);
117 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
118 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
119 concatjson maildir)) {
120 ok($out{$f}, "got $sw $f as output format");
123 lei_ok(qw(_complete lei import));
124 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
125 for my $sw (qw(--flags --no-flags --no-kw --kw --no-keywords
127 ok($out{$sw}, "$sw offered as `lei import' completion");
131 my $test_fail = sub {
132 lei('q', 'whatever', '-C', '/dev/null');
133 is($? >> 8, 1, 'chdir at end fails to /dev/null');
134 lei('-C', '/dev/null', 'q', 'whatever');
135 is($? >> 8, 1, 'chdir at beginning fails to /dev/null');
137 skip 'no curl', 3 unless which('curl');
138 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
139 is($? >> 8, 3, 'got curl exit for bogus URL');
140 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
141 is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
143 is($lei_out, '', 'no output');
149 $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
153 $test_completion->();