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