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