user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
blob 752e5af93c0b60ce519cf838331494a2d5c8c472 8584 bytes (raw)
name: t/init.t 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
 
# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict;
use warnings;
use Test::More;
use PublicInbox::Config;
use PublicInbox::TestCommon;
use PublicInbox::Admin;
my ($tmpdir, $for_destroy) = tmpdir();
sub quiet_fail {
	my ($cmd, $msg) = @_;
	my $err = '';
	ok(!run_script($cmd, undef, { 2 => \$err, 1 => \$err }), $msg);
}

{
	local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
	my $cfgfile = "$ENV{PI_DIR}/config";
	my $cmd = [ '-init', 'blist', "$tmpdir/blist",
		   qw(http://example.com/blist blist@example.com) ];
	ok(run_script($cmd), 'public-inbox-init OK');

	is(read_indexlevel('blist'), '', 'indexlevel unset by default');

	ok(-e $cfgfile, "config exists, now");
	ok(run_script($cmd), 'public-inbox-init OK (idempotent)');

	chmod 0666, $cfgfile or die "chmod failed: $!";
	$cmd = [ '-init', 'clist', "$tmpdir/clist",
		   qw(http://example.com/clist clist@example.com)];
	ok(run_script($cmd), 'public-inbox-init clist OK');
	is((stat($cfgfile))[2] & 07777, 0666, "permissions preserved");

	$cmd = [ '-init', 'clist', '-V2', "$tmpdir/clist",
		   qw(http://example.com/clist clist@example.com) ];
	quiet_fail($cmd, 'attempting to init V2 from V1 fails');
	ok(!-e "$cfgfile.lock", 'no lock leftover after init');

	open my $lock, '+>', "$cfgfile.lock" or die;
	$cmd = [ '-init', 'lock', "$tmpdir/lock",
		qw(http://example.com/lock lock@example.com) ];
	ok(-e "$cfgfile.lock", 'lock exists');

	# this calls exit():
	my $err = '';
	ok(!run_script($cmd, undef, {2 => \$err}), 'lock init failed');
	is($? >> 8, 255, 'got expected exit code on lock failure');
	ok(unlink("$cfgfile.lock"),
		'-init did not unlink lock on failure');

	my @init_args = ('i', "$tmpdir/i",
		   qw(http://example.com/i i@example.com));
	$cmd = [ qw(-init -c .bogus=val), @init_args ];
	quiet_fail($cmd, 'invalid -c KEY=VALUE fails');
	$cmd = [ qw(-init -c .bogus=val), @init_args ];
	quiet_fail($cmd, '-c KEY-only fails');
	$cmd = [ qw(-init -c address=clist@example.com), @init_args ];
	quiet_fail($cmd, '-c address=CONFLICTING-VALUE fails');

	$cmd = [ qw(-init -c no=problem -c no=problemo), @init_args ];
	ok(run_script($cmd), '-c KEY=VALUE runs');
	my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
	chomp(my @v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
	is_deeply(\@v, [ qw(problem problemo) ]) or xbail(\@v);

	ok(run_script($cmd), '-c KEY=VALUE runs idempotently');
	chomp(my @v2 = xqx([qw(git config --get-all publicinbox.i.no)], $env));
	is_deeply(\@v, \@v2, 'nothing repeated') or xbail(\@v2);

	ok(run_script([@$cmd, '-c', 'no=more']), '-c KEY=VALUE addendum');
	chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
	is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);


	ok(run_script([@$cmd, '-c', 'no=problem']), '-c KEY=VALUE repeated');
	chomp(@v = xqx([qw(git config --get-all publicinbox.i.no)], $env));
	is_deeply(\@v, [ qw(problem problemo more) ]) or xbail(\@v);

	ok(run_script([@$cmd, '-c', 'address=j@example.com']),
		'-c KEY=VALUE address');
	chomp(@v = xqx([qw(git config --get-all publicinbox.i.address)], $env));
	is_deeply(\@v, [ qw(i@example.com j@example.com) ],
		'extra address added via -c KEY=VALUE');
}
{
	my $env = { PI_DIR => "$tmpdir/.public-inbox/" };
	my $rdr = { 2 => \(my $err = '') };
	my $cmd = [ '-init', 'alist', "$tmpdir/a\nlist",
		   qw(http://example.com/alist alist@example.com) ];
	ok(!run_script($cmd, $env, $rdr),
		'public-inbox-init rejects LF in inboxdir');
	like($err, qr/`\\n' not allowed in `/s, 'reported \\n');
	is_deeply([glob("$tmpdir/.public-inbox/pi-init-*")], [],
		'no junk files left behind');

	# "git init" does this, too
	$cmd = [ '-init', 'deep-non-existent', "$tmpdir/a/b/c/d",
		   qw(http://example.com/abcd abcd@example.com) ];
	$err = '';
	ok(run_script($cmd, $env, $rdr), 'initializes non-existent hierarchy');
	ok(-d "$tmpdir/a/b/c/d", 'directory created');
	is(PublicInbox::Inbox::try_cat("$tmpdir/a/b/c/d/description"),
		"public inbox for abcd\@example.com\n", 'description set');

	open my $fh, '>', "$tmpdir/d" or BAIL_OUT "open: $!";
	close $fh;
	$cmd = [ '-init', 'd-f-conflict', "$tmpdir/d/f/conflict",
		   qw(http://example.com/conflict onflict@example.com) ];
	ok(!run_script($cmd, $env, $rdr), 'fails on D/F conflict');
}

SKIP: {
	require_mods(qw(DBD::SQLite Search::Xapian), 2);
	require_git(2.6, 1) or skip "git 2.6+ required", 2;
	use_ok 'PublicInbox::Msgmap';
	local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
	local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
	my $cfgfile = "$ENV{PI_DIR}/config";
	my $cmd = [ '-init', '-V2', 'v2list', "$tmpdir/v2list",
		   qw(http://example.com/v2list v2list@example.com) ];
	ok(run_script($cmd), 'public-inbox-init -V2 OK');
	ok(-d "$tmpdir/v2list", 'v2list directory exists');
	ok(-f "$tmpdir/v2list/msgmap.sqlite3", 'msgmap exists');
	ok(-d "$tmpdir/v2list/all.git", 'catch-all.git directory exists');
	$cmd = [ '-init', 'v2list', "$tmpdir/v2list",
		   qw(http://example.com/v2list v2list@example.com) ];
	ok(run_script($cmd), 'public-inbox-init is idempotent');
	ok(! -d "$tmpdir/public-inbox" && !-d "$tmpdir/objects",
		'idempotent invocation w/o -V2 does not make inbox v1');
	is(read_indexlevel('v2list'), '', 'indexlevel unset by default');

	$cmd = [ '-init', 'v2list', "-V1", "$tmpdir/v2list",
		   qw(http://example.com/v2list v2list@example.com) ];
	quiet_fail($cmd, 'initializing V2 as V1 fails');

	foreach my $lvl (qw(medium basic)) {
		my $dir = "$tmpdir/v2$lvl";
		$cmd = [ '-init', "v2$lvl", '-V2', '-L', $lvl,
			$dir, "http://example.com/v2$lvl",
			"v2$lvl\@example.com" ];
		ok(run_script($cmd), "-init -L $lvl");
		is(read_indexlevel("v2$lvl"), $lvl, "indexlevel set to '$lvl'");
		my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
		is(PublicInbox::Admin::detect_indexlevel($ibx), $lvl,
			'detected expected level w/o config');
		ok(!$ibx->{-skip_docdata}, 'docdata written by default');
	}
	for my $v (1, 2) {
		my $name = "v$v-skip-docdata";
		my $dir = "$tmpdir/$name";
		$cmd = [ '-init', $name, "-V$v", '--skip-docdata',
			$dir, "http://example.com/$name",
			"$name\@example.com" ];
		ok(run_script($cmd), "-init -V$v --skip-docdata");
		my $ibx = PublicInbox::Inbox->new({ inboxdir => $dir });
		is(PublicInbox::Admin::detect_indexlevel($ibx), 'full',
			"detected default indexlevel -V$v");
		ok($ibx->{-skip_docdata}, "docdata skip set -V$v");
		ok($ibx->search->has_threadid, 'has_threadid flag set on new inbox');
	}

	# loop for idempotency
	for (1..2) {
		$cmd = [ '-init', '-V2', '-S1', 'skip1', "$tmpdir/skip1",
			   qw(http://example.com/skip1 skip1@example.com) ];
		ok(run_script($cmd), "--skip-epoch 1");
		my $gits = [ glob("$tmpdir/skip1/git/*.git") ];
		is_deeply($gits, ["$tmpdir/skip1/git/1.git"], 'skip OK');
	}

	$cmd = [ '-init', '-V2', '--skip-epoch=2', 'skip2', "$tmpdir/skip2",
		   qw(http://example.com/skip2 skip2@example.com) ];
	ok(run_script($cmd), "--skip-epoch 2");
	my $gits = [ glob("$tmpdir/skip2/git/*.git") ];
	is_deeply($gits, ["$tmpdir/skip2/git/2.git"], 'skipping 2 works, too');

	xsys(qw(git config), "--file=$ENV{PI_DIR}/config",
			'publicinboxmda.spamcheck', 'none') == 0 or
			BAIL_OUT "git config $?";
	my $addr = 'skip3@example.com';
	$cmd = [ qw(-init -V2 -Lbasic --skip-artnum=12 skip3), "$tmpdir/skip3",
		   qw(http://example.com/skip3), $addr ];
	ok(run_script($cmd), '--skip-artnum -V2');
	my $env = { ORIGINAL_RECIPIENT => $addr };
	my $mid = 'skip-artnum@example.com';
	my $msg = "Message-ID: <$mid>\n\n";
	my $rdr = { 0 => \$msg, 2 => \(my $err = '')  };
	ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
	diag "err=$err" if $err;
	my $mm = PublicInbox::Msgmap->new_file("$tmpdir/skip3/msgmap.sqlite3");
	my $n = $mm->num_for($mid);
	is($n, 13, 'V2 NNTP article numbers skipped via --skip-artnum');

	$addr = 'skip4@example.com';
	$env = { ORIGINAL_RECIPIENT => $addr };
	$cmd = [ qw(-init -V1 --skip-artnum 12 -Lmedium skip4), "$tmpdir/skip4",
		   qw(http://example.com/skip4), $addr ];
	ok(run_script($cmd), '--skip-artnum -V1');
	$err = '';
	ok(run_script([qw(-mda --no-precheck)], $env, $rdr), 'deliver V1');
	diag "err=$err" if $err;
	$mm = PublicInbox::Msgmap->new("$tmpdir/skip4");
	$n = $mm->num_for($mid);
	is($n, 13, 'V1 NNTP article numbers skipped via --skip-artnum');
}

done_testing();

sub read_indexlevel {
	my ($inbox) = @_;
	my $cmd = [ qw(git config), "publicinbox.$inbox.indexlevel" ];
	my $env = { GIT_CONFIG => "$ENV{PI_DIR}/config" };
	chomp(my $lvl = xqx($cmd, $env));
	$lvl;
}

debug log:

solving 752e5af9 ...
found 752e5af9 in https://80x24.org/public-inbox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).