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');
53 my $env = { PI_DIR => "$tmpdir/.public-inbox/" };
54 my $rdr = { 2 => \(my $err = '') };
55 my $cmd = [ '-init', 'alist', "$tmpdir/a\nlist",
56 qw(http://example.com/alist alist@example.com) ];
57 ok(!run_script($cmd, $env, $rdr),
58 'public-inbox-init rejects LF in inboxdir');
59 like($err, qr/`\\n' not allowed in `/s, 'reported \\n');
60 is_deeply([glob("$tmpdir/.public-inbox/pi-init-*")], [],
61 'no junk files left behind');
63 # "git init" does this, too
64 $cmd = [ '-init', 'deep-non-existent', "$tmpdir/a/b/c/d",
65 qw(http://example.com/abcd abcd@example.com) ];
67 ok(run_script($cmd, $env, $rdr), 'initializes non-existent hierarchy');
68 ok(-d "$tmpdir/a/b/c/d", 'directory created');
69 open my $fh, '>', "$tmpdir/d" or BAIL_OUT "open: $!";
71 $cmd = [ '-init', 'd-f-conflict', "$tmpdir/d/f/conflict",
72 qw(http://example.com/conflict onflict@example.com) ];
73 ok(!run_script($cmd, $env, $rdr), 'fails on D/F conflict');
77 require_mods(qw(DBD::SQLite Search::Xapian::WritableDatabase), 2);
78 require_git(2.6, 1) or skip "git 2.6+ required", 2;
79 use_ok 'PublicInbox::Msgmap';
80 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
81 local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
82 my $cfgfile = "$ENV{PI_DIR}/config";
83 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
84 qw(http://example.com/v2list v2list@example.com) ];
85 ok(run_script($cmd), 'public-inbox-init -V2 OK');
86 ok(-d "$tmpdir/v2list", 'v2list directory exists');
87 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
88 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
89 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
90 qw(http://example.com/v2list v2list@example.com) ];
91 ok(run_script($cmd), 'public-inbox-init is idempotent');
92 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
93 'idempotent invocation w/o -V2 does not make inbox v1');
94 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
96 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
97 qw(http://example.com/v2list v2list@example.com) ];
98 quiet_fail($cmd, 'initializing V2 as V1 fails');
100 foreach my $lvl (qw(medium basic)) {
101 my $dir = "$tmpdir/v2$lvl";
102 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
103 $dir, "http://example.com/v2$lvl",
104 "v2$lvl\@example.com" ];
105 ok(run_script($cmd), "-init -L $lvl");
106 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
107 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
108 is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
109 'detected expected level w/o config');
110 ok(!$ibx->{-skip_docdata}, 'docdata written by default');
113 my $name = "v$v-skip-docdata";
114 my $dir = "$tmpdir/$name";
115 $cmd = [ '-init', $name, "-V$v", '--skip-docdata',
116 $dir, "http://example.com/$name",
117 "$name\@example.com" ];
118 ok(run_script($cmd), "-init -V$v --skip-docdata");
119 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
120 is(PublicInbox::Admin::detect_indexlevel($ibx), 'full',
121 "detected default indexlevel -V$v");
122 ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
123 ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');
126 # loop for idempotency
128 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
129 qw(http://example.com/skip1 skip1@example.com) ];
130 ok(run_script($cmd), "--skip-epoch 1");
131 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
132 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
135 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
136 qw(http://example.com/skip2 skip2@example.com) ];
137 ok(run_script($cmd), "--skip-epoch 2");
138 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
139 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
141 xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
142 'publicinboxmda.spamcheck', 'none') == 0 or
143 BAIL_OUT "git config $?";
144 my $addr = 'skip3@example.com';
145 $cmd = [ qw(-init -V2 -Lbasic --skip-artnum=12 skip3), "$tmpdir/skip3",
146 qw(http://example.com/skip3), $addr ];
147 ok(run_script($cmd), '--skip-artnum -V2');
148 my $env = { ORIGINAL_RECIPIENT => $addr };
149 my $mid = 'skip-artnum@example.com';
150 my $msg = "Message-ID: <$mid>\n\n";
151 my $rdr = { 0 => \$msg, 2 => \(my $err = '') };
152 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
153 diag "err=$err" if $err;
154 my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
155 my $n = $mm->num_for($mid);
156 is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');
158 $addr = 'skip4@example.com';
159 $env = { ORIGINAL_RECIPIENT => $addr };
160 $cmd = [ qw(-init -V1 --skip-artnum 12 -Lmedium skip4), "$tmpdir/skip4",
161 qw(http://example.com/skip4), $addr ];
162 ok(run_script($cmd), '--skip-artnum -V1');
164 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
165 diag "err=$err" if $err;
166 $mm = PublicInbox::Msgmap->new("$tmpdir/skip4");
167 $n = $mm->num_for($mid);
168 is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
173 sub read_indexlevel {
175 my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
176 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
177 chomp(my $lvl = xqx($cmd, $env));