]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei.t
lei: support "-C" to chdir in all sub commands
[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
98 my $test_completion = sub {
99         lei_ok(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         lei_ok(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         lei_ok(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                 lei_ok(qw(_complete lei q), $sw);
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         lei_ok(qw(_complete lei 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         lei('q', 'whatever', '-C', '/dev/null');
133         is($? >> 8, 1, 'chdir at end fails to /dev/null');
134         lei('-C', '/dev/null', 'q', 'whatever');
135         is($? >> 8, 1, 'chdir at beginning fails to /dev/null');
136 SKIP: {
137         skip 'no curl', 3 unless which('curl');
138         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
139         is($? >> 8, 3, 'got curl exit for bogus URL');
140         lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
141         is($? >> 8, 3, 'got curl exit for bogus URL with Maildir');
142         is($lei_out, '', 'no output');
143 }; # /SKIP
144 };
145
146 test_lei(sub {
147         $home = $ENV{HOME};
148         $home_trash = [ "$home/.local", "$home/.config", "$home/junk" ];
149         $test_help->();
150         $test_config->();
151         $test_init->();
152         $test_completion->();
153         $test_fail->();
154 });
155
156 done_testing;