]> Sergey Matveev's repositories - public-inbox.git/blob - t/config.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / config.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Config;
7 use File::Temp qw/tempdir/;
8 my $tmpdir = tempdir('pi-config-XXXXXX', TMPDIR => 1, CLEANUP => 1);
9
10 {
11         is(system(qw(git init -q --bare), $tmpdir), 0, "git init successful");
12         my @cmd = ('git', "--git-dir=$tmpdir", qw(config foo.bar hihi));
13         is(system(@cmd), 0, "set config");
14
15         my $tmp = PublicInbox::Config->new("$tmpdir/config");
16
17         is("hihi", $tmp->{"foo.bar"}, "config read correctly");
18         is("true", $tmp->{"core.bare"}, "used --bare repo");
19 }
20
21 {
22         my $f = "examples/public-inbox-config";
23         ok(-r $f, "$f is readable");
24
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',
32                 'name' => 'meta',
33                 feedmax => 25,
34                 -httpbackend_limiter => undef,
35                 nntpserver => undef,
36         }, "lookup matches expected output");
37
38         is($cfg->lookup('blah@example.com'), undef,
39                 "non-existent lookup returns undef");
40
41         my $test = $cfg->lookup('test@public-inbox.org');
42         is_deeply($test, {
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',
49                 'name' => 'test',
50                 feedmax => 25,
51                 'url' => 'http://example.com/test',
52                 -httpbackend_limiter => undef,
53                 nntpserver => undef,
54         }, "lookup matches expected output for test");
55 }
56
57
58 {
59         my $cfgpfx = "publicinbox.test";
60         my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
61         my $config = PublicInbox::Config->new({
62                 "$cfgpfx.address" => 'test@example.com',
63                 "$cfgpfx.mainrepo" => '/path/to/non/existent',
64                 "$cfgpfx.altid" => [ @altid ],
65         });
66         my $ibx = $config->lookup_name('test');
67         is_deeply($ibx->{altid}, [ @altid ]);
68 }
69
70 {
71         my $pfx = "publicinbox.test";
72         my %h = (
73                 "$pfx.address" => 'test@example.com',
74                 "$pfx.mainrepo" => '/path/to/non/existent',
75                 "publicinbox.nntpserver" => 'news.example.com',
76         );
77         my %tmp = %h;
78         my $cfg = PublicInbox::Config->new(\%tmp);
79         my $ibx = $cfg->lookup_name('test');
80         is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
81
82         delete $h{'publicinbox.nntpserver'};
83         $h{"$pfx.nntpserver"} = 'news.alt.example.com';
84         $cfg = PublicInbox::Config->new(\%h);
85         $ibx = $cfg->lookup_name('test');
86         is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
87 }
88
89 # no obfuscate domains
90 {
91         my $pfx = "publicinbox.test";
92         my $pfx2 = "publicinbox.foo";
93         my %h = (
94                 "$pfx.address" => 'test@example.com',
95                 "$pfx.mainrepo" => '/path/to/non/existent',
96                 "$pfx2.address" => 'foo@example.com',
97                 "$pfx2.mainrepo" => '/path/to/foo',
98                 lc("publicinbox.noObfuscate") =>
99                         'public-inbox.org @example.com z@EXAMPLE.com',
100                 "$pfx.obfuscate" => 'true', # :<
101         );
102         my %tmp = %h;
103         my $cfg = PublicInbox::Config->new(\%tmp);
104         my $ibx = $cfg->lookup_name('test');
105         my $re = $ibx->{-no_obfuscate_re};
106         like('meta@public-inbox.org', $re,
107                 'public-inbox.org address not to be obfuscated');
108         like('t@example.com', $re, 'example.com address not to be obfuscated');
109         unlike('t@example.comM', $re, 'example.comM address does not match');
110         is_deeply($ibx->{-no_obfuscate}, {
111                         'test@example.com' => 1,
112                         'foo@example.com' => 1,
113                         'z@example.com' => 1,
114                 }, 'known addresses populated');
115 }
116
117 my @invalid = (
118         # git rejects this because it locks refnames, but we don't have
119         # this problem with inbox names:
120         # 'inbox.lock',
121
122         # git rejects these:
123         '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
124         '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
125         'question?', 'colon:', '[square-brace]', "\fformfeed",
126         "\0zero", "\bbackspace",
127
128 );
129
130 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
131 my $xre = join('|', keys %X);
132
133 for my $s (@invalid) {
134         my $d = $s;
135         $d =~ s/($xre)/$X{$1}/g;
136         ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
137 }
138
139 # obviously-valid examples
140 my @valid = qw(a a@example a@example.com);
141
142 # Rejecting more was considered, but then it dawned on me that
143 # people may intentionally use inbox names which are not URL-friendly
144 # to prevent the PSGI interface from displaying them...
145 # URL-unfriendly
146 # '<', '>', '%', '#', '?', '&', '(', ')',
147
148 # maybe these aren't so bad, they're common in Message-IDs, even:
149 # '!', '$', '=', '+'
150 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
151 for my $s (@valid) {
152         ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
153 }
154
155 {
156         my $f = "$tmpdir/ordered";
157         open my $fh, '>', $f or die "open: $!";
158         my @expect;
159         foreach my $i (0..3) {
160                 push @expect, "$i";
161                 print $fh <<"" or die "print: $!";
162 [publicinbox "$i"]
163         mainrepo = /path/to/$i.git
164         address = $i\@example.com
165
166         }
167         close $fh or die "close: $!";
168         my $cfg = PublicInbox::Config->new($f);
169         my @result;
170         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
171         is_deeply(\@result, \@expect);
172 }
173
174 {
175         my $pfx1 = "publicinbox.test1";
176         my $pfx2 = "publicinbox.test2";
177         my $h = {
178                 "$pfx1.address" => 'test@example.com',
179                 "$pfx1.mainrepo" => '/path/to/non/existent',
180                 "$pfx2.address" => 'foo@example.com',
181                 "$pfx2.mainrepo" => '/path/to/foo',
182                 "$pfx1.coderepo" => 'project',
183                 "$pfx2.coderepo" => 'project',
184                 "coderepo.project.dir" => '/path/to/project.git',
185         };
186         my $cfg = PublicInbox::Config->new($h);
187         my $t1 = $cfg->lookup_name('test1');
188         my $t2 = $cfg->lookup_name('test2');
189         is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
190                 'inboxes share ::Git object');
191 }
192
193 done_testing();