]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: replace "I:"-prefixed info messages with "#"
[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                 ok($lei->($arg), "lei @$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         ok($lei->(qw(init -h)), 'init -h');
34         like($lei_out, qr! \Q$home\E/\.local/share/lei/store\b!,
35                 'actual path shown in init -h');
36         ok($lei->(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         ok($lei->(qw(config -h)), 'config-h');
42         like($lei_out, qr! \Q$home\E/\.config/lei/config\b!,
43                 'actual path shown in config -h');
44         ok($lei->(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         ok($lei->('init'), 'init w/o args');
59         $ok_err_info->('after init w/o args');
60         ok($lei->('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         ok($lei->('init', "$home/x"), 'init conflict resolved');
69         $ok_err_info->('init w/ arg');
70         ok($lei->('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         ok($lei->(qw(config a.b c)), 'config set var');
88         is($lei_out.$lei_err, '', 'no output on var set');
89         ok($lei->(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
98 my $test_completion = sub {
99         ok($lei->(qw(_complete lei)), 'no errors on complete');
100         my %out = map { $_ => 1 } split(/\s+/s, $lei_out);
101         ok($out{'q'}, "`lei q' offered as completion");
102         ok($out{'add-external'}, "`lei add-external' offered as completion");
103
104         ok($lei->(qw(_complete lei q)), 'complete q (no args)');
105         %out = map { $_ => 1 } split(/\s+/s, $lei_out);
106         for my $sw (qw(-f --format -o --output --mfolder --augment -a
107                         --mua --no-local --local --verbose -v
108                         --save-as --no-remote --remote --torsocks
109                         --reverse -r )) {
110                 ok($out{$sw}, "$sw offered as `lei q' completion");
111         }
112
113         ok($lei->(qw(_complete lei q --form)), 'complete q --format');
114         is($lei_out, "--format\n", 'complete lei q --format');
115         for my $sw (qw(-f --format)) {
116                 ok($lei->(qw(_complete lei q), $sw), "complete q $sw ARG");
117                 %out = map { $_ => 1 } split(/\s+/s, $lei_out);
118                 for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
119                                 concatjson maildir)) {
120                         ok($out{$f}, "got $sw $f as output format");
121                 }
122         }
123         ok($lei->(qw(_complete lei import)), 'complete import');
124         %out = map { $_ => 1 } split(/\s+/s, $lei_out);
125         for my $sw (qw(--flags --no-flags --no-kw --kw --no-keywords
126                         --keywords)) {
127                 ok($out{$sw}, "$sw offered as `lei import' completion");
128         }
129 };
130
131 my $test_fail = sub {
132 SKIP: {
133         skip 'no curl', 3 unless which('curl');
134         $lei->(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
135         is($? >> 8, 3, 'got curl exit for bogus URL');
136         $lei->(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
137         is($? >> 8, 3, 'got curl exit for bogus URL with Maildir');
138         is($lei_out, '', 'no output');
139 }; # /SKIP
140 };
141
142 test_lei(sub {
143         $home = $ENV{HOME};
144         $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
145         $test_help->();
146         $test_config->();
147         $test_init->();
148         $test_completion->();
149         $test_fail->();
150 });
151
152 done_testing;