lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / config.t
blobc41a42d366783a7fa2fc73cb6c6bdeeeb80d21a1
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use v5.12;
4 use PublicInbox::TestCommon;
5 use PublicInbox::Import;
6 use_ok 'PublicInbox';
7 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
8 use_ok 'PublicInbox::Config';
9 my ($tmpdir, $for_destroy) = tmpdir();
10 use autodie qw(open close);
11 my $validate_git_behavior = $ENV{TEST_VALIDATE_GIT_BEHAVIOR};
14         my $f = "$tmpdir/bool_config";
15         open my $fh, '>', $f;
16         print $fh <<EOM;
17 [imap]
18         debug
19         port = 2
20 EOM
21         close $fh;
22         my $cfg = PublicInbox::Config->git_config_dump($f);
23         $validate_git_behavior and
24                 is(xqx([qw(git config -f), $f, qw(--bool imap.debug)]),
25                         "true\n", 'git handles key-only as truth');
26         ok($cfg->git_bool($cfg->{'imap.debug'}), 'key-only value handled');
27         is($cfg->{'imap.port'}, 2, 'normal k=v read after key-only');
31         PublicInbox::Import::init_bare($tmpdir);
32         my $inboxdir = "$tmpdir/new\nline";
33         my @cmd = ('git', "--git-dir=$tmpdir",
34                 qw(config publicinbox.foo.inboxdir), $inboxdir);
35         is(xsys(@cmd), 0, "set config");
37         my $tmp = PublicInbox::Config->new("$tmpdir/config");
39         is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
40                 'config read correctly');
41         is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
43         my @warn;
44         local $SIG{__WARN__} = sub { push @warn, @_ };
45         $tmp = PublicInbox::Config->new("$tmpdir/config");
46         is($tmp->lookup_name('foo'), undef, 'reject invalid inboxdir');
47         like("@warn", qr/^E:.*must not contain `\\n'/sm,
48                 'warned about newline');
52         my $f = "examples/public-inbox-config";
53         ok(-r $f, "$f is readable");
55         my $cfg = PublicInbox::Config->new($f);
56         is_deeply($cfg->lookup('meta@public-inbox.org'), {
57                 'inboxdir' => '/home/pi/meta-main.git',
58                 'address' => [ 'meta@public-inbox.org' ],
59                 'domain' => 'public-inbox.org',
60                 'url' => [ 'http://example.com/meta' ],
61                 -primary_address => 'meta@public-inbox.org',
62                 'name' => 'meta',
63                 -httpbackend_limiter => undef,
64         }, "lookup matches expected output");
66         is($cfg->lookup('blah@example.com'), undef,
67                 "non-existent lookup returns undef");
69         my $test = $cfg->lookup('test@public-inbox.org');
70         is_deeply($test, {
71                 'address' => ['try@public-inbox.org',
72                               'sandbox@public-inbox.org',
73                               'test@public-inbox.org'],
74                 -primary_address => 'try@public-inbox.org',
75                 'inboxdir' => '/home/pi/test-main.git',
76                 'domain' => 'public-inbox.org',
77                 'name' => 'test',
78                 'url' => [ 'http://example.com/test' ],
79                 -httpbackend_limiter => undef,
80         }, "lookup matches expected output for test");
85         my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
86         my $config = cfg_new $tmpdir, <<EOF;
87 [publicinbox "test"]
88         address = test\@example.com
89         inboxdir = /path/to/non/existent
90         altid=serial:gmane:file=a
91         altid=serial:enamg:file=b
92 EOF
93         my $ibx = $config->lookup_name('test');
94         is_deeply($ibx->{altid}, [ @altid ]);
96         $config = cfg_new $tmpdir, <<EOF;
97 [publicinbox "test"]
98         address = test\@example.com
99         inboxdir = /path/to/non/existent
101         $ibx = $config->lookup_name('test');
102         is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
104         $config = cfg_new $tmpdir, <<EOF;
105 [publicinbox "test"]
106         address = test\@example.com
107         inboxdir = /path/to/non/existent
108         mainrepo = /path/to/deprecated
110         $ibx = $config->lookup_name('test');
111         is($ibx->{inboxdir}, '/path/to/non/existent',
112                 'inboxdir takes precedence');
116         my $cfg = cfg_new $tmpdir, <<EOF;
117 [publicinbox "test"]
118         address = test\@example.com
119         inboxdir = /path/to/non/existent
120         newsgroup = inbox.test
121 [publicinbox]
122         nntpserver = news.example.com
124         my $ibx = $cfg->lookup_name('test');
125         is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
126                 [ 'nntp://news.example.com/inbox.test' ],
127                 'nntp_url uses global NNTP server');
129         $cfg = cfg_new $tmpdir, <<EOF;
130 [publicinbox "test"]
131         address = test\@example.com
132         inboxdir = /path/to/non/existent
133         newsgroup = inbox.test
134         nntpserver = news.alt.example.com
135 [publicinbox]
136         nntpserver = news.example.com
137         imapserver = imaps://mail.example.com
139         $ibx = $cfg->lookup_name('test');
140         is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
141                 [ 'nntp://news.alt.example.com/inbox.test' ],
142                 'nntp_url uses per-inbox NNTP server');
143         is_deeply($ibx->imap_url({ www => { pi_cfg => $cfg }}),
144                 [ 'imaps://mail.example.com/inbox.test' ],
145                 'nntp_url uses per-inbox NNTP server');
148 # no obfuscate domains
150         my $cfg = cfg_new $tmpdir, <<EOF;
151 [publicinbox "test"]
152         address = test\@example.com
153         inboxdir = /path/to/non/existent
154 [publicinbox "foo"]
155         address = foo\@example.com
156         inboxdir = /path/to/foo
157 [publicinbox]
158         noobfuscate = public-inbox.org \@example.com z\@EXAMPLE.com
159 [publicinbox "test"]
160         obfuscate = true
162         my $ibx = $cfg->lookup_name('test');
163         my $re = $ibx->{-no_obfuscate_re};
164         like('meta@public-inbox.org', $re,
165                 'public-inbox.org address not to be obfuscated');
166         like('t@example.com', $re, 'example.com address not to be obfuscated');
167         unlike('t@example.comM', $re, 'example.comM address does not match');
168         is_deeply($ibx->{-no_obfuscate}, {
169                         'test@example.com' => 1,
170                         'foo@example.com' => 1,
171                         'z@example.com' => 1,
172                 }, 'known addresses populated');
175 my @invalid = (
176         # git rejects this because it locks refnames, but we don't have
177         # this problem with inbox names:
178         # 'inbox.lock',
180         # git rejects these:
181         '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
182         '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
183         'question?', 'colon:', '[square-brace]', "\fformfeed",
184         "\0zero", "\bbackspace",
188 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
189 my $xre = join('|', keys %X);
191 for my $s (@invalid) {
192         my $d = $s;
193         $d =~ s/($xre)/$X{$1}/g;
194         ok(!PublicInbox::Config::valid_foo_name($s), "`$d' name rejected");
197 # obviously-valid examples
198 my @valid = qw(a a@example a@example.com);
200 # Rejecting more was considered, but then it dawned on me that
201 # people may intentionally use inbox names which are not URL-friendly
202 # to prevent the PSGI interface from displaying them...
203 # URL-unfriendly
204 # '<', '>', '%', '#', '?', '&', '(', ')',
206 # maybe these aren't so bad, they're common in Message-IDs, even:
207 # '!', '$', '=', '+'
208 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
209 for my $s (@valid) {
210         ok(PublicInbox::Config::valid_foo_name($s), "`$s' name accepted");
214         my $f = "$tmpdir/ordered";
215         open my $fh, '>', $f or die "open: $!";
216         my @expect;
217         foreach my $i (0..3) {
218                 push @expect, "$i";
219                 print $fh <<"" or die "print: $!";
220 [publicinbox "$i"]
221         inboxdir = /path/to/$i.git
222         address = $i\@example.com
224         }
225         close $fh or die "close: $!";
226         my $cfg = PublicInbox::Config->new($f);
227         my @result;
228         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
229         is_deeply(\@result, \@expect);
233         my $cfg = cfg_new $tmpdir, <<EOF;
234 [publicinbox "test1"]
235         address = test\@example.com
236         inboxdir = /path/to/non/existent
237         coderepo = project
238 [publicinbox "test2"]
239         address = foo\@example.com
240         inboxdir = /path/to/foo
241         coderepo = project
242 [coderepo "project"]
243         dir = /path/to/project.git
245         my $t1 = $cfg->lookup_name('test1');
246         my $t2 = $cfg->lookup_name('test2');
247         ok $cfg->repo_objs($t1)->[0], 'coderepo parsed';
248         is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0],
249                 'inboxes share ::Git object');
253         for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
254                 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
255                 is(xqx([qw(git -c), "test.val=$t",
256                         qw(config --bool test.val)]),
257                         "true\n", "$t matches git-config behavior");
258         }
259         for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
260                 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
261                 is(xqx([qw(git -c), "test.val=$f",
262                         qw(config --bool test.val)]),
263                         "false\n", "$f matches git-config behavior");
264         }
265         is(PublicInbox::Config::git_bool('bogus'), undef,
266                 'bogus is undef');
269 SKIP: {
270         # XXX wildcard match requires git 2.26+
271         require_git v1.8.5, 2;
272         my $cfg = cfg_new $tmpdir, <<EOF;
273 [imap "imap://mail.example.com"]
274         pollInterval = 9
276         my $url = 'imap://mail.example.com/INBOX';
277         is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
278         is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');
281 my $glob2re = PublicInbox::Config->can('glob2re');
282 is($glob2re->('http://[::1]:1234/foo/'), undef, 'IPv6 URL not globbed');
283 is($glob2re->('foo'), undef, 'plain string unchanged');
284 is_deeply($glob2re->('[f-o]'), '[f-o]' , 'range accepted');
285 is_deeply($glob2re->('*'), '[^/]*?' , 'wildcard accepted');
286 is_deeply($glob2re->('{a,b,c}'), '(a|b|c)' , 'braces');
287 is_deeply($glob2re->('{,b,c}'), '(|b|c)' , 'brace with empty @ start');
288 is_deeply($glob2re->('{a,b,}'), '(a|b|)' , 'brace with empty @ end');
289 is_deeply($glob2re->('{a}'), undef, 'ungrouped brace');
290 is_deeply($glob2re->('{a'), undef, 'open left brace');
291 is_deeply($glob2re->('a}'), undef, 'open right brace');
292 is_deeply($glob2re->('*.[ch]'), '[^/]*?\\.[ch]', 'suffix glob');
293 is_deeply($glob2re->('{[a-z],9,}'), '([a-z]|9|)' , 'brace with range');
294 is_deeply($glob2re->('\\{a,b\\}'), undef, 'escaped brace');
295 is_deeply($glob2re->('\\\\{a,b}'), '\\\\\\\\(a|b)', 'fake escape brace');
296 is_deeply($glob2re->('**/foo'), '.*/foo', 'double asterisk start');
297 is_deeply($glob2re->('foo/**'), 'foo/.*', 'double asterisk end');
298 my $re = $glob2re->('a/**/b');
299 is_deeply($re, 'a(?:/.*?/|/)b', 'double asterisk middle');
300 like($_, qr!$re!, "a/**/b matches $_") for ('a/b', 'a/c/b', 'a/c/a/b');
301 unlike($_, qr!$re!, "a/**/b doesn't match $_") for ('a/ab');
304         my $w = '';
305         local $SIG{__WARN__} = sub { $w .= "@_"; };
306         my $cfg = cfg_new $tmpdir, <<EOF;
307 [publicinbox "a"]
308         address = a\@example.com
309         inboxdir = $tmpdir/aa
310 [publicinbox "b"]
311         address = b\@example.com
312         inboxdir = $tmpdir/aa
314         $cfg->fill_all;
315         like $w, qr!`\Q$tmpdir/aa\E' used by both!, 'inboxdir conflict warned';
319         my $w = '';
320         local $SIG{__WARN__} = sub { $w .= "@_"; };
321         my $cfg = cfg_new $tmpdir, <<EOF;
322 [publicinbox "a"]
323         address = a\@example.com
324         inboxdir = $tmpdir/a
325         newsgroup = inbox.test
326 [publicinbox "b"]
327         address = b\@example.com
328         inboxdir = $tmpdir/b
329         newsgroup = inbox.tesT
331         $cfg->fill_all;
332         like $w, qr!`inbox\.test' used by both!, 'newsgroup conflict warned';
333         like $w, qr!`inbox\.tesT' lowercased!, 'upcase warned';
336 done_testing;