]> Sergey Matveev's repositories - public-inbox.git/blob - t/purge.t
No ext_urls
[public-inbox.git] / t / purge.t
1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 require_git(2.6);
8 require_mods(qw(DBD::SQLite));
9 use Cwd qw(abs_path); # we need this since we chdir below
10 local $ENV{HOME} = abs_path('t');
11 my $purge = abs_path('blib/script/public-inbox-purge');
12 my ($tmpdir, $for_destroy) = tmpdir();
13 use_ok 'PublicInbox::V2Writable';
14 my $inboxdir = "$tmpdir/v2";
15 my $ibx = PublicInbox::Inbox->new({
16         inboxdir => $inboxdir,
17         name => 'test-v2purge',
18         version => 2,
19         -no_fsync => 1,
20         -primary_address => 'test@example.com',
21         indexlevel => 'basic',
22 });
23
24 my $raw = <<'EOF';
25 From: a@example.com
26 To: test@example.com
27 Subject: this is a subject
28 Message-ID: <a-mid@b>
29 Date: Fri, 02 Oct 1993 00:00:00 +0000
30
31 Hello World
32
33 EOF
34
35 my $cfgfile = "$tmpdir/config";
36 local $ENV{PI_CONFIG} = $cfgfile;
37 open my $cfg_fh, '>', $cfgfile or die "open: $!";
38
39 my $v2w = PublicInbox::V2Writable->new($ibx, {nproc => 1});
40 my $mime = PublicInbox::Eml->new($raw);
41 ok($v2w->add($mime), 'add message to be purged');
42 $v2w->done;
43
44 # failing cases, first:
45 my $in = "$raw\nMOAR\n";
46 my ($out, $err) = ('', '');
47 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
48 ok(run_script([$purge, '-f', $inboxdir], undef, $opt), 'purge -f OK');
49
50 $out = $err = '';
51 ok(!run_script([$purge, $inboxdir], undef, $opt), 'mismatch fails without -f');
52 is($? >> 8, 1, 'missed purge exits with 1');
53
54 # a successful case:
55 $opt->{0} = \$raw;
56 ok(run_script([$purge, $inboxdir], undef, $opt), 'match OK');
57 like($out, qr/\b[a-f0-9]{40,}/m, 'removed commit noted');
58
59 # add (old) vger filter to config file
60 print $cfg_fh <<EOF or die "print $!";
61 [publicinbox "test-v2purge"]
62         inboxdir = $inboxdir
63         address = test\@example.com
64         indexlevel = basic
65         filter = PublicInbox::Filter::Vger
66 EOF
67 close $cfg_fh or die "close: $!";
68
69 ok($v2w->add($mime), 'add vger-signatured message to be purged');
70 $v2w->done;
71
72 my $pre_scrub = $raw . <<'EOF';
73
74 --
75 To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
76 the body of a message to majordomo@vger.kernel.org
77 More majordomo info at  http://vger.kernel.org/majordomo-info.html
78 Please read the FAQ at  http://www.tux.org/lkml/
79 EOF
80
81 $out = $err = '';
82 ok(chdir('/'), "chdir / OK for --all test");
83 $opt->{0} = \$pre_scrub;
84 ok(run_script([$purge, '--all'], undef, $opt), 'scrub purge OK');
85 like($out, qr/\b[a-f0-9]{40,}/m, 'removed commit noted');
86 # diag "out: $out"; diag "err: $err";
87
88 $out = $err = '';
89 ok(!run_script([$purge, '--all' ], undef, $opt),
90         'scrub purge not idempotent without -f');
91 # diag "out: $out"; diag "err: $err";
92
93 done_testing();