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 my $umask = umask(022) // xbail "umask: $!";
101 ok(run_script($cmd, $env, $rdr), 'initializes non-existent hierarchy');
102 umask($umask) // xbail "umask: $!";
103 ok(-d "$tmpdir/a/b/c/d", 'directory created');
104 my $desc = "$tmpdir/a/b/c/d/description";
105 is(PublicInbox::Git::try_cat($desc),
106 "public inbox for abcd\@example.com\n", 'description set');
107 my $mode = (stat($desc))[2];
108 is(sprintf('0%03o', $mode & 0777), '0644',
109 'description respects umask');
111 open my $fh, '>', "$tmpdir/d" or BAIL_OUT "open: $!";
113 $cmd = [ '-init', 'd-f-conflict', "$tmpdir/d/f/conflict",
114 qw(http://example.com/conflict onflict@example.com) ];
115 ok(!run_script($cmd, $env, $rdr), 'fails on D/F conflict');
119 require_mods(qw(DBD::SQLite Search::Xapian), 2);
120 require_git(2.6, 1) or skip "git 2.6+ required", 2;
121 use_ok 'PublicInbox::Msgmap';
122 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
123 local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
124 my $cfgfile = "$ENV{PI_DIR}/config";
125 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
126 qw(http://example.com/v2list v2list@example.com) ];
127 ok(run_script($cmd), 'public-inbox-init -V2 OK');
128 ok(-d "$tmpdir/v2list", 'v2list directory exists');
129 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
130 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
131 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
132 qw(http://example.com/v2list v2list@example.com) ];
133 ok(run_script($cmd), 'public-inbox-init is idempotent');
134 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
135 'idempotent invocation w/o -V2 does not make inbox v1');
136 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
138 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
139 qw(http://example.com/v2list v2list@example.com) ];
140 quiet_fail($cmd, 'initializing V2 as V1 fails');
142 foreach my $lvl (qw(medium basic)) {
143 my $dir = "$tmpdir/v2$lvl";
144 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
145 $dir, "http://example.com/v2$lvl",
146 "v2$lvl\@example.com" ];
147 ok(run_script($cmd), "-init -L $lvl");
148 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
149 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
150 is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
151 'detected expected level w/o config');
152 ok(!$ibx->{-skip_docdata}, 'docdata written by default');
155 my $name = "v$v-skip-docdata";
156 my $dir = "$tmpdir/$name";
157 $cmd = [ '-init', $name, "-V$v", '--skip-docdata',
158 $dir, "http://example.com/$name",
159 "$name\@example.com" ];
160 ok(run_script($cmd), "-init -V$v --skip-docdata");
161 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
162 is(PublicInbox::Admin::detect_indexlevel($ibx), 'full',
163 "detected default indexlevel -V$v");
164 ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
165 ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');
168 # loop for idempotency
170 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
171 qw(http://example.com/skip1 skip1@example.com) ];
172 ok(run_script($cmd), "--skip-epoch 1");
173 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
174 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
177 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
178 qw(http://example.com/skip2 skip2@example.com) ];
179 ok(run_script($cmd), "--skip-epoch 2");
180 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
181 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
183 xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
184 'publicinboxmda.spamcheck', 'none') == 0 or
185 BAIL_OUT "git config $?";
186 my $addr = 'skip3@example.com';
187 $cmd = [ qw(-init -V2 -Lbasic --skip-artnum=12 skip3), "$tmpdir/skip3",
188 qw(http://example.com/skip3), $addr ];
189 ok(run_script($cmd), '--skip-artnum -V2');
190 my $env = { ORIGINAL_RECIPIENT => $addr };
191 my $mid = 'skip-artnum@example.com';
192 my $msg = "Message-ID: <$mid>\n\n";
193 my $rdr = { 0 => \$msg, 2 => \(my $err = '') };
194 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
195 diag "err=$err" if $err;
196 my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
197 my $n = $mm->num_for($mid);
198 is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');
200 $addr = 'skip4@example.com';
201 $env = { ORIGINAL_RECIPIENT => $addr };
202 $cmd = [ qw(-init -V1 --skip-artnum 12 -Lmedium skip4), "$tmpdir/skip4",
203 qw(http://example.com/skip4), $addr ];
204 ok(run_script($cmd), '--skip-artnum -V1');
206 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
207 diag "err=$err" if $err;
208 $mm = PublicInbox::Msgmap->new_file(
209 "$tmpdir/skip4/public-inbox/msgmap.sqlite3");
210 $n = $mm->num_for($mid);
211 is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
216 sub read_indexlevel {
218 my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
219 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
220 chomp(my $lvl = xqx($cmd, $env));