public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 8ad50d13aa024e99ed0c8459f50ad1a6857be488 3725 bytes (raw)
$ git show HEAD:t/lei-watch.t	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
 
#!perl -w
# Copyright all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
use File::Path qw(make_path remove_tree);
use PublicInbox::IO qw(write_file);
plan skip_all => "TEST_FLAKY not enabled for $0" if !$ENV{TEST_FLAKY};
require_mods('lei');
my $have_fast_inotify = eval { require PublicInbox::Inotify } ||
	eval { require IO::KQueue };

$have_fast_inotify or
	diag("$0 IO::KQueue or inotify missing, test will be slow");

my ($ro_home, $cfg_path) = setup_public_inboxes;
test_lei(sub {
	my ($md, $mh1, $mh2) = map { "$ENV{HOME}/$_" } qw(md mh1 mh2);
	my $cfg_f = "$ENV{HOME}/.config/lei/config";
	my $md2 = $md.'2';
	lei_ok 'ls-watch';
	is($lei_out, '', 'nothing in ls-watch, yet');

	my ($ino_fdinfo, $ino_contents);
	SKIP: {
		$have_fast_inotify && $^O eq 'linux' or
			skip 'Linux/inotify-only internals check', 1;
		lei_ok 'daemon-pid'; chomp(my $pid = $lei_out);
		skip 'missing /proc/$PID/fd', 1 if !-d "/proc/$pid/fd";
		my @ino = grep {
			(readlink($_) // '') =~ /\binotify\b/
		} glob("/proc/$pid/fd/*");
		is(scalar(@ino), 1, 'only one inotify FD');
		my $ino_fd = (split('/', $ino[0]))[-1];
		$ino_fdinfo = "/proc/$pid/fdinfo/$ino_fd";
		open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
		$ino_contents = [ <$fh> ];
	}

	if (0) { # TODO
		my $url = 'imaps://example.com/foo.bar.0';
		lei_ok([qw(add-watch --state=pause), $url], undef, {});
		lei_ok 'ls-watch';
		is($lei_out, "$url\n", 'ls-watch shows added watch');
		ok(!lei(qw(add-watch --state=pause), 'bogus'.$url),
			'bogus URL rejected');
	}

	# first, make sure tag-ro works
	make_path("$md/new", "$md/cur", "$md/tmp", $mh1, $mh2);
	lei_ok qw(add-watch --state=tag-ro), $md;
	lei_ok 'ls-watch';
	like($lei_out, qr/^\Qmaildir:$md\E$/sm, 'maildir shown');
	lei_ok qw(q mid:testmessage@example.com -o), $md, '-I', "$ro_home/t1";
	my @f = glob("$md/cur/*:2,");
	is(scalar(@f), 1, 'got populated maildir with one result');

	rename($f[0], "$f[0]S") or xbail "rename $!"; # set (S)een
	tick($have_fast_inotify ? 0.2 : 2.2); # always needed for 1 CPU systems
	lei_ok qw(note-event done); # flushes immediately (instead of 5s)

	lei_ok qw(q mid:testmessage@example.com -o), $md2, '-I', "$ro_home/t1";
	my @f2 = glob("$md2/*/*");
	is(scalar(@f2), 1, 'got one result');
	like($f2[0], qr/S\z/, 'seen set from rename') or diag explain(\@f2);
	my $e2 = eml_load($f2[0]);
	my $e1 = eml_load("$f[0]S");
	is_deeply($e2, $e1, 'results match');

	SKIP: {
		$ino_fdinfo or skip 'Linux/inotify-only watch check', 1;
		open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
		my $cmp = [ <$fh> ];
		ok(scalar(@$cmp) > scalar(@$ino_contents),
			'inotify has Maildir watches');
	}

	lei_ok 'rm-watch', $md;
	lei_ok 'ls-watch', \'refresh watches';
	is($lei_out, '', 'no watches left');

	lei_ok 'add-watch', $md2;
	remove_tree($md2);
	lei_ok 'rm-watch', "maildir:".$md2, \'with maildir: prefix';
	lei_ok 'ls-watch', \'refresh watches';
	is($lei_out, '', 'no watches left');

	lei_ok 'add-watch', $md;
	remove_tree($md);
	lei_ok 'rm-watch', $md, \'absolute path w/ missing dir';
	lei_ok 'ls-watch', \'refresh watches';
	is($lei_out, '', 'no watches left');

	SKIP: {
		$ino_fdinfo or skip 'Linux/inotify-only removal removal', 1;
		open my $fh, '<', $ino_fdinfo or xbail "open $ino_fdinfo: $!";
		my $cmp = [ <$fh> ];
		is_xdeeply($cmp, $ino_contents, 'inotify Maildir watches gone');
	};

	write_file '>', "$mh1/.mh_sequences";
	lei_ok qw(add-watch --state=tag-ro), $mh1, "mh:$mh2";
	lei_ok 'ls-watch', \'refresh watches';
	like $lei_out, qr/^\Qmh:$mh1\E$/sm, 'MH 1 shown';
	like $lei_out, qr/^\Qmh:$mh2\E$/sm, 'MH 2 shown';
});

done_testing;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git