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