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