]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei import|convert: drop --no-kw aliases
[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 use PublicInbox::Spawn qw(which);
7
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');
11 my $home;
12 my $home_trash = [];
13 my $cleanup = sub { rmtree([@$home_trash, @_]) };
14
15 my $test_help = sub {
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');
20
21         for my $arg (['-h'], ['--help'], ['help'], [qw(daemon-pid --help)]) {
22                 lei_ok($arg);
23                 like($lei_out, qr/^usage:/sm, "usage in stdout (@$arg)");
24                 is($lei_err, '', "nothing in stderr (@$arg)");
25         }
26
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');
32         }
33         lei_ok(qw(init -h));
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');
40
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');
48 };
49
50 my $ok_err_info = sub {
51         my ($msg) = @_;
52         is(grep(!/^#/, split(/^/, $lei_err)), 0, $msg) or
53                 diag "$msg: err=$lei_err";
54 };
55
56 my $test_init = sub {
57         $cleanup->();
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');
62
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');
66         $cleanup->();
67
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");
74
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");
81         }
82         is($lei_out, '', 'nothing in stdout on init failure');
83 };
84
85 my $test_config = sub {
86         $cleanup->();
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');
96
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');
105 };
106
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");
112
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
118                         --reverse -r )) {
119                 ok($out{$sw}, "$sw offered as `lei q' completion");
120         }
121
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");
130                 }
131         }
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");
136         }
137 };
138
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');
144
145         for my $lk (qw(ei inbox)) {
146                 my $d = "$home/newline\n$lk";
147                 mkdir $d;
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");
154                 }
155         }
156         lei_ok('sucks', \'yes, but hopefully less every day');
157 SKIP: {
158         skip 'no curl', 3 unless which('curl');
159         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
160         is($? >> 8, 3, 'got curl exit for bogus URL');
161         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
162         is($? >> 8, 3, 'got curl exit for bogus URL with Maildir') or
163                 diag $lei_err;
164         is($lei_out, '', 'no output');
165 }; # /SKIP
166 };
167
168 test_lei(sub {
169         $home = $ENV{HOME};
170         $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
171         $test_help->();
172         $test_config->();
173         $test_init->();
174         $test_completion->();
175         $test_fail->();
176 });
177
178 done_testing;