2 # Copyright (C) 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;
6 use File::Path qw(rmtree);
8 # this only tests the basic help/config/init/completion bits of lei;
9 # actual functionality is tested in other t/lei-*.t tests
12 my $cleanup = sub { rmtree([@$home_trash, @_]) };
15 ok(!lei([]), 'no args fails');
16 is($? >> 8, 1, '$? is 1');
17 is($lei_out, '', 'nothing in stdout');
18 like($lei_err, qr/^usage:/sm, 'usage in stderr');
20 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
22 like($lei_out, qr/^usage:/sm, "usage in stdout (@$arg)");
23 is($lei_err, '', "nothing in stderr (@$arg)");
26 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
27 ok(!lei($arg), "lei @$arg");
28 is($? >> 8, 1, '$? set correctly');
29 isnt($lei_err, '', 'something in stderr');
30 is($lei_out, '', 'nothing in stdout');
33 like($lei_out, qr! \Q$home\E/\.local/share/lei/store\b!,
34 'actual path shown in init -h');
35 lei_ok(qw(init -h), { XDG_DATA_HOME => '/XDH' },
36 \'init with XDG_DATA_HOME');
37 like($lei_out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
38 is($lei_err, '', 'no errors from init -h');
40 lei_ok(qw(config -h));
41 like($lei_out, qr! \Q$home\E/\.config/lei/config\b!,
42 'actual path shown in config -h');
43 my $exp_help = qr/\Q$lei_out\E/s;
44 ok(!lei('config'), 'config w/o args fails');
45 like($lei_err, $exp_help, 'config w/o args shows our help in stderr');
46 lei_ok(qw(config -h), { XDG_CONFIG_HOME => '/XDC' },
47 \'config with XDG_CONFIG_HOME');
48 like($lei_out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
49 is($lei_err, '', 'no errors from config -h');
51 lei_ok(qw(-c foo.bar config dash.c works));
52 lei_ok(qw(config dash.c));
53 is($lei_out, "works\n", 'config set w/ -c');
55 lei_ok(qw(-c foo.bar config --add dash.c add-works));
56 lei_ok(qw(config --get-all dash.c));
57 is($lei_out, "works\nadd-works\n", 'config --add w/ -c');
60 my $ok_err_info = sub {
62 is(grep(!/^#/, split(/^/, $lei_err)), 0, $msg) or
63 diag "$msg: err=$lei_err";
68 lei_ok('init', \'init w/o args');
69 $ok_err_info->('after init w/o args');
70 lei_ok('init', \'idempotent init w/o args');
71 $ok_err_info->('after idempotent init w/o args');
73 ok(!lei('init', "$home/x"), 'init conflict');
74 is(grep(/^E:/, split(/^/, $lei_err)), 1, 'got error on conflict');
75 ok(!-e "$home/x", 'nothing created on conflict');
78 lei_ok('init', "$home/x", \'init conflict resolved');
79 $ok_err_info->('init w/ arg');
80 lei_ok('init', "$home/x", \'init idempotent w/ path');
81 $ok_err_info->('init idempotent w/ arg');
82 ok(-d "$home/x", 'created dir');
83 $cleanup->("$home/x");
85 ok(!lei('init', "$home/x", "$home/2"), 'too many args fails');
86 like($lei_err, qr/too many/, 'noted excessive');
87 ok(!-e "$home/x", 'x not created on excessive');
88 for my $d (@$home_trash) {
89 my $base = (split(m!/!, $d))[-1];
90 ok(!-d $d, "$base not created");
92 is($lei_out, '', 'nothing in stdout on init failure');
95 my $test_config = sub {
97 lei_ok(qw(config a.b c), \'config set var');
98 is($lei_out.$lei_err, '', 'no output on var set');
99 lei_ok(qw(config -l), \'config -l');
100 is($lei_err, '', 'no errors on listing');
101 is($lei_out, "a.b=c\n", 'got expected output');
102 ok(!lei(qw(config -f), "$home/.config/f", qw(x.y z)),
103 'config set var with -f fails');
104 like($lei_err, qr/not supported/, 'not supported noted');
105 ok(!-f "$home/config/f", 'no file created');
107 lei_ok(qw(-c imap.debug config --bool imap.debug));
108 is($lei_out, "true\n", "-c sets w/o value");
109 lei_ok(qw(-c imap.debug=1 config --bool imap.debug));
110 is($lei_out, "true\n", "-c coerces value");
111 lei_ok(qw(-c imap.debug=tr00 config imap.debug));
112 is($lei_out, "tr00\n", "-c string value passed as-is");
113 lei_ok(qw(-c imap.debug=a -c imap.debug=b config --get-all imap.debug));
114 is($lei_out, "a\nb\n", '-c and --get-all work together');
115 my $env = { VISUAL => 'cat', EDITOR => 'cat' };
116 lei_ok([qw(config -e)], $env);
117 is($lei_out, "[a]\n\tb = c\n", '--edit works');
118 ok(!lei([qw(-c a.b=c config -e)], $env), '-c conflicts with -e');
119 like($lei_err, qr/not allowed/, 'error message shown');
122 my $test_completion = sub {
123 lei_ok(qw(_complete lei), \'no errors on complete');
124 my %out = map { $_ => 1 } split(/\s+/s, $lei_out);
125 ok($out{'q'}, "`lei q' offered as completion");
126 ok($out{'add-external'}, "`lei add-external' offered as completion");
128 lei_ok(qw(_complete lei q), \'complete q (no args)');
129 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
130 for my $sw (qw(-f --format -o --output --mfolder --augment -a
131 --mua --no-local --local --verbose -v
132 --save --no-save --no-remote --remote --torsocks
134 ok($out{$sw}, "$sw offered as `lei q' completion");
137 lei_ok(qw(_complete lei q --form), \'complete q --format');
138 is($lei_out, "--format\n", 'complete lei q --format');
139 for my $sw (qw(-f --format)) {
140 lei_ok(qw(_complete lei q), $sw);
141 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
142 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
143 concatjson maildir)) {
144 ok($out{$f}, "got $sw $f as output format");
147 lei_ok(qw(_complete lei import));
148 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
149 for my $sw (qw(--no-kw --kw)) {
150 ok($out{$sw}, "$sw offered as `lei import' completion");
154 my $test_fail = sub {
155 lei('q', 'whatever', '-C', '/dev/null');
156 is($? >> 8, 1, 'chdir at end fails to /dev/null');
157 lei('-C', '/dev/null', 'q', 'whatever');
158 is($? >> 8, 1, 'chdir at beginning fails to /dev/null');
160 lei_ok('q', "foo\n");
161 like($lei_err, qr/trailing `\\n' removed/s, "noted `\\n' removal");
163 lei(qw(q from:infinity..));
164 is($? >> 8, 22, 'combined query fails on invalid range op');
165 lei(qw(q -t from:infinity..));
166 is($? >> 8, 22, 'single query fails on invalid range op');
168 for my $lk (qw(ei inbox)) {
169 my $d = "$home/newline\n$lk";
170 my $all = $lk eq 'ei' ? 'ALL' : 'all';
171 { # quiet newline warning on older Perls
172 local $^W = undef if $^V lt v5.22.0;
173 File::Path::mkpath("$d/$all.git/objects");
175 open my $fh, '>', "$d/$lk.lock" or BAIL_OUT "open $d/$lk.lock";
176 for my $fl (qw(-I --only)) {
177 ok(!lei('q', $fl, $d, 'whatever'),
178 "newline $lk.lock fails with q $fl");
179 like($lei_err, qr/`\\n' not allowed/,
180 "error noted with q $fl");
183 lei_ok('sucks', \'yes, but hopefully less every day');
184 like($lei_out, qr/loaded features/, 'loaded features shown');
186 lei_ok([qw(q --stdin -f text)], undef, { 0 => \'', %$lei_opt });
187 is($lei_err, '', 'no errors on empty stdin');
188 is($lei_out, '', 'no output on empty query');
191 skip 'no curl', 3 unless require_cmd('curl', 1);
192 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
193 is($? >> 8, 3, 'got curl exit for bogus URL');
194 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
195 is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
197 is($lei_out, '', 'no output');
203 $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
207 $test_completion->();
211 test_lei({ mods => [] }, sub {
212 lei_ok('sucks', \'no optional modules required');