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