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');
65 require_mods(qw(DBD::SQLite Search::Xapian::WritableDatabase), 2);
66 require_git(2.6, 1) or skip "git 2.6+ required", 2;
67 use_ok 'PublicInbox::Msgmap';
68 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
69 my $cfgfile = "$ENV{PI_DIR}/config";
70 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
71 qw(http://example.com/v2list v2list@example.com) ];
72 ok(run_script($cmd), 'public-inbox-init -V2 OK');
73 ok(-d "$tmpdir/v2list", 'v2list directory exists');
74 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
75 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
76 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
77 qw(http://example.com/v2list v2list@example.com) ];
78 ok(run_script($cmd), 'public-inbox-init is idempotent');
79 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
80 'idempotent invocation w/o -V2 does not make inbox v1');
81 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
83 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
84 qw(http://example.com/v2list v2list@example.com) ];
85 quiet_fail($cmd, 'initializing V2 as V1 fails');
87 foreach my $lvl (qw(medium basic)) {
88 my $dir = "$tmpdir/v2$lvl";
89 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
90 $dir, "http://example.com/v2$lvl",
91 "v2$lvl\@example.com" ];
92 ok(run_script($cmd), "-init -L $lvl");
93 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
94 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
95 is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
96 'detected expected level w/o config');
99 # loop for idempotency
101 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
102 qw(http://example.com/skip1 skip1@example.com) ];
103 ok(run_script($cmd), "--skip-epoch 1");
104 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
105 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
108 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
109 qw(http://example.com/skip2 skip2@example.com) ];
110 ok(run_script($cmd), "--skip-epoch 2");
111 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
112 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
114 xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
115 'publicinboxmda.spamcheck', 'none') == 0 or
116 BAIL_OUT "git config $?";
117 my $addr = 'skip3@example.com';
118 $cmd = [ qw(-init -V2 -Lbasic -N12 skip3), "$tmpdir/skip3",
119 qw(http://example.com/skip3), $addr ];
120 ok(run_script($cmd), '--skip-artnum -V2');
121 my $env = { ORIGINAL_RECIPIENT => $addr };
122 my $mid = 'skip-artnum@example.com';
123 my $msg = "Message-ID: <$mid>\n\n";
124 my $rdr = { 0 => \$msg, 2 => \(my $err = '') };
125 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
126 my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
127 my $n = $mm->num_for($mid);
128 is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');
130 $addr = 'skip4@example.com';
131 $env = { ORIGINAL_RECIPIENT => $addr };
132 $cmd = [ qw(-init -V1 -N12 -Lmedium skip4), "$tmpdir/skip4",
133 qw(http://example.com/skip4), $addr ];
134 ok(run_script($cmd), '--skip-artnum -V1');
135 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
136 $mm = PublicInbox::Msgmap->new("$tmpdir/skip4");
137 $n = $mm->num_for($mid);
138 is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
143 sub read_indexlevel {
145 my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
146 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
147 chomp(my $lvl = xqx($cmd, $env));