1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use File::Temp qw/tempdir/;
8 my @mods = qw(DBD::SQLite Search::Xapian);
9 foreach my $mod (@mods) {
11 plan skip_all => "$mod missing for convert-compact.t" if $@;
13 use_ok 'PublicInbox::V2Writable';
14 use PublicInbox::Import;
15 my $tmpdir = tempdir('convert-compact-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 mainrepo => "$tmpdir/v1",
19 -primary_address => 'test@example.com',
22 ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{mainrepo}]),
23 'initialized v1 repo');
24 ok(umask(077), 'set restrictive umask');
25 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{mainrepo}",
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->create(
31 From => 'a@example.com',
32 To => 'test@example.com',
33 Subject => 'this is a subject',
34 'Message-ID' => '<a-mid@b>',
35 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
37 body => "hello world\n",
39 ok($im->add($mime), 'added one message');
40 ok($im->remove($mime), 'remove message');
41 ok($im->add($mime), 'added message again');
43 PublicInbox::SearchIdx->new($ibx, 1)->index_sync;
45 is(((stat("$ibx->{mainrepo}/public-inbox"))[2]) & 07777, 0755,
46 'sharedRepository respected for v1');
47 is(((stat("$ibx->{mainrepo}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
48 'sharedRepository respected for v1 msgmap');
49 my @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*/*");
52 is($st[2] & 07777, -f _ ? 0644 : 0755,
53 'sharedRepository respected on file after convert');
56 local $ENV{PATH} = "blib/script:$ENV{PATH}";
57 open my $err, '>>', "$tmpdir/err.log" or die "open: err.log $!\n";
58 open my $out, '>>', "$tmpdir/out.log" or die "open: out.log $!\n";
59 my $rdr = { 1 => fileno($out), 2 => fileno($err) };
61 my $cmd = [ 'public-inbox-compact', $ibx->{mainrepo} ];
62 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 compact works');
64 @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*");
65 is(scalar(@xdir), 1, 'got one xapian directory after compact');
66 is(((stat($xdir[0]))[2]) & 07777, 0755,
67 'sharedRepository respected on v1 compact');
69 $cmd = [ 'public-inbox-convert', $ibx->{mainrepo}, "$tmpdir/v2" ];
70 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'convert works');
71 @xdir = glob("$tmpdir/v2/xap*/*");
74 is($st[2] & 07777, -f _ ? 0644 : 0755,
75 'sharedRepository respected after convert');
78 $cmd = [ 'public-inbox-compact', "$tmpdir/v2" ];
79 my $env = { NPROC => 2 };
80 ok(PublicInbox::Import::run_die($cmd, $env, $rdr), 'v2 compact works');
81 $ibx->{mainrepo} = "$tmpdir/v2";
84 @xdir = glob("$tmpdir/v2/xap*/*");
87 is($st[2] & 07777, -f _ ? 0644 : 0755,
88 'sharedRepository respected after v2 compact');
90 is(((stat("$tmpdir/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
91 'sharedRepository respected for v2 msgmap');
93 @xdir = (glob("$tmpdir/v2/git/*.git/objects/*/*"),
94 glob("$tmpdir/v2/git/*.git/objects/pack/*"));
97 is($st[2] & 07777, -f _ ? 0444 : 0755,
98 'sharedRepository respected after v2 compact');
100 my $msgs = $ibx->recent({limit => 1000});
101 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
102 is(scalar @$msgs, 1, 'only one message in history');