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