]> Sergey Matveev's repositories - public-inbox.git/blob - t/convert-compact.t
t/convert-compact: avoid warning on `scalar(split(...))'
[public-inbox.git] / t / convert-compact.t
1 # Copyright (C) 2018-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::MIME;
7 use PublicInbox::Spawn qw(which);
8 use PublicInbox::TestCommon;
9 require_git(2.6);
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 which('xapian-compact') or
12         plan skip_all => 'xapian-compact missing for '.__FILE__;
13
14 use_ok 'PublicInbox::V2Writable';
15 use PublicInbox::Import;
16 my ($tmpdir, $for_destroy) = tmpdir();
17 my $ibx = {
18         inboxdir => "$tmpdir/v1",
19         name => 'test-v1',
20         -primary_address => 'test@example.com',
21 };
22
23 PublicInbox::Import::init_bare($ibx->{inboxdir});
24 ok(umask(077), 'set restrictive umask');
25 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{inboxdir}",
26         qw(config core.sharedRepository 0644)]), 'set sharedRepository');
27 $ibx = PublicInbox::Inbox->new($ibx);
28 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
29 my $mime = PublicInbox::MIME->new(<<'EOF');
30 From: a@example.com
31 To: b@example.com
32 Subject: this is a subject
33 Message-ID: <a-mid@b>
34 Date: Fri, 02 Oct 1993 00:00:00 +0000
35
36 hello world
37 EOF
38
39 ok($im->add($mime), 'added one message');
40 ok($im->remove($mime), 'remove message');
41 ok($im->add($mime), 'added message again');
42 $im->done;
43 for (1..2) {
44         eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
45         is($@, '', 'no errors syncing');
46 }
47
48 is(((stat("$ibx->{inboxdir}/public-inbox"))[2]) & 07777, 0755,
49         'sharedRepository respected for v1');
50 is(((stat("$ibx->{inboxdir}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
51         'sharedRepository respected for v1 msgmap');
52 my @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*/*");
53 foreach (@xdir) {
54         my @st = stat($_);
55         is($st[2] & 07777, -f _ ? 0644 : 0755,
56                 'sharedRepository respected on file after convert');
57 }
58
59 local $ENV{PI_CONFIG} = '/dev/null';
60 my ($out, $err) = ('', '');
61 my $rdr = { 1 => \$out, 2 => \$err };
62
63 my $cmd = [ '-compact', $ibx->{inboxdir} ];
64 ok(run_script($cmd, undef, $rdr), 'v1 compact works');
65
66 @xdir = glob("$ibx->{inboxdir}/public-inbox/xap*");
67 is(scalar(@xdir), 1, 'got one xapian directory after compact');
68 is(((stat($xdir[0]))[2]) & 07777, 0755,
69         'sharedRepository respected on v1 compact');
70
71 my $hwm = do {
72         my $mm = $ibx->mm;
73         $ibx->cleanup;
74         $mm->num_highwater;
75 };
76 ok(defined($hwm) && $hwm > 0, "highwater mark set #$hwm");
77
78 $cmd = [ '-convert', '--no-index', $ibx->{inboxdir}, "$tmpdir/no-index" ];
79 ok(run_script($cmd, undef, $rdr), 'convert --no-index works');
80
81 $cmd = [ '-convert', $ibx->{inboxdir}, "$tmpdir/v2" ];
82 ok(run_script($cmd, undef, $rdr), 'convert works');
83 @xdir = glob("$tmpdir/v2/xap*/*");
84 foreach (@xdir) {
85         my @st = stat($_);
86         is($st[2] & 07777, -f _ ? 0644 : 0755,
87                 'sharedRepository respected after convert');
88 }
89
90 $cmd = [ '-compact', "$tmpdir/v2" ];
91 my $env = { NPROC => 2 };
92 ok(run_script($cmd, $env, $rdr), 'v2 compact works');
93 $ibx->{inboxdir} = "$tmpdir/v2";
94 $ibx->{version} = 2;
95 is($ibx->mm->num_highwater, $hwm, 'highwater mark unchanged in v2 inbox');
96
97 @xdir = glob("$tmpdir/v2/xap*/*");
98 foreach (@xdir) {
99         my @st = stat($_);
100         is($st[2] & 07777, -f _ ? 0644 : 0755,
101                 'sharedRepository respected after v2 compact');
102 }
103 is(((stat("$tmpdir/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
104         'sharedRepository respected for v2 msgmap');
105
106 @xdir = (glob("$tmpdir/v2/git/*.git/objects/*/*"),
107          glob("$tmpdir/v2/git/*.git/objects/pack/*"));
108 foreach (@xdir) {
109         my @st = stat($_);
110         is($st[2] & 07777, -f _ ? 0444 : 0755,
111                 'sharedRepository respected after v2 compact');
112 }
113 my $msgs = $ibx->recent({limit => 1000});
114 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
115 is(scalar @$msgs, 1, 'only one message in history');
116
117 $ibx = undef;
118 $err = '';
119 $cmd = [ qw(-index --reindex -c), "$tmpdir/v2" ];
120 ok(run_script($cmd, undef, $rdr), '--reindex -c');
121 like($err, qr/xapian-compact/, 'xapian-compact ran (-c)');
122
123 $rdr->{2} = \(my $err2 = '');
124 $cmd = [ qw(-index --reindex -cc), "$tmpdir/v2" ];
125 ok(run_script($cmd, undef, $rdr), '--reindex -c -c');
126 like($err2, qr/xapian-compact/, 'xapian-compact ran (-c -c)');
127 ok(($err2 =~ tr/\n/\n/) > ($err =~ tr/\n/\n/), '-compacted twice');
128
129 done_testing();