]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-watch.t
xt/mem-imapd-tls: update aliases to DSdeflate subs
[public-inbox.git] / t / lei-watch.t
1 #!perl -w
2 # Copyright 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(make_path remove_tree);
6 plan skip_all => "TEST_FLAKY not enabled for $0" if !$ENV{TEST_FLAKY};
7 require_mods('lei');
8 my $have_fast_inotify = eval { require Linux::Inotify2 } ||
9         eval { require IO::KQueue };
10
11 $have_fast_inotify or
12         diag("$0 IO::KQueue or Linux::Inotify2 missing, test will be slow");
13
14 my ($ro_home, $cfg_path) = setup_public_inboxes;
15 test_lei(sub {
16         my $md = "$ENV{HOME}/md";
17         my $cfg_f = "$ENV{HOME}/.config/lei/config";
18         my $md2 = $md.'2';
19         lei_ok 'ls-watch';
20         is($lei_out, '', 'nothing in ls-watch, yet');
21
22         my ($ino_fdinfo, $ino_contents);
23         SKIP: {
24                 $have_fast_inotify && $^O eq 'linux' or
25                         skip 'Linux/inotify-only internals check', 1;
26                 lei_ok 'daemon-pid'; chomp(my $pid = $lei_out);
27                 skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$pid/fd";
28                 my @ino = grep {
29                         (readlink($_) // '') =~ /\binotify\b/
30                 } glob("/proc/$pid/fd/*");
31                 is(scalar(@ino), 1, 'only one inotify FD');
32                 my $ino_fd = (split('/', $ino[0]))[-1];
33                 $ino_fdinfo = "/proc/$pid/fdinfo/$ino_fd";
34                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
35                 $ino_contents = [ <$fh> ];
36         }
37
38         if (0) { # TODO
39                 my $url = 'imaps://example.com/foo.bar.0';
40                 lei_ok([qw(add-watch --state=pause), $url], undef, {});
41                 lei_ok 'ls-watch';
42                 is($lei_out, "$url\n", 'ls-watch shows added watch');
43                 ok(!lei(qw(add-watch --state=pause), 'bogus'.$url),
44                         'bogus URL rejected');
45         }
46
47         # first, make sure tag-ro works
48         make_path("$md/new", "$md/cur", "$md/tmp");
49         lei_ok qw(add-watch --state=tag-ro), $md;
50         lei_ok 'ls-watch';
51         like($lei_out, qr/^\Qmaildir:$md\E$/sm, 'maildir shown');
52         lei_ok qw(q mid:testmessage@example.com -o), $md, '-I', "$ro_home/t1";
53         my @f = glob("$md/cur/*:2,");
54         is(scalar(@f), 1, 'got populated maildir with one result');
55         rename($f[0], "$f[0]S") or xbail "rename $!"; # set (S)een
56         tick($have_fast_inotify ? 0.2 : 2.2); # always needed for 1 CPU systems
57         lei_ok qw(note-event done); # flushes immediately (instead of 5s)
58
59         lei_ok qw(q mid:testmessage@example.com -o), $md2, '-I', "$ro_home/t1";
60         my @f2 = glob("$md2/*/*");
61         is(scalar(@f2), 1, 'got one result');
62         like($f2[0], qr/S\z/, 'seen set from rename') or diag explain(\@f2);
63         my $e2 = eml_load($f2[0]);
64         my $e1 = eml_load("$f[0]S");
65         is_deeply($e2, $e1, 'results match');
66
67         SKIP: {
68                 $ino_fdinfo or skip 'Linux/inotify-only watch check', 1;
69                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
70                 my $cmp = [ <$fh> ];
71                 ok(scalar(@$cmp) > scalar(@$ino_contents),
72                         'inotify has Maildir watches');
73         }
74
75         lei_ok 'rm-watch', $md;
76         lei_ok 'ls-watch', \'refresh watches';
77         is($lei_out, '', 'no watches left');
78
79         lei_ok 'add-watch', $md2;
80         remove_tree($md2);
81         lei_ok 'rm-watch', "maildir:".$md2, \'with maildir: prefix';
82         lei_ok 'ls-watch', \'refresh watches';
83         is($lei_out, '', 'no watches left');
84
85         lei_ok 'add-watch', $md;
86         remove_tree($md);
87         lei_ok 'rm-watch', $md, \'absolute path w/ missing dir';
88         lei_ok 'ls-watch', \'refresh watches';
89         is($lei_out, '', 'no watches left');
90
91         SKIP: {
92                 $ino_fdinfo or skip 'Linux/inotify-only removal removal', 1;
93                 open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
94                 my $cmp = [ <$fh> ];
95                 is_xdeeply($cmp, $ino_contents, 'inotify Maildir watches gone');
96         };
97 });
98
99 done_testing;