1 # Copyright (C) 2014-2021 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;
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');
51 my @init_args = ('i', "$tmpdir/i",
52 qw(http://example.com/i i@example.com));
53 $cmd = [ qw(-init -c .bogus=val), @init_args ];
54 quiet_fail($cmd, 'invalid -c KEY=VALUE fails');
55 $cmd = [ qw(-init -c .bogus=val), @init_args ];
56 quiet_fail($cmd, '-c KEY-only fails');
57 $cmd = [ qw(-init -c address=clist@example.com), @init_args ];
58 quiet_fail($cmd, '-c address=CONFLICTING-VALUE fails');
60 $cmd = [ qw(-init -c no=problem -c no=problemo), @init_args ];
61 ok(run_script($cmd), '-c KEY=VALUE runs');
62 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
63 chomp(my @v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
64 is_deeply(\@v, [ qw(problem problemo) ]) or xbail(\@v);
66 ok(run_script($cmd), '-c KEY=VALUE runs idempotently');
67 chomp(my @v2 = xqx([qw(git config --get-all publicinbox.i.no)], $env));
68 is_deeply(\@v, \@v2, 'nothing repeated') or xbail(\@v2);
70 ok(run_script([@$cmd, '-c', 'no=more']), '-c KEY=VALUE addendum');
71 chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
72 is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);
75 ok(run_script([@$cmd, '-c', 'no=problem']), '-c KEY=VALUE repeated');
76 chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
77 is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);
79 ok(run_script([@$cmd, '-c', 'address=j@example.com']),
80 '-c KEY=VALUE address');
81 chomp(@v = xqx([qw(git config --get-all publicinbox.i.address)], $env));
82 is_deeply(\@v, [ qw(i@example.com j@example.com) ],
83 'extra address added via -c KEY=VALUE');
86 my $env = { PI_DIR => "$tmpdir/.public-inbox/" };
87 my $rdr = { 2 => \(my $err = '') };
88 my $cmd = [ '-init', 'alist', "$tmpdir/a\nlist",
89 qw(http://example.com/alist alist@example.com) ];
90 ok(!run_script($cmd, $env, $rdr),
91 'public-inbox-init rejects LF in inboxdir');
92 like($err, qr/`\\n' not allowed in `/s, 'reported \\n');
93 is_deeply([glob("$tmpdir/.public-inbox/pi-init-*")], [],
94 'no junk files left behind');
96 # "git init" does this, too
97 $cmd = [ '-init', 'deep-non-existent', "$tmpdir/a/b/c/d",
98 qw(http://example.com/abcd abcd@example.com) ];
100 ok(run_script($cmd, $env, $rdr), 'initializes non-existent hierarchy');
101 ok(-d "$tmpdir/a/b/c/d", 'directory created');
102 open my $fh, '>', "$tmpdir/d" or BAIL_OUT "open: $!";
104 $cmd = [ '-init', 'd-f-conflict', "$tmpdir/d/f/conflict",
105 qw(http://example.com/conflict onflict@example.com) ];
106 ok(!run_script($cmd, $env, $rdr), 'fails on D/F conflict');
110 require_mods(qw(DBD::SQLite Search::Xapian), 2);
111 require_git(2.6, 1) or skip "git 2.6+ required", 2;
112 use_ok 'PublicInbox::Msgmap';
113 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
114 local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
115 my $cfgfile = "$ENV{PI_DIR}/config";
116 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
117 qw(http://example.com/v2list v2list@example.com) ];
118 ok(run_script($cmd), 'public-inbox-init -V2 OK');
119 ok(-d "$tmpdir/v2list", 'v2list directory exists');
120 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
121 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
122 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
123 qw(http://example.com/v2list v2list@example.com) ];
124 ok(run_script($cmd), 'public-inbox-init is idempotent');
125 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
126 'idempotent invocation w/o -V2 does not make inbox v1');
127 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
129 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
130 qw(http://example.com/v2list v2list@example.com) ];
131 quiet_fail($cmd, 'initializing V2 as V1 fails');
133 foreach my $lvl (qw(medium basic)) {
134 my $dir = "$tmpdir/v2$lvl";
135 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
136 $dir, "http://example.com/v2$lvl",
137 "v2$lvl\@example.com" ];
138 ok(run_script($cmd), "-init -L $lvl");
139 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
140 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
141 is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
142 'detected expected level w/o config');
143 ok(!$ibx->{-skip_docdata}, 'docdata written by default');
146 my $name = "v$v-skip-docdata";
147 my $dir = "$tmpdir/$name";
148 $cmd = [ '-init', $name, "-V$v", '--skip-docdata',
149 $dir, "http://example.com/$name",
150 "$name\@example.com" ];
151 ok(run_script($cmd), "-init -V$v --skip-docdata");
152 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
153 is(PublicInbox::Admin::detect_indexlevel($ibx), 'full',
154 "detected default indexlevel -V$v");
155 ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
156 ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');
159 # loop for idempotency
161 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
162 qw(http://example.com/skip1 skip1@example.com) ];
163 ok(run_script($cmd), "--skip-epoch 1");
164 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
165 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
168 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
169 qw(http://example.com/skip2 skip2@example.com) ];
170 ok(run_script($cmd), "--skip-epoch 2");
171 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
172 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
174 xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
175 'publicinboxmda.spamcheck', 'none') == 0 or
176 BAIL_OUT "git config $?";
177 my $addr = 'skip3@example.com';
178 $cmd = [ qw(-init -V2 -Lbasic --skip-artnum=12 skip3), "$tmpdir/skip3",
179 qw(http://example.com/skip3), $addr ];
180 ok(run_script($cmd), '--skip-artnum -V2');
181 my $env = { ORIGINAL_RECIPIENT => $addr };
182 my $mid = 'skip-artnum@example.com';
183 my $msg = "Message-ID: <$mid>\n\n";
184 my $rdr = { 0 => \$msg, 2 => \(my $err = '') };
185 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
186 diag "err=$err" if $err;
187 my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
188 my $n = $mm->num_for($mid);
189 is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');
191 $addr = 'skip4@example.com';
192 $env = { ORIGINAL_RECIPIENT => $addr };
193 $cmd = [ qw(-init -V1 --skip-artnum 12 -Lmedium skip4), "$tmpdir/skip4",
194 qw(http://example.com/skip4), $addr ];
195 ok(run_script($cmd), '--skip-artnum -V1');
197 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
198 diag "err=$err" if $err;
199 $mm = PublicInbox::Msgmap->new("$tmpdir/skip4");
200 $n = $mm->num_for($mid);
201 is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
206 sub read_indexlevel {
208 my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
209 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
210 chomp(my $lvl = xqx($cmd, $env));