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