1 # Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::Config;
7 use File::Temp qw/tempdir/;
8 my $tmpdir = tempdir('pi-config-XXXXXX', TMPDIR => 1, CLEANUP => 1);
11 is(system(qw(git init -q --bare), $tmpdir), 0, "git init successful");
12 my @cmd = ('git', "--git-dir=$tmpdir", qw(config foo.bar), "hi\nhi");
13 is(system(@cmd), 0, "set config");
15 my $tmp = PublicInbox::Config->new("$tmpdir/config");
17 is("hi\nhi", $tmp->{"foo.bar"}, "config read correctly");
18 is("true", $tmp->{"core.bare"}, "used --bare repo");
22 my $f = "examples/public-inbox-config";
23 ok(-r $f, "$f is readable");
25 my $cfg = PublicInbox::Config->new($f);
26 is_deeply($cfg->lookup('meta@public-inbox.org'), {
27 'mainrepo' => '/home/pi/meta-main.git',
28 'address' => [ 'meta@public-inbox.org' ],
29 'domain' => 'public-inbox.org',
30 'url' => 'http://example.com/meta',
31 -primary_address => 'meta@public-inbox.org',
34 -httpbackend_limiter => undef,
36 }, "lookup matches expected output");
38 is($cfg->lookup('blah@example.com'), undef,
39 "non-existent lookup returns undef");
41 my $test = $cfg->lookup('test@public-inbox.org');
43 'address' => ['try@public-inbox.org',
44 'sandbox@public-inbox.org',
45 'test@public-inbox.org'],
46 -primary_address => 'try@public-inbox.org',
47 'mainrepo' => '/home/pi/test-main.git',
48 'domain' => 'public-inbox.org',
51 'url' => 'http://example.com/test',
52 -httpbackend_limiter => undef,
54 }, "lookup matches expected output for test");
59 my $cfgpfx = "publicinbox.test";
60 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
61 my $config = PublicInbox::Config->new(\<<EOF);
62 $cfgpfx.address=test\@example.com
63 $cfgpfx.mainrepo=/path/to/non/existent
64 $cfgpfx.altid=serial:gmane:file=a
65 $cfgpfx.altid=serial:enamg:file=b
67 my $ibx = $config->lookup_name('test');
68 is_deeply($ibx->{altid}, [ @altid ]);
72 my $pfx = "publicinbox.test";
74 $pfx.address=test\@example.com
75 $pfx.mainrepo=/path/to/non/existent
76 publicinbox.nntpserver=news.example.com
78 my $cfg = PublicInbox::Config->new(\$str);
79 my $ibx = $cfg->lookup_name('test');
80 is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
83 $pfx.address=test\@example.com
84 $pfx.mainrepo=/path/to/non/existent
85 $pfx.nntpserver=news.alt.example.com
87 $cfg = PublicInbox::Config->new(\$str);
88 $ibx = $cfg->lookup_name('test');
89 is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
92 # no obfuscate domains
94 my $pfx = "publicinbox.test";
95 my $pfx2 = "publicinbox.foo";
97 $pfx.address=test\@example.com
98 $pfx.mainrepo=/path/to/non/existent
99 $pfx2.address=foo\@example.com
100 $pfx2.mainrepo=/path/to/foo
101 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
104 my $cfg = PublicInbox::Config->new(\$str);
105 my $ibx = $cfg->lookup_name('test');
106 my $re = $ibx->{-no_obfuscate_re};
107 like('meta@public-inbox.org', $re,
108 'public-inbox.org address not to be obfuscated');
109 like('t@example.com', $re, 'example.com address not to be obfuscated');
110 unlike('t@example.comM', $re, 'example.comM address does not match');
111 is_deeply($ibx->{-no_obfuscate}, {
112 'test@example.com' => 1,
113 'foo@example.com' => 1,
114 'z@example.com' => 1,
115 }, 'known addresses populated');
119 # git rejects this because it locks refnames, but we don't have
120 # this problem with inbox names:
124 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
125 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
126 'question?', 'colon:', '[square-brace]', "\fformfeed",
127 "\0zero", "\bbackspace",
131 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
132 my $xre = join('|', keys %X);
134 for my $s (@invalid) {
136 $d =~ s/($xre)/$X{$1}/g;
137 ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
140 # obviously-valid examples
141 my @valid = qw(a a@example a@example.com);
143 # Rejecting more was considered, but then it dawned on me that
144 # people may intentionally use inbox names which are not URL-friendly
145 # to prevent the PSGI interface from displaying them...
147 # '<', '>', '%', '#', '?', '&', '(', ')',
149 # maybe these aren't so bad, they're common in Message-IDs, even:
151 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
153 ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
157 my $f = "$tmpdir/ordered";
158 open my $fh, '>', $f or die "open: $!";
160 foreach my $i (0..3) {
162 print $fh <<"" or die "print: $!";
164 mainrepo = /path/to/$i.git
165 address = $i\@example.com
168 close $fh or die "close: $!";
169 my $cfg = PublicInbox::Config->new($f);
171 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
172 is_deeply(\@result, \@expect);
176 my $pfx1 = "publicinbox.test1";
177 my $pfx2 = "publicinbox.test2";
179 $pfx1.address=test\@example.com
180 $pfx1.mainrepo=/path/to/non/existent
181 $pfx2.address=foo\@example.com
182 $pfx2.mainrepo=/path/to/foo
183 $pfx1.coderepo=project
184 $pfx2.coderepo=project
185 coderepo.project.dir=/path/to/project.git
187 my $cfg = PublicInbox::Config->new(\$str);
188 my $t1 = $cfg->lookup_name('test1');
189 my $t2 = $cfg->lookup_name('test2');
190 is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
191 'inboxes share ::Git object');
195 my $check_git = !!$ENV{CHECK_GIT_BOOL};
196 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
197 is(PublicInbox::Config::_git_config_bool($t), 1, "$t is true");
199 is(`git -c test.val=$t config --bool test.val`,
200 "true\n", "$t matches git-config behavior");
203 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
204 is(PublicInbox::Config::_git_config_bool($f), 0, "$f is false");
206 is(`git -c test.val=$f config --bool test.val`,
207 "false\n", "$f matches git-config behavior");
210 is(PublicInbox::Config::_git_config_bool('bogus'), undef,