]> Sergey Matveev's repositories - public-inbox.git/blob - t/config.t
t/config: test --get-urlmatch for git <2.26
[public-inbox.git] / t / config.t
1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Config;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
9 my ($tmpdir, $for_destroy) = tmpdir();
10
11 {
12         PublicInbox::Import::init_bare($tmpdir);
13         my $inboxdir = "$tmpdir/new\nline";
14         my @cmd = ('git', "--git-dir=$tmpdir",
15                 qw(config publicinbox.foo.inboxdir), $inboxdir);
16         is(xsys(@cmd), 0, "set config");
17
18         my $tmp = PublicInbox::Config->new("$tmpdir/config");
19
20         is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
21                 'config read correctly');
22         is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
23
24         my @warn;
25         local $SIG{__WARN__} = sub { push @warn, @_ };
26         $tmp = PublicInbox::Config->new("$tmpdir/config");
27         is($tmp->lookup_name('foo'), undef, 'reject invalid inboxdir');
28         like("@warn", qr/^E:.*must not contain `\\n'/sm,
29                 'warned about newline');
30 }
31
32 {
33         my $f = "examples/public-inbox-config";
34         ok(-r $f, "$f is readable");
35
36         my $cfg = PublicInbox::Config->new($f);
37         is_deeply($cfg->lookup('meta@public-inbox.org'), {
38                 'inboxdir' => '/home/pi/meta-main.git',
39                 'address' => [ 'meta@public-inbox.org' ],
40                 'domain' => 'public-inbox.org',
41                 'url' => [ 'http://example.com/meta' ],
42                 -primary_address => 'meta@public-inbox.org',
43                 'name' => 'meta',
44                 feedmax => 25,
45                 -httpbackend_limiter => undef,
46                 nntpserver => undef,
47         }, "lookup matches expected output");
48
49         is($cfg->lookup('blah@example.com'), undef,
50                 "non-existent lookup returns undef");
51
52         my $test = $cfg->lookup('test@public-inbox.org');
53         is_deeply($test, {
54                 'address' => ['try@public-inbox.org',
55                               'sandbox@public-inbox.org',
56                               'test@public-inbox.org'],
57                 -primary_address => 'try@public-inbox.org',
58                 'inboxdir' => '/home/pi/test-main.git',
59                 'domain' => 'public-inbox.org',
60                 'name' => 'test',
61                 feedmax => 25,
62                 'url' => [ 'http://example.com/test' ],
63                 -httpbackend_limiter => undef,
64                 nntpserver => undef,
65         }, "lookup matches expected output for test");
66 }
67
68
69 {
70         my $cfgpfx = "publicinbox.test";
71         my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
72         my $config = PublicInbox::Config->new(\<<EOF);
73 $cfgpfx.address=test\@example.com
74 $cfgpfx.mainrepo=/path/to/non/existent
75 $cfgpfx.altid=serial:gmane:file=a
76 $cfgpfx.altid=serial:enamg:file=b
77 EOF
78         my $ibx = $config->lookup_name('test');
79         is_deeply($ibx->{altid}, [ @altid ]);
80
81         $config = PublicInbox::Config->new(\<<EOF);
82 $cfgpfx.address=test\@example.com
83 $cfgpfx.mainrepo=/path/to/non/existent
84 EOF
85         $ibx = $config->lookup_name('test');
86         is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
87
88         $config = PublicInbox::Config->new(\<<EOF);
89 $cfgpfx.address=test\@example.com
90 $cfgpfx.inboxdir=/path/to/non/existent
91 $cfgpfx.mainrepo=/path/to/deprecated
92 EOF
93         $ibx = $config->lookup_name('test');
94         is($ibx->{inboxdir}, '/path/to/non/existent',
95                 'inboxdir takes precedence');
96 }
97
98 {
99         my $pfx = "publicinbox.test";
100         my $str = <<EOF;
101 $pfx.address=test\@example.com
102 $pfx.inboxdir=/path/to/non/existent
103 publicinbox.nntpserver=news.example.com
104 EOF
105         my $cfg = PublicInbox::Config->new(\$str);
106         my $ibx = $cfg->lookup_name('test');
107         is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
108
109         $str = <<EOF;
110 $pfx.address=test\@example.com
111 $pfx.inboxdir=/path/to/non/existent
112 $pfx.nntpserver=news.alt.example.com
113 EOF
114         $cfg = PublicInbox::Config->new(\$str);
115         $ibx = $cfg->lookup_name('test');
116         is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
117 }
118
119 # no obfuscate domains
120 {
121         my $pfx = "publicinbox.test";
122         my $pfx2 = "publicinbox.foo";
123         my $str = <<EOF;
124 $pfx.address=test\@example.com
125 $pfx.inboxdir=/path/to/non/existent
126 $pfx2.address=foo\@example.com
127 $pfx2.inboxdir=/path/to/foo
128 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
129 $pfx.obfuscate=true
130 EOF
131         my $cfg = PublicInbox::Config->new(\$str);
132         my $ibx = $cfg->lookup_name('test');
133         my $re = $ibx->{-no_obfuscate_re};
134         like('meta@public-inbox.org', $re,
135                 'public-inbox.org address not to be obfuscated');
136         like('t@example.com', $re, 'example.com address not to be obfuscated');
137         unlike('t@example.comM', $re, 'example.comM address does not match');
138         is_deeply($ibx->{-no_obfuscate}, {
139                         'test@example.com' => 1,
140                         'foo@example.com' => 1,
141                         'z@example.com' => 1,
142                 }, 'known addresses populated');
143 }
144
145 my @invalid = (
146         # git rejects this because it locks refnames, but we don't have
147         # this problem with inbox names:
148         # 'inbox.lock',
149
150         # git rejects these:
151         '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
152         '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
153         'question?', 'colon:', '[square-brace]', "\fformfeed",
154         "\0zero", "\bbackspace",
155
156 );
157
158 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
159 my $xre = join('|', keys %X);
160
161 for my $s (@invalid) {
162         my $d = $s;
163         $d =~ s/($xre)/$X{$1}/g;
164         ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
165 }
166
167 # obviously-valid examples
168 my @valid = qw(a a@example a@example.com);
169
170 # Rejecting more was considered, but then it dawned on me that
171 # people may intentionally use inbox names which are not URL-friendly
172 # to prevent the PSGI interface from displaying them...
173 # URL-unfriendly
174 # '<', '>', '%', '#', '?', '&', '(', ')',
175
176 # maybe these aren't so bad, they're common in Message-IDs, even:
177 # '!', '$', '=', '+'
178 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
179 for my $s (@valid) {
180         ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
181 }
182
183 {
184         my $f = "$tmpdir/ordered";
185         open my $fh, '>', $f or die "open: $!";
186         my @expect;
187         foreach my $i (0..3) {
188                 push @expect, "$i";
189                 print $fh <<"" or die "print: $!";
190 [publicinbox "$i"]
191         inboxdir = /path/to/$i.git
192         address = $i\@example.com
193
194         }
195         close $fh or die "close: $!";
196         my $cfg = PublicInbox::Config->new($f);
197         my @result;
198         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
199         is_deeply(\@result, \@expect);
200 }
201
202 {
203         my $pfx1 = "publicinbox.test1";
204         my $pfx2 = "publicinbox.test2";
205         my $str = <<EOF;
206 $pfx1.address=test\@example.com
207 $pfx1.inboxdir=/path/to/non/existent
208 $pfx2.address=foo\@example.com
209 $pfx2.inboxdir=/path/to/foo
210 $pfx1.coderepo=project
211 $pfx2.coderepo=project
212 coderepo.project.dir=/path/to/project.git
213 EOF
214         my $cfg = PublicInbox::Config->new(\$str);
215         my $t1 = $cfg->lookup_name('test1');
216         my $t2 = $cfg->lookup_name('test2');
217         is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
218                 'inboxes share ::Git object');
219 }
220
221 {
222         for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
223                 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
224                 is(xqx([qw(git -c), "test.val=$t",
225                         qw(config --bool test.val)]),
226                         "true\n", "$t matches git-config behavior");
227         }
228         for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
229                 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
230                 is(xqx([qw(git -c), "test.val=$f",
231                         qw(config --bool test.val)]),
232                         "false\n", "$f matches git-config behavior");
233         }
234         is(PublicInbox::Config::git_bool('bogus'), undef,
235                 'bogus is undef');
236 }
237
238 SKIP: {
239         # XXX wildcard match requires git 2.26+
240         require_git('1.8.5', 2) or
241                 skip 'git 1.8.5+ required for --url-match', 2;
242         my $f = "$tmpdir/urlmatch";
243         open my $fh, '>', $f or BAIL_OUT $!;
244         print $fh <<EOF or BAIL_OUT $!;
245 [imap "imap://mail.example.com"]
246         pollInterval = 9
247 EOF
248         close $fh or BAIL_OUT;
249         local $ENV{PI_CONFIG} = $f;
250         my $cfg = PublicInbox::Config->new;
251         my $url = 'imap://mail.example.com/INBOX';
252         is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
253         is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');
254 };
255
256
257 done_testing();