]> Sergey Matveev's repositories - public-inbox.git/blob - t/purge.t
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / t / purge.t
1 # Copyright (C) 2019-2020 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         -primary_address => 'test@example.com',
20         indexlevel => 'basic',
21 });
22
23 my $raw = <<'EOF';
24 From: a@example.com
25 To: test@example.com
26 Subject: this is a subject
27 Message-ID: <a-mid@b>
28 Date: Fri, 02 Oct 1993 00:00:00 +0000
29
30 Hello World
31
32 EOF
33
34 my $cfgfile = "$tmpdir/config";
35 local $ENV{PI_CONFIG} = $cfgfile;
36 open my $cfg_fh, '>', $cfgfile or die "open: $!";
37
38 my $v2w = PublicInbox::V2Writable->new($ibx, {nproc => 1});
39 my $mime = PublicInbox::MIME->new($raw);
40 ok($v2w->add($mime), 'add message to be purged');
41 $v2w->done;
42
43 # failing cases, first:
44 my $in = "$raw\nMOAR\n";
45 my ($out, $err) = ('', '');
46 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
47 ok(run_script([$purge, '-f', $inboxdir], undef, $opt), 'purge -f OK');
48
49 $out = $err = '';
50 ok(!run_script([$purge, $inboxdir], undef, $opt), 'mismatch fails without -f');
51 is($? >> 8, 1, 'missed purge exits with 1');
52
53 # a successful case:
54 $opt->{0} = \$raw;
55 ok(run_script([$purge, $inboxdir], undef, $opt), 'match OK');
56 like($out, qr/\b[a-f0-9]{40,}/m, 'removed commit noted');
57
58 # add (old) vger filter to config file
59 print $cfg_fh <<EOF or die "print $!";
60 [publicinbox "test-v2purge"]
61         inboxdir = $inboxdir
62         address = test\@example.com
63         indexlevel = basic
64         filter = PublicInbox::Filter::Vger
65 EOF
66 close $cfg_fh or die "close: $!";
67
68 ok($v2w->add($mime), 'add vger-signatured message to be purged');
69 $v2w->done;
70
71 my $pre_scrub = $raw . <<'EOF';
72
73 --
74 To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
75 the body of a message to majordomo@vger.kernel.org
76 More majordomo info at  http://vger.kernel.org/majordomo-info.html
77 Please read the FAQ at  http://www.tux.org/lkml/
78 EOF
79
80 $out = $err = '';
81 ok(chdir('/'), "chdir / OK for --all test");
82 $opt->{0} = \$pre_scrub;
83 ok(run_script([$purge, '--all'], undef, $opt), 'scrub purge OK');
84 like($out, qr/\b[a-f0-9]{40,}/m, 'removed commit noted');
85 # diag "out: $out"; diag "err: $err";
86
87 $out = $err = '';
88 ok(!run_script([$purge, '--all' ], undef, $opt),
89         'scrub purge not idempotent without -f');
90 # diag "out: $out"; diag "err: $err";
91
92 done_testing();