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);
7 # this only tests the basic help/config/init/completion bits of lei;
8 # actual functionality is tested in other t/lei-*.t tests
11 my $cleanup = sub { rmtree([@$home_trash, @_]) };
14 ok(!lei([]), 'no args fails');
15 is($? >> 8, 1, '$? is 1');
16 is($lei_out, '', 'nothing in stdout');
17 like($lei_err, qr/^usage:/sm, 'usage in stderr');
19 for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
21 like($lei_out, qr/^usage:/sm, "usage in stdout (@$arg)");
22 is($lei_err, '', "nothing in stderr (@$arg)");
25 for my $arg ([''], ['--halp'], ['halp'], [qw(daemon-pid --halp)]) {
26 ok(!lei($arg), "lei @$arg");
27 is($? >> 8, 1, '$? set correctly');
28 isnt($lei_err, '', 'something in stderr');
29 is($lei_out, '', 'nothing in stdout');
32 like($lei_out, qr! \Q$home\E/\.local/share/lei/store\b!,
33 'actual path shown in init -h');
34 lei_ok(qw(init -h), { XDG_DATA_HOME => '/XDH' },
35 \'init with XDG_DATA_HOME');
36 like($lei_out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
37 is($lei_err, '', 'no errors from init -h');
39 lei_ok(qw(config -h));
40 like($lei_out, qr! \Q$home\E/\.config/lei/config\b!,
41 'actual path shown in config -h');
42 lei_ok(qw(config -h), { XDG_CONFIG_HOME => '/XDC' },
43 \'config with XDG_CONFIG_HOME');
44 like($lei_out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
45 is($lei_err, '', 'no errors from config -h');
48 my $ok_err_info = sub {
50 is(grep(!/^#/, split(/^/, $lei_err)), 0, $msg) or
51 diag "$msg: err=$lei_err";
56 lei_ok('init', \'init w/o args');
57 $ok_err_info->('after init w/o args');
58 lei_ok('init', \'idempotent init w/o args');
59 $ok_err_info->('after idempotent init w/o args');
61 ok(!lei('init', "$home/x"), 'init conflict');
62 is(grep(/^E:/, split(/^/, $lei_err)), 1, 'got error on conflict');
63 ok(!-e "$home/x", 'nothing created on conflict');
66 lei_ok('init', "$home/x", \'init conflict resolved');
67 $ok_err_info->('init w/ arg');
68 lei_ok('init', "$home/x", \'init idempotent w/ path');
69 $ok_err_info->('init idempotent w/ arg');
70 ok(-d "$home/x", 'created dir');
71 $cleanup->("$home/x");
73 ok(!lei('init', "$home/x", "$home/2"), 'too many args fails');
74 like($lei_err, qr/too many/, 'noted excessive');
75 ok(!-e "$home/x", 'x not created on excessive');
76 for my $d (@$home_trash) {
77 my $base = (split(m!/!, $d))[-1];
78 ok(!-d $d, "$base not created");
80 is($lei_out, '', 'nothing in stdout on init failure');
83 my $test_config = sub {
85 lei_ok(qw(config a.b c), \'config set var');
86 is($lei_out.$lei_err, '', 'no output on var set');
87 lei_ok(qw(config -l), \'config -l');
88 is($lei_err, '', 'no errors on listing');
89 is($lei_out, "a.b=c\n", 'got expected output');
90 ok(!lei(qw(config -f), "$home/.config/f", qw(x.y z)),
91 'config set var with -f fails');
92 like($lei_err, qr/not supported/, 'not supported noted');
93 ok(!-f "$home/config/f", 'no file created');
95 lei_ok(qw(-c imap.debug config --bool imap.debug));
96 is($lei_out, "true\n", "-c sets w/o value");
97 lei_ok(qw(-c imap.debug=1 config --bool imap.debug));
98 is($lei_out, "true\n", "-c coerces value");
99 lei_ok(qw(-c imap.debug=tr00 config imap.debug));
100 is($lei_out, "tr00\n", "-c string value passed as-is");
101 lei_ok(qw(-c imap.debug=a -c imap.debug=b config --get-all imap.debug));
102 is($lei_out, "a\nb\n", '-c and --get-all work together');
104 lei_ok([qw(config -e)], { VISUAL => 'cat', EDITOR => 'cat' });
105 is($lei_out, "[a]\n\tb = c\n", '--edit works');
108 my $test_completion = sub {
109 lei_ok(qw(_complete lei), \'no errors on complete');
110 my %out = map { $_ => 1 } split(/\s+/s, $lei_out);
111 ok($out{'q'}, "`lei q' offered as completion");
112 ok($out{'add-external'}, "`lei add-external' offered as completion");
114 lei_ok(qw(_complete lei q), \'complete q (no args)');
115 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
116 for my $sw (qw(-f --format -o --output --mfolder --augment -a
117 --mua --no-local --local --verbose -v
118 --save --no-save --no-remote --remote --torsocks
120 ok($out{$sw}, "$sw offered as `lei q' completion");
123 lei_ok(qw(_complete lei q --form), \'complete q --format');
124 is($lei_out, "--format\n", 'complete lei q --format');
125 for my $sw (qw(-f --format)) {
126 lei_ok(qw(_complete lei q), $sw);
127 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
128 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
129 concatjson maildir)) {
130 ok($out{$f}, "got $sw $f as output format");
133 lei_ok(qw(_complete lei import));
134 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
135 for my $sw (qw(--no-kw --kw)) {
136 ok($out{$sw}, "$sw offered as `lei import' completion");
140 my $test_fail = sub {
141 lei('q', 'whatever', '-C', '/dev/null');
142 is($? >> 8, 1, 'chdir at end fails to /dev/null');
143 lei('-C', '/dev/null', 'q', 'whatever');
144 is($? >> 8, 1, 'chdir at beginning fails to /dev/null');
146 lei_ok('q', "foo\n");
147 like($lei_err, qr/trailing `\\n' removed/s, "noted `\\n' removal");
149 for my $lk (qw(ei inbox)) {
150 my $d = "$home/newline\n$lk";
151 my $all = $lk eq 'ei' ? 'ALL' : 'all';
152 File::Path::mkpath("$d/$all.git/objects");
153 open my $fh, '>', "$d/$lk.lock" or BAIL_OUT "open $d/$lk.lock";
154 for my $fl (qw(-I --only)) {
155 ok(!lei('q', $fl, $d, 'whatever'),
156 "newline $lk.lock fails with q $fl");
157 like($lei_err, qr/`\\n' not allowed/,
158 "error noted with q $fl");
161 lei_ok('sucks', \'yes, but hopefully less every day');
162 like($lei_out, qr/loaded features/, 'loaded features shown');
164 skip 'no curl', 3 unless require_cmd('curl', 1);
165 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
166 is($? >> 8, 3, 'got curl exit for bogus URL');
167 lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
168 is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
170 is($lei_out, '', 'no output');
176 $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
180 $test_completion->();
184 test_lei({ mods => [] }, sub {
185 lei_ok('sucks', \'no optional modules required');