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 require './t/common.perl';
10 my @mods = qw(DBD::SQLite Search::Xapian);
11 foreach my $mod (@mods) {
13 plan skip_all => "$mod missing for convert-compact.t" if $@;
15 use_ok 'PublicInbox::V2Writable';
16 use PublicInbox::Import;
17 my $tmpdir = tempdir('convert-compact-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 mainrepo => "$tmpdir/v1",
21 -primary_address => 'test@example.com',
24 ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{mainrepo}]),
25 'initialized v1 repo');
26 ok(umask(077), 'set restrictive umask');
27 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{mainrepo}",
28 qw(config core.sharedRepository 0644)]), 'set sharedRepository');
29 $ibx = PublicInbox::Inbox->new($ibx);
30 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
31 my $mime = PublicInbox::MIME->create(
33 From => 'a@example.com',
34 To => 'test@example.com',
35 Subject => 'this is a subject',
36 'Message-ID' => '<a-mid@b>',
37 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
39 body => "hello world\n",
41 ok($im->add($mime), 'added one message');
42 ok($im->remove($mime), 'remove message');
43 ok($im->add($mime), 'added message again');
46 eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
47 is($@, '', 'no errors syncing');
50 is(((stat("$ibx->{mainrepo}/public-inbox"))[2]) & 07777, 0755,
51 'sharedRepository respected for v1');
52 is(((stat("$ibx->{mainrepo}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
53 'sharedRepository respected for v1 msgmap');
54 my @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*/*");
57 is($st[2] & 07777, -f _ ? 0644 : 0755,
58 'sharedRepository respected on file after convert');
61 local $ENV{PATH} = "blib/script:$ENV{PATH}";
62 local $ENV{PI_CONFIG} = '/dev/null';
63 open my $err, '>>', "$tmpdir/err.log" or die "open: err.log $!\n";
64 open my $out, '>>', "$tmpdir/out.log" or die "open: out.log $!\n";
65 my $rdr = { 1 => fileno($out), 2 => fileno($err) };
67 my $cmd = [ 'public-inbox-compact', $ibx->{mainrepo} ];
68 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 compact works');
70 @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*");
71 is(scalar(@xdir), 1, 'got one xapian directory after compact');
72 is(((stat($xdir[0]))[2]) & 07777, 0755,
73 'sharedRepository respected on v1 compact');
75 $cmd = [ 'public-inbox-convert', $ibx->{mainrepo}, "$tmpdir/v2" ];
76 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'convert works');
77 @xdir = glob("$tmpdir/v2/xap*/*");
80 is($st[2] & 07777, -f _ ? 0644 : 0755,
81 'sharedRepository respected after convert');
84 $cmd = [ 'public-inbox-compact', "$tmpdir/v2" ];
85 my $env = { NPROC => 2 };
86 ok(PublicInbox::Import::run_die($cmd, $env, $rdr), 'v2 compact works');
87 $ibx->{mainrepo} = "$tmpdir/v2";
90 @xdir = glob("$tmpdir/v2/xap*/*");
93 is($st[2] & 07777, -f _ ? 0644 : 0755,
94 'sharedRepository respected after v2 compact');
96 is(((stat("$tmpdir/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
97 'sharedRepository respected for v2 msgmap');
99 @xdir = (glob("$tmpdir/v2/git/*.git/objects/*/*"),
100 glob("$tmpdir/v2/git/*.git/objects/pack/*"));
103 is($st[2] & 07777, -f _ ? 0444 : 0755,
104 'sharedRepository respected after v2 compact');
106 my $msgs = $ibx->recent({limit => 1000});
107 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
108 is(scalar @$msgs, 1, 'only one message in history');