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