]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-q-save.t
t/lei-*: better diagnostics for occasional failures
[public-inbox.git] / t / lei-q-save.t
1 #!perl -w
2 # Copyright (C) 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 PublicInbox::Smsg;
6 my $doc1 = eml_load('t/plack-qp.eml');
7 $doc1->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 5)}));
8 my $doc2 = eml_load('t/utf8.eml');
9 $doc2->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
10 my $doc3 = eml_load('t/msg_iter-order.eml');
11 $doc3->header_set('Date', PublicInbox::Smsg::date({ds => time - (86400 * 4)}));
12
13 my $pre_existing = <<'EOF';
14 From x Mon Sep 17 00:00:00 2001
15 Message-ID: <import-before@example.com>
16 Subject: pre-existing
17 Date: Sat, 02 Oct 2010 00:00:00 +0000
18
19 blah
20 EOF
21
22 test_lei(sub {
23         my $home = $ENV{HOME};
24         my $in = $doc1->as_string;
25         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
26         lei_ok qw(q -q --save z:0.. d:last.week..), '-o', "MAILDIR:$home/md/";
27         my %before = map { $_ => 1 } glob("$home/md/cur/*");
28         my $f = (keys %before)[0] or xbail({before => \%before});
29         is_deeply(eml_load($f), $doc1, 'doc1 matches');
30         lei_ok qw(ls-mail-sync);
31         is($lei_out, "maildir:$home/md\n", 'canonicalized mail sync name');
32
33         my @s = glob("$home/.local/share/lei/saved-searches/md-*");
34         is(scalar(@s), 1, 'got one saved search');
35         my $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
36         is($cfg->{'lei.q.output'}, "maildir:$home/md", 'canonicalized output');
37         is_deeply($cfg->{'lei.q'}, ['z:0..', 'd:last.week..'],
38                 'store relative time, not parsed (absolute) timestamp');
39
40         # ensure "lei up" works, since it compliments "lei q --save"
41         $in = $doc2->as_string;
42         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
43         lei_ok qw(up -q md -C), $home;
44         lei_ok qw(up -q . -C), "$home/md";
45         lei_ok qw(up -q), "/$home/md";
46         my %after = map { $_ => 1 } glob("$home/md/{new,cur}/*");
47         is(delete $after{(keys(%before))[0]}, 1, 'original message kept');
48         is(scalar(keys %after), 1, 'one new message added');
49         $f = (keys %after)[0] or xbail({after => \%after});
50         is_deeply(eml_load($f), $doc2, 'doc2 matches');
51
52         # check stdin
53         lei_ok [qw(q --save - -o), "mboxcl2:mbcl2" ],
54                 undef, { -C => $home, %$lei_opt, 0 => \'d:last.week..'};
55         @s = glob("$home/.local/share/lei/saved-searches/mbcl2-*");
56         $cfg = PublicInbox::Config->new("$s[0]/lei.saved-search");
57         is_deeply $cfg->{'lei.q'}, 'd:last.week..',
58                 'q --stdin stores relative time';
59         my $size = -s "$home/mbcl2";
60         ok(defined($size) && $size > 0, 'results written');
61         lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
62         is(-s "$home/mbcl2", $size, 'size unchanged on noop up');
63
64         $in = $doc3->as_string;
65         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
66         lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
67         ok(-s "$home/mbcl2" > $size, 'size increased after up');
68
69         ok(!lei(qw(up -q), $home), 'up fails w/o --save');
70
71         lei_ok qw(ls-search); my @d = split(/\n/, $lei_out);
72         lei_ok qw(ls-search -z); my @z = split(/\0/, $lei_out);
73         is_deeply(\@d, \@z, '-z output matches non-z');
74         is_deeply(\@d, [ "$home/mbcl2", "$home/md" ],
75                 'ls-search output alphabetically sorted');
76         lei_ok qw(ls-search -l);
77         my $json = PublicInbox::Config->json->decode($lei_out);
78         ok($json && $json->[0]->{output}, 'JSON has output');
79         lei_ok qw(_complete lei up);
80         like($lei_out, qr!^\Q$home/mbcl2\E$!sm, 'complete got mbcl2 output');
81         like($lei_out, qr!^\Q$home/md\E$!sm, 'complete got maildir output');
82
83         unlink("$home/mbcl2") or xbail "unlink $!";
84         lei_ok qw(_complete lei up);
85         like($lei_out, qr!^\Q$home/mbcl2\E$!sm,
86                 'mbcl2 output shown despite unlink');
87         lei_ok([qw(up mbcl2)], undef, { -C => $home, %$lei_opt });
88         ok(-f "$home/mbcl2"  && -s _ == 0, 'up recreates on missing output');
89
90         # no --augment
91         open my $mb, '>', "$home/mbrd" or xbail "open $!";
92         print $mb $pre_existing;
93         close $mb or xbail "close: $!";
94         lei_ok(qw(q --save -o mboxrd:mbrd m:qp@example.com -C), $home);
95         open $mb, '<', "$home/mbrd" or xbail "open $!";
96         is_deeply([grep(/pre-existing/, <$mb>)], [],
97                 'pre-existing messsage gone w/o augment');
98         close $mb;
99         lei_ok(qw(q m:import-before@example.com));
100         is(json_utf8->decode($lei_out)->[0]->{'s'},
101                 'pre-existing', '--save imported before clobbering');
102
103         # --augment
104         open $mb, '>', "$home/mbrd-aug" or xbail "open $!";
105         print $mb $pre_existing;
106         close $mb or xbail "close: $!";
107         lei_ok(qw(q -a --save -o mboxrd:mbrd-aug m:qp@example.com -C), $home);
108         open $mb, '<', "$home/mbrd-aug" or xbail "open $!";
109         $mb = do { local $/; <$mb> };
110         like($mb, qr/pre-existing/, 'pre-existing message preserved w/ -a');
111         like($mb, qr/<qp\@example\.com>/, 'new result written w/ -a');
112
113         lei_ok(qw(up --all=local));
114
115         ok(!lei(qw(forget-search), "$home/bogus"), 'bogus forget');
116         lei_ok qw(_complete lei forget-search);
117         like($lei_out, qr/mbrd-aug/, 'forget-search completion');
118         lei_ok(qw(forget-search -v), "$home/mbrd-aug");
119         is($lei_out, '', 'no output');
120         like($lei_err, qr/\bmbrd-aug\b/, '-v (verbose) reported unlinks');
121         lei_ok qw(_complete lei forget-search);
122         unlike($lei_out, qr/mbrd-aug/,
123                 'forget-search completion cleared after forget');
124         ok(!lei('up', "$home/mbrd-aug"), 'lei up fails after forget');
125
126         # dedupe=mid
127         my $o = "$home/dd-mid";
128         $in = $doc2->as_string . "\n-------\nappended list sig\n";
129         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
130         lei_ok(qw(q --dedupe=mid --save m:testmessage@example.com -o), $o);
131         my @m = glob("$o/cur/*");
132         is(scalar(@m), 1, '--dedupe=mid w/ --save');
133         $in = $doc2->as_string . "\n-------\nanother list sig\n";
134         lei_ok [qw(import -q -F eml -)], undef, { 0 => \$in, %$lei_opt };
135         lei_ok 'up', $o;
136         is_deeply([glob("$o/cur/*")], \@m, 'lei up dedupe=mid works');
137
138         for my $dd (qw(content)) {
139                 $o = "$home/dd-$dd";
140                 lei_ok(qw(q --save m:testmessage@example.com -o), $o,
141                                 "--dedupe=$dd");
142                 @m = glob("$o/cur/*");
143                 is(scalar(@m), 3, 'all 3 matches with dedupe='.$dd);
144         }
145
146         # dedupe=oid
147         $o = "$home/dd-oid";
148         my $ibx = create_inbox 'ibx', indexlevel => 'medium',
149                         tmpdir => "$home/v1", sub {};
150         lei_ok(qw(q --save --dedupe=oid m:qp@example.com -o), $o,
151                 '-I', $ibx->{inboxdir});
152         @m = glob("$o/cur/*");
153         is(scalar(@m), 1, 'got first result');
154
155         my $im = $ibx->importer(0);
156         my $diff = "X-Insignificant-Header: x\n".$doc1->as_string;
157         $im->add(PublicInbox::Eml->new($diff));
158         $im->done;
159         lei_ok('up', $o);
160         @m = glob("$o/{new,cur}/*");
161         is(scalar(@m), 2, 'got 2nd result due to different OID');
162
163         SKIP: {
164                 symlink($o, "$home/ln -s") or
165                         skip "symlinks not supported in $home?: $!", 1;
166                 lei_ok('up', "$home/ln -s");
167         };
168 });
169 done_testing;