]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-refresh-mail-sync.t
lei refresh-mail-sync: implicitly remove missing folders
[public-inbox.git] / t / lei-refresh-mail-sync.t
1 #!perl -w
2 # Copyright (C) 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 require_mods(qw(lei));
6 use File::Path qw(remove_tree);
7
8 my $stop_daemon = sub { # needed since we don't have inotify
9         lei_ok qw(daemon-pid);
10         chomp(my $pid = $lei_out);
11         $pid > 0 or xbail "bad pid: $pid";
12         kill('TERM', $pid) or xbail "kill: $!";
13         for (0..10) {
14                 tick;
15                 kill(0, $pid) or last;
16         }
17         kill(0, $pid) and xbail "daemon still running (PID:$pid)";
18 };
19
20 test_lei({ daemon_only => 1 }, sub {
21         my $d = "$ENV{HOME}/d";
22         my ($ro_home, $cfg_path) = setup_public_inboxes;
23         lei_ok qw(daemon-pid);
24         lei_ok qw(add-external), "$ro_home/t2";
25         lei_ok qw(q mid:testmessage@example.com -o), "Maildir:$d";
26         my (@o) = glob("$d/*/*");
27         scalar(@o) == 1 or xbail('multiple results', \@o);
28         my ($bn0) = ($o[0] =~ m!/([^/]+)\z!);
29
30         my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
31         lei_ok 'inspect', "blob:$oid";
32         my $before = json_utf8->decode($lei_out);
33         my $exp0 = { 'mail-sync' => { "maildir:$d" => [ $bn0 ] } };
34         is_deeply($before, $exp0, 'inspect shows expected');
35
36         $stop_daemon->();
37         my $dst = $o[0];
38         $dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
39                 rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";
40
41         lei_ok 'inspect', "blob:$oid";
42         is_deeply(json_utf8->decode($lei_out),
43                 $before, 'inspect unchanged immediately after restart');
44         lei_ok 'refresh-mail-sync', '--all';
45         lei_ok 'inspect', "blob:$oid";
46         my ($bn1) = ($dst =~ m!/([^/]+)\z!);
47         my $exp1 = { 'mail-sync' => { "maildir:$d" => [ $bn1 ] } };
48         is_deeply(json_utf8->decode($lei_out), $exp1,
49                 'refresh-mail-sync updated location');
50
51         $stop_daemon->();
52         rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";
53
54         lei_ok 'refresh-mail-sync', $d;
55         lei_ok 'inspect', "blob:$oid";
56         is($lei_out, '{}', 'no known locations after "removal"');
57         lei_ok 'refresh-mail-sync', "Maildir:$d";
58
59         $stop_daemon->();
60         rename("$d/unwatched", $dst) or xbail "rename $dst back";
61
62         lei_ok 'refresh-mail-sync', "Maildir:$d";
63         lei_ok 'inspect', "blob:$oid";
64         is_deeply(json_utf8->decode($lei_out), $exp1,
65                 'replaced file noted again');
66
67         $stop_daemon->();
68
69         remove_tree($d);
70         lei_ok 'refresh-mail-sync', '--all';
71         lei_ok 'inspect', "blob:$oid";
72         is($lei_out, '{}', 'no known locations after "removal"');
73         lei_ok 'ls-mail-sync';
74         is($lei_out, '', 'no sync left when folder is gone');
75 });
76
77 done_testing;