]> Sergey Matveev's repositories - public-inbox.git/blob - t/convert-compact.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / convert-compact.t
1 # Copyright (C) 2018-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 File::Temp qw/tempdir/;
7 use PublicInbox::MIME;
8 use PublicInbox::Spawn qw(which);
9 require './t/common.perl';
10 require_git(2.6);
11 my @mods = qw(DBD::SQLite Search::Xapian);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for convert-compact.t" if $@;
15 }
16 which('xapian-compact') or
17         plan skip_all => 'xapian-compact missing for '.__FILE__;
18
19 use_ok 'PublicInbox::V2Writable';
20 use PublicInbox::Import;
21 my $tmpdir = tempdir('convert-compact-XXXXXX', TMPDIR => 1, CLEANUP => 1);
22 my $ibx = {
23         mainrepo => "$tmpdir/v1",
24         name => 'test-v1',
25         -primary_address => 'test@example.com',
26 };
27
28 ok(PublicInbox::Import::run_die([qw(git init --bare -q), $ibx->{mainrepo}]),
29         'initialized v1 repo');
30 ok(umask(077), 'set restrictive umask');
31 ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{mainrepo}",
32         qw(config core.sharedRepository 0644)]), 'set sharedRepository');
33 $ibx = PublicInbox::Inbox->new($ibx);
34 my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx);
35 my $mime = PublicInbox::MIME->create(
36         header => [
37                 From => 'a@example.com',
38                 To => 'test@example.com',
39                 Subject => 'this is a subject',
40                 'Message-ID' => '<a-mid@b>',
41                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
42         ],
43         body => "hello world\n",
44 );
45 ok($im->add($mime), 'added one message');
46 ok($im->remove($mime), 'remove message');
47 ok($im->add($mime), 'added message again');
48 $im->done;
49 for (1..2) {
50         eval { PublicInbox::SearchIdx->new($ibx, 1)->index_sync; };
51         is($@, '', 'no errors syncing');
52 }
53
54 is(((stat("$ibx->{mainrepo}/public-inbox"))[2]) & 07777, 0755,
55         'sharedRepository respected for v1');
56 is(((stat("$ibx->{mainrepo}/public-inbox/msgmap.sqlite3"))[2]) & 07777, 0644,
57         'sharedRepository respected for v1 msgmap');
58 my @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*/*");
59 foreach (@xdir) {
60         my @st = stat($_);
61         is($st[2] & 07777, -f _ ? 0644 : 0755,
62                 'sharedRepository respected on file after convert');
63 }
64
65 local $ENV{PATH} = "blib/script:$ENV{PATH}";
66 local $ENV{PI_CONFIG} = '/dev/null';
67 open my $err, '>>', "$tmpdir/err.log" or die "open: err.log $!\n";
68 open my $out, '>>', "$tmpdir/out.log" or die "open: out.log $!\n";
69 my $rdr = { 1 => fileno($out), 2 => fileno($err) };
70
71 my $cmd = [ 'public-inbox-compact', $ibx->{mainrepo} ];
72 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'v1 compact works');
73
74 @xdir = glob("$ibx->{mainrepo}/public-inbox/xap*");
75 is(scalar(@xdir), 1, 'got one xapian directory after compact');
76 is(((stat($xdir[0]))[2]) & 07777, 0755,
77         'sharedRepository respected on v1 compact');
78
79 $cmd = [ 'public-inbox-convert', $ibx->{mainrepo}, "$tmpdir/v2" ];
80 ok(PublicInbox::Import::run_die($cmd, undef, $rdr), 'convert works');
81 @xdir = glob("$tmpdir/v2/xap*/*");
82 foreach (@xdir) {
83         my @st = stat($_);
84         is($st[2] & 07777, -f _ ? 0644 : 0755,
85                 'sharedRepository respected after convert');
86 }
87
88 $cmd = [ 'public-inbox-compact', "$tmpdir/v2" ];
89 my $env = { NPROC => 2 };
90 ok(PublicInbox::Import::run_die($cmd, $env, $rdr), 'v2 compact works');
91 $ibx->{mainrepo} = "$tmpdir/v2";
92 $ibx->{version} = 2;
93
94 @xdir = glob("$tmpdir/v2/xap*/*");
95 foreach (@xdir) {
96         my @st = stat($_);
97         is($st[2] & 07777, -f _ ? 0644 : 0755,
98                 'sharedRepository respected after v2 compact');
99 }
100 is(((stat("$tmpdir/v2/msgmap.sqlite3"))[2]) & 07777, 0644,
101         'sharedRepository respected for v2 msgmap');
102
103 @xdir = (glob("$tmpdir/v2/git/*.git/objects/*/*"),
104          glob("$tmpdir/v2/git/*.git/objects/pack/*"));
105 foreach (@xdir) {
106         my @st = stat($_);
107         is($st[2] & 07777, -f _ ? 0444 : 0755,
108                 'sharedRepository respected after v2 compact');
109 }
110 my $msgs = $ibx->recent({limit => 1000});
111 is($msgs->[0]->{mid}, 'a-mid@b', 'message exists in history');
112 is(scalar @$msgs, 1, 'only one message in history');
113
114 done_testing();