1 # Copyright (C) 2014-2020 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;
8 use PublicInbox::Admin;
10 my ($tmpdir, $for_destroy) = tmpdir();
14 ok(!run_script($cmd, undef, { 2 => \$err, 1 => \$err }), $msg);
18 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
19 my $cfgfile = "$ENV{PI_DIR}/config";
20 my $cmd = [ '-init', 'blist', "$tmpdir/blist",
21 qw(http://example.com/blist blist@example.com) ];
22 ok(run_script($cmd), 'public-inbox-init OK');
24 is(read_indexlevel('blist'), '', 'indexlevel unset by default');
26 ok(-e $cfgfile, "config exists, now");
27 ok(run_script($cmd), 'public-inbox-init OK (idempotent)');
29 chmod 0666, $cfgfile or die "chmod failed: $!";
30 $cmd = [ '-init', 'clist', "$tmpdir/clist",
31 qw(http://example.com/clist clist@example.com)];
32 ok(run_script($cmd), 'public-inbox-init clist OK');
33 is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");
35 $cmd = [ '-init', 'clist', '-V2', "$tmpdir/clist",
36 qw(http://example.com/clist clist@example.com) ];
37 quiet_fail($cmd, 'attempting to init V2 from V1 fails');
38 ok(!-e "$cfgfile.lock", 'no lock leftover after init');
40 open my $lock, '+>', "$cfgfile.lock" or die;
41 $cmd = [ '-init', 'lock', "$tmpdir/lock",
42 qw(http://example.com/lock lock@example.com) ];
43 ok(-e "$cfgfile.lock", 'lock exists');
47 ok(!run_script($cmd, undef, {2 => \$err}), 'lock init failed');
48 is($? >> 8, 255, 'got expected exit code on lock failure');
49 ok(unlink("$cfgfile.lock"),
50 '-init did not unlink lock on failure');
54 require_mods(qw(DBD::SQLite Search::Xapian::WritableDatabase), 2);
55 require_git(2.6, 1) or skip "git 2.6+ required", 2;
56 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
57 my $cfgfile = "$ENV{PI_DIR}/config";
58 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
59 qw(http://example.com/v2list v2list@example.com) ];
60 ok(run_script($cmd), 'public-inbox-init -V2 OK');
61 ok(-d "$tmpdir/v2list", 'v2list directory exists');
62 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
63 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
64 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
65 qw(http://example.com/v2list v2list@example.com) ];
66 ok(run_script($cmd), 'public-inbox-init is idempotent');
67 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
68 'idempotent invocation w/o -V2 does not make inbox v1');
69 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
71 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
72 qw(http://example.com/v2list v2list@example.com) ];
73 quiet_fail($cmd, 'initializing V2 as V1 fails');
75 foreach my $lvl (qw(medium basic)) {
76 my $dir = "$tmpdir/v2$lvl";
77 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
78 $dir, "http://example.com/v2$lvl",
79 "v2$lvl\@example.com" ];
80 ok(run_script($cmd), "-init -L $lvl");
81 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
82 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
83 is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
84 'detected expected level w/o config');
87 # loop for idempotency
89 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
90 qw(http://example.com/skip1 skip1@example.com) ];
91 ok(run_script($cmd), "--skip-epoch 1");
92 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
93 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
97 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
98 qw(http://example.com/skip2 skip2@example.com) ];
99 ok(run_script($cmd), "--skip-epoch 2");
100 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
101 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
106 sub read_indexlevel {
108 local $ENV{GIT_CONFIG} = "$ENV{PI_DIR}/config";
109 chomp(my $lvl = `git config publicinbox.$inbox.indexlevel`);