1 # Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::Config;
7 use PublicInbox::TestCommon;
9 my ($tmpdir, $for_destroy) = tmpdir();
13 ok(!run_script($cmd, undef, { 2 => \$err, 1 => \$err }), $msg);
17 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
18 my $cfgfile = "$ENV{PI_DIR}/config";
19 my $cmd = [ '-init', 'blist', "$tmpdir/blist",
20 qw(http://example.com/blist blist@example.com) ];
21 ok(run_script($cmd), 'public-inbox-init OK');
23 is(read_indexlevel('blist'), '', 'indexlevel unset by default');
25 ok(-e $cfgfile, "config exists, now");
26 ok(run_script($cmd), 'public-inbox-init OK (idempotent)');
28 chmod 0666, $cfgfile or die "chmod failed: $!";
29 $cmd = [ '-init', 'clist', "$tmpdir/clist",
30 qw(http://example.com/clist clist@example.com)];
31 ok(run_script($cmd), 'public-inbox-init clist OK');
32 is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");
34 $cmd = [ '-init', 'clist', '-V2', "$tmpdir/clist",
35 qw(http://example.com/clist clist@example.com) ];
36 quiet_fail($cmd, 'attempting to init V2 from V1 fails');
37 ok(!-e "$cfgfile.lock", 'no lock leftover after init');
39 open my $lock, '+>', "$cfgfile.lock" or die;
40 $cmd = [ '-init', 'lock', "$tmpdir/lock",
41 qw(http://example.com/lock lock@example.com) ];
42 ok(-e "$cfgfile.lock", 'lock exists');
46 ok(!run_script($cmd, undef, {2 => \$err}), 'lock init failed');
47 is($? >> 8, 255, 'got expected exit code on lock failure');
48 ok(unlink("$cfgfile.lock"),
49 '-init did not unlink lock on failure');
53 require_mods(qw(DBD::SQLite Search::Xapian::WritableDatabase), 2);
54 require_git(2.6, 1) or skip "git 2.6+ required", 2;
55 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
56 my $cfgfile = "$ENV{PI_DIR}/config";
57 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
58 qw(http://example.com/v2list v2list@example.com) ];
59 ok(run_script($cmd), 'public-inbox-init -V2 OK');
60 ok(-d "$tmpdir/v2list", 'v2list directory exists');
61 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
62 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
63 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
64 qw(http://example.com/v2list v2list@example.com) ];
65 ok(run_script($cmd), 'public-inbox-init is idempotent');
66 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
67 'idempotent invocation w/o -V2 does not make inbox v1');
68 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
70 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
71 qw(http://example.com/v2list v2list@example.com) ];
72 quiet_fail($cmd, 'initializing V2 as V1 fails');
74 foreach my $lvl (qw(medium basic)) {
75 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
76 "$tmpdir/v2$lvl", "http://example.com/v2$lvl",
77 "v2$lvl\@example.com" ];
78 ok(run_script($cmd), "-init -L $lvl");
79 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
82 # loop for idempotency
84 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
85 qw(http://example.com/skip1 skip1@example.com) ];
86 ok(run_script($cmd), "--skip-epoch 1");
87 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
88 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
92 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
93 qw(http://example.com/skip2 skip2@example.com) ];
94 ok(run_script($cmd), "--skip-epoch 2");
95 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
96 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
101 sub read_indexlevel {
103 local $ENV{GIT_CONFIG} = "$ENV{PI_DIR}/config";
104 chomp(my $lvl = `git config publicinbox.$inbox.indexlevel`);