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');
97 lei_ok(qw(-c imap.debug config --bool imap.debug));
98 is($lei_out, "true\n", "-c sets w/o value");
99 lei_ok(qw(-c imap.debug=1 config --bool imap.debug));
100 is($lei_out, "true\n", "-c coerces value");
101 lei_ok(qw(-c imap.debug=tr00 config imap.debug));
102 is($lei_out, "tr00\n", "-c string value passed as-is");
103 lei_ok(qw(-c imap.debug=a -c imap.debug=b config --get-all imap.debug));
104 is($lei_out, "a\nb\n", '-c and --get-all work together');
107 my $test_completion = sub {
108 lei_ok(qw(_complete lei), \'no errors on complete');
109 my %out = map { $_ => 1 } split(/\s+/s, $lei_out);
110 ok($out{'q'}, "`lei q' offered as completion");
111 ok($out{'add-external'}, "`lei add-external' offered as completion");
113 lei_ok(qw(_complete lei q), \'complete q (no args)');
114 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
115 for my $sw (qw(-f --format -o --output --mfolder --augment -a
116 --mua --no-local --local --verbose -v
117 --save --no-remote --remote --torsocks
119 ok($out{$sw}, "$sw offered as `lei q' completion");
122 lei_ok(qw(_complete lei q --form), \'complete q --format');
123 is($lei_out, "--format\n", 'complete lei q --format');
124 for my $sw (qw(-f --format)) {
125 lei_ok(qw(_complete lei q), $sw);
126 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
127 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
128 concatjson maildir)) {
129 ok($out{$f}, "got $sw $f as output format");
132 lei_ok(qw(_complete lei import));
133 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
134 for my $sw (qw(--no-kw --kw)) {
135 ok($out{$sw}, "$sw offered as `lei import' completion");
139 my $test_fail = sub {
140 lei('q', 'whatever', '-C', '/dev/null');
141 is($? >> 8, 1, 'chdir at end fails to /dev/null');
142 lei('-C', '/dev/null', 'q', 'whatever');
143 is($? >> 8, 1, 'chdir at beginning fails to /dev/null');
145 for my $lk (qw(ei inbox)) {
146 my $d = "$home/newline\n$lk";
148 open my $fh, '>', "$d/$lk.lock" or BAIL_OUT "open $d/$lk.lock";
149 for my $fl (qw(-I --only)) {
150 ok(!lei('q', $fl, $d, 'whatever'),
151 "newline $lk.lock fails with q $fl");
152 like($lei_err, qr/`\\n' not allowed/,
153 "error noted with q $fl");
156 lei_ok('sucks', \'yes, but hopefully less every day');
157 like($lei_out, qr/loaded features/, 'loaded features shown');
159 skip 'no curl', 3 unless which('curl');
160 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
161 is($? >> 8, 3, 'got curl exit for bogus URL');
162 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
163 is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
165 is($lei_out, '', 'no output');
171 $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
175 $test_completion->();