]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / lei.t
1 #!perl -w
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
7 # this only tests the basic help/config/init/completion bits of lei;
8 # actual functionality is tested in other t/lei-*.t tests
9 my $home;
10 my $home_trash = [];
11 my $cleanup = sub { rmtree([@$home_trash, @_]) };
12
13 my $test_help = sub {
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');
18
19         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
20                 lei_ok($arg);
21                 like($lei_out, qr/^usage:/sm, "usage in stdout (@$arg)");
22                 is($lei_err, '', "nothing in stderr (@$arg)");
23         }
24
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');
30         }
31         lei_ok(qw(init -h));
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');
38
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');
46 };
47
48 my $ok_err_info = sub {
49         my ($msg) = @_;
50         is(grep(!/^#/, split(/^/, $lei_err)), 0, $msg) or
51                 diag "$msg: err=$lei_err";
52 };
53
54 my $test_init = sub {
55         $cleanup->();
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');
60
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');
64         $cleanup->();
65
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");
72
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");
79         }
80         is($lei_out, '', 'nothing in stdout on init failure');
81 };
82
83 my $test_config = sub {
84         $cleanup->();
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');
94
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');
103
104         lei_ok([qw(config -e)], { VISUAL => 'cat', EDITOR => 'cat' });
105         is($lei_out, "[a]\n\tb = c\n", '--edit works');
106 };
107
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");
113
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
119                         --reverse -r )) {
120                 ok($out{$sw}, "$sw offered as `lei q' completion");
121         }
122
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");
131                 }
132         }
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");
137         }
138 };
139
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');
145
146         lei_ok('q', "foo\n");
147         like($lei_err, qr/trailing `\\n' removed/s, "noted `\\n' removal");
148
149         for my $lk (qw(ei inbox)) {
150                 my $d = "$home/newline\n$lk";
151                 mkdir $d;
152                 open my $fh, '>', "$d/$lk.lock" or BAIL_OUT "open $d/$lk.lock";
153                 for my $fl (qw(-I --only)) {
154                         ok(!lei('q', $fl, $d, 'whatever'),
155                                 "newline $lk.lock fails with q $fl");
156                         like($lei_err, qr/`\\n' not allowed/,
157                                 "error noted with q $fl");
158                 }
159         }
160         lei_ok('sucks', \'yes, but hopefully less every day');
161         like($lei_out, qr/loaded features/, 'loaded features shown');
162 SKIP: {
163         skip 'no curl', 3 unless require_cmd('curl', 1);
164         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
165         is($? >> 8, 3, 'got curl exit for bogus URL');
166         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
167         is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
168                 diag $lei_err;
169         is($lei_out, '', 'no output');
170 }; # /SKIP
171 };
172
173 test_lei(sub {
174         $home = $ENV{HOME};
175         $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
176         $test_help->();
177         $test_config->();
178         $test_init->();
179         $test_completion->();
180         $test_fail->();
181 });
182
183 test_lei({ mods => [] }, sub {
184         lei_ok('sucks', \'no optional modules required');
185 });
186
187 done_testing;