2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # 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 use PublicInbox::InboxWritable;
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 my $umask = umask(070) // xbail "umask: $!";
23 ok(run_script($cmd), 'public-inbox-init OK');
24 umask($umask) // xbail "umask: $!";
25 my $mode = (stat($cfgfile))[2];
26 is(sprintf('0%03o', $mode & 0777), '0604', 'config respects umask');
28 is(read_indexlevel('blist'), '', 'indexlevel unset by default');
30 ok(-e $cfgfile, "config exists, now");
31 ok(run_script($cmd), 'public-inbox-init OK (idempotent)');
33 chmod 0666, $cfgfile or die "chmod failed: $!";
34 $cmd = [ '-init', 'clist', "$tmpdir/clist",
35 qw(http://example.com/clist clist@example.com)];
36 ok(run_script($cmd), 'public-inbox-init clist OK');
37 is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");
39 $cmd = [ '-init', 'clist', '-V2', "$tmpdir/clist",
40 qw(http://example.com/clist clist@example.com) ];
41 quiet_fail($cmd, 'attempting to init V2 from V1 fails');
42 ok(!-e "$cfgfile.lock", 'no lock leftover after init');
44 open my $lock, '+>', "$cfgfile.lock" or die;
45 $cmd = [ '-init', 'lock', "$tmpdir/lock",
46 qw(http://example.com/lock lock@example.com) ];
47 ok(-e "$cfgfile.lock", 'lock exists');
51 ok(!run_script($cmd, undef, {2 => \$err}), 'lock init failed');
52 is($? >> 8, 255, 'got expected exit code on lock failure');
53 ok(unlink("$cfgfile.lock"),
54 '-init did not unlink lock on failure');
56 my @init_args = ('i', "$tmpdir/i",
57 qw(http://example.com/i i@example.com));
58 $cmd = [ qw(-init -c .bogus=val), @init_args ];
59 quiet_fail($cmd, 'invalid -c KEY=VALUE fails');
60 $cmd = [ qw(-init -c .bogus=val), @init_args ];
61 quiet_fail($cmd, '-c KEY-only fails');
62 $cmd = [ qw(-init -c address=clist@example.com), @init_args ];
63 quiet_fail($cmd, '-c address=CONFLICTING-VALUE fails');
65 $cmd = [ qw(-init -c no=problem -c no=problemo), @init_args ];
66 ok(run_script($cmd), '-c KEY=VALUE runs');
67 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
68 chomp(my @v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
69 is_deeply(\@v, [ qw(problem problemo) ]) or xbail(\@v);
71 ok(run_script($cmd), '-c KEY=VALUE runs idempotently');
72 chomp(my @v2 = xqx([qw(git config --get-all publicinbox.i.no)], $env));
73 is_deeply(\@v, \@v2, 'nothing repeated') or xbail(\@v2);
75 ok(run_script([@$cmd, '-c', 'no=more']), '-c KEY=VALUE addendum');
76 chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
77 is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);
80 ok(run_script([@$cmd, '-c', 'no=problem']), '-c KEY=VALUE repeated');
81 chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
82 is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);
84 ok(run_script([@$cmd, '-c', 'address=j@example.com']),
85 '-c KEY=VALUE address');
86 chomp(@v = xqx([qw(git config --get-all publicinbox.i.address)], $env));
87 is_deeply(\@v, [ qw(i@example.com j@example.com) ],
88 'extra address added via -c KEY=VALUE');
91 my $env = { PI_DIR => "$tmpdir/.public-inbox/" };
92 my $rdr = { 2 => \(my $err = '') };
93 my $cmd = [ '-init', 'alist', "$tmpdir/a\nlist",
94 qw(http://example.com/alist alist@example.com) ];
95 ok(!run_script($cmd, $env, $rdr),
96 'public-inbox-init rejects LF in inboxdir');
97 like($err, qr/`\\n' not allowed in `/s, 'reported \\n');
98 is_deeply([glob("$tmpdir/.public-inbox/pi-init-*")], [],
99 'no junk files left behind');
101 # "git init" does this, too
102 $cmd = [ '-init', 'deep-non-existent', "$tmpdir/a/b/c/d",
103 qw(http://example.com/abcd abcd@example.com) ];
105 my $umask = umask(022) // xbail "umask: $!";
106 ok(run_script($cmd, $env, $rdr), 'initializes non-existent hierarchy');
107 umask($umask) // xbail "umask: $!";
108 ok(-d "$tmpdir/a/b/c/d", 'directory created');
109 my $desc = "$tmpdir/a/b/c/d/description";
110 is(PublicInbox::IO::try_cat($desc),
111 "public inbox for abcd\@example.com\n", 'description set');
112 my $mode = (stat($desc))[2];
113 is(sprintf('0%03o', $mode & 0777), '0644',
114 'description respects umask');
116 open my $fh, '>', "$tmpdir/d" or BAIL_OUT "open: $!";
118 $cmd = [ '-init', 'd-f-conflict', "$tmpdir/d/f/conflict",
119 qw(http://example.com/conflict onflict@example.com) ];
120 ok(!run_script($cmd, $env, $rdr), 'fails on D/F conflict');
124 require_mods(qw(DBD::SQLite Xapian), 2);
126 use_ok 'PublicInbox::Msgmap';
127 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
128 local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
129 my $cfgfile = "$ENV{PI_DIR}/config";
130 my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
131 qw(http://example.com/v2list v2list@example.com) ];
132 ok(run_script($cmd), 'public-inbox-init -V2 OK');
133 ok(-d "$tmpdir/v2list", 'v2list directory exists');
134 ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
135 ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
136 $cmd = [ '-init', 'v2list', "$tmpdir/v2list",
137 qw(http://example.com/v2list v2list@example.com) ];
138 ok(run_script($cmd), 'public-inbox-init is idempotent');
139 ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
140 'idempotent invocation w/o -V2 does not make inbox v1');
141 is(read_indexlevel('v2list'), '', 'indexlevel unset by default');
143 $cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
144 qw(http://example.com/v2list v2list@example.com) ];
145 quiet_fail($cmd, 'initializing V2 as V1 fails');
147 foreach my $lvl (qw(medium basic)) {
148 my $dir = "$tmpdir/v2$lvl";
149 $cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
150 $dir, "http://example.com/v2$lvl",
151 "v2$lvl\@example.com" ];
152 ok(run_script($cmd), "-init -L $lvl");
153 is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
154 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
155 is(PublicInbox::InboxWritable::detect_indexlevel($ibx), $lvl,
156 'detected expected level w/o config');
157 ok(!$ibx->{-skip_docdata}, 'docdata written by default');
160 my $name = "v$v-skip-docdata";
161 my $dir = "$tmpdir/$name";
162 $cmd = [ '-init', $name, "-V$v", '--skip-docdata',
163 $dir, "http://example.com/$name",
164 "$name\@example.com" ];
165 ok(run_script($cmd), "-init -V$v --skip-docdata");
166 my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
167 is(PublicInbox::InboxWritable::detect_indexlevel($ibx), 'full',
168 "detected default indexlevel -V$v");
169 ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
170 ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');
173 # loop for idempotency
175 $cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
176 qw(http://example.com/skip1 skip1@example.com) ];
177 ok(run_script($cmd), "--skip-epoch 1");
178 my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
179 is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
182 $cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
183 qw(http://example.com/skip2 skip2@example.com) ];
184 ok(run_script($cmd), "--skip-epoch 2");
185 my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
186 is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');
188 xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
189 'publicinboxmda.spamcheck', 'none') == 0 or
190 BAIL_OUT "git config $?";
191 my $addr = 'skip3@example.com';
192 $cmd = [ qw(-init -V2 -Lbasic --skip-artnum=12 skip3), "$tmpdir/skip3",
193 qw(http://example.com/skip3), $addr ];
194 ok(run_script($cmd), '--skip-artnum -V2');
195 my $env = { ORIGINAL_RECIPIENT => $addr };
196 my $mid = 'skip-artnum@example.com';
197 my $msg = "Message-ID: <$mid>\n\n";
198 my $rdr = { 0 => \$msg, 2 => \(my $err = '') };
199 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
200 diag "err=$err" if $err;
201 my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
202 my $n = $mm->num_for($mid);
203 is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');
205 $addr = 'skip4@example.com';
206 $env = { ORIGINAL_RECIPIENT => $addr };
207 $cmd = [ qw(-init -V1 --skip-artnum 12 -Lmedium skip4), "$tmpdir/skip4",
208 qw(http://example.com/skip4), $addr ];
209 ok(run_script($cmd), '--skip-artnum -V1');
211 ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
212 diag "err=$err" if $err;
213 $mm = PublicInbox::Msgmap->new_file(
214 "$tmpdir/skip4/public-inbox/msgmap.sqlite3");
215 $n = $mm->num_for($mid);
216 is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
220 local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
221 my $cmd = [ qw(-init -C), "$tmpdir", qw(chdirlist chdirlist),
222 qw(http://example.com/chdirlist chdirlist@example.com)];
223 ok(run_script($cmd), '-init with -C (chdir)');
224 ok(-d "$tmpdir/chdirlist", '-C processed correctly');
229 sub read_indexlevel {
231 my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
232 my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
233 chomp(my $lvl = xqx($cmd, $env));