From 21146412fdc32f8e6707b4e290ad715b35f20845 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 24 Sep 2023 05:42:14 +0000 Subject: config: drop scalar ref support from internal API It's a needless branch to maintain exclusively for our tests. The `git config -l' output isn't pleasant to write in tests, anyways. So just use heredocs to write git configs in their native format rather than emulate the output of `git config -l'. This does make the test suite do more work with temporary files and process invocations, but it doesn't seem very measurable when testing on tmpfs (TMPDIR=/dev/shm). We'll make a minor improvement to TestCommon::tmpdir by allowing it to return a single value (which I suspect we can rely on in more places since File::Temp::Dir overloads stringification). --- lib/PublicInbox/Config.pm | 23 +++------ lib/PublicInbox/TestCommon.pm | 20 ++++++-- t/config.t | 112 +++++++++++++++++++++--------------------- t/config_limiter.t | 31 ++++++------ t/inbox_idle.t | 15 +++--- t/psgi_bad_mids.t | 18 +++---- t/psgi_mount.t | 14 +++--- t/psgi_multipart_not.t | 16 +++--- t/psgi_scan_all.t | 18 ++++--- t/psgi_search.t | 12 ++--- t/psgi_text.t | 21 +++----- t/watch_filter_rubylang.t | 30 +++++------ t/watch_imap.t | 20 ++++---- t/watch_maildir.t | 24 ++++----- t/watch_maildir_v2.t | 44 +++++++++-------- t/watch_multiple_headers.t | 21 ++++---- 16 files changed, 212 insertions(+), 227 deletions(-) diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index 533f4a52..9f764c32 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -24,21 +24,14 @@ sub _array ($) { ref($_[0]) eq 'ARRAY' ? $_[0] : [ $_[0] ] } sub new { my ($class, $file, $lei) = @_; $file //= default_file(); - my $self; - my $set_dedupe; - if (ref($file) eq 'SCALAR') { # used by some tests - open my $fh, '<', $file or die; # PerlIO::scalar - $self = config_fh_parse($fh, "\n", '='); - bless $self, $class; - } else { - if (-f $file && $DEDUPE) { - $file = rel2abs_collapsed($file); - $self = $DEDUPE->{$file} and return $self; - $set_dedupe = 1; - } - $self = git_config_dump($class, $file, $lei); - $self->{'-f'} = $file; - } + my ($self, $set_dedupe); + if (-f $file && $DEDUPE) { + $file = rel2abs_collapsed($file); + $self = $DEDUPE->{$file} and return $self; + $set_dedupe = 1; + } + $self = git_config_dump($class, $file, $lei); + $self->{-f} = $file; # caches $self->{-by_addr} = {}; $self->{-by_list_id} = {}; diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm index ae67a0ae..9ad181e2 100644 --- a/lib/PublicInbox/TestCommon.pm +++ b/lib/PublicInbox/TestCommon.pm @@ -26,7 +26,7 @@ BEGIN { create_coderepo no_scm_rights tcp_host_port test_lei lei lei_ok $lei_out $lei_err $lei_opt test_httpd xbail require_cmd is_xdeeply tail_f - ignore_inline_c_missing no_pollerfd no_coredump); + ignore_inline_c_missing no_pollerfd no_coredump cfg_new); require Test::More; my @methods = grep(!/\W/, @Test::More::EXPORT); eval(join('', map { "*$_=\\&Test::More::$_;" } @methods)); @@ -46,11 +46,9 @@ sub eml_load ($) { sub tmpdir (;$) { my ($base) = @_; require File::Temp; - unless (defined $base) { - ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); - } + ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!) unless defined $base; my $tmpdir = File::Temp->newdir("pi-$base-$$-XXXX", TMPDIR => 1); - ($tmpdir->dirname, $tmpdir); + wantarray ? ($tmpdir->dirname, $tmpdir) : $tmpdir; } sub tcp_server () { @@ -894,6 +892,18 @@ sub no_pollerfd ($) { is(grep(/$re/, @of), 0, "no $re FDs") or diag explain(\@of); } } + +sub cfg_new ($;@) { + my ($tmpdir, @body) = @_; + use autodie; + require PublicInbox::Config; + my $f = "$tmpdir/tmp_cfg"; + open my $fh, '>', $f; + print $fh @body; + close $fh; + PublicInbox::Config->new($f); +} + package PublicInbox::TestCommon::InboxWakeup; use strict; sub on_inbox_unlock { ${$_[0]}->($_[1]) } diff --git a/t/config.t b/t/config.t index 8a27a920..9b6684b7 100644 --- a/t/config.t +++ b/t/config.t @@ -82,28 +82,30 @@ EOM { - my $cfgpfx = "publicinbox.test"; my @altid = qw(serial:gmane:file=a serial:enamg:file=b); - my $config = PublicInbox::Config->new(\<lookup_name('test'); is_deeply($ibx->{altid}, [ @altid ]); - $config = PublicInbox::Config->new(\<lookup_name('test'); is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works'); - $config = PublicInbox::Config->new(\<lookup_name('test'); is($ibx->{inboxdir}, '/path/to/non/existent', @@ -111,28 +113,29 @@ EOF } { - my $pfx = "publicinbox.test"; - my $str = <new(\$str); my $ibx = $cfg->lookup_name('test'); is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}), [ 'nntp://news.example.com/inbox.test' ], 'nntp_url uses global NNTP server'); - $str = <new(\$str); $ibx = $cfg->lookup_name('test'); is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}), [ 'nntp://news.alt.example.com/inbox.test' ], @@ -144,17 +147,18 @@ EOF # no obfuscate domains { - my $pfx = "publicinbox.test"; - my $pfx2 = "publicinbox.foo"; - my $str = <new(\$str); my $ibx = $cfg->lookup_name('test'); my $re = $ibx->{-no_obfuscate_re}; like('meta@public-inbox.org', $re, @@ -226,20 +230,21 @@ for my $s (@valid) { } { - my $pfx1 = "publicinbox.test1"; - my $pfx2 = "publicinbox.test2"; - my $str = <new(\$str); my $t1 = $cfg->lookup_name('test1'); my $t2 = $cfg->lookup_name('test2'); + ok $cfg->repo_objs($t1)->[0], 'coderepo parsed'; is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0], 'inboxes share ::Git object'); } @@ -263,16 +268,11 @@ EOF SKIP: { # XXX wildcard match requires git 2.26+ - require_git('1.8.5', 2); - my $f = "$tmpdir/urlmatch"; - open my $fh, '>', $f or BAIL_OUT $!; - print $fh <new; my $url = 'imap://mail.example.com/INBOX'; is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit'); is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss'); diff --git a/t/config_limiter.t b/t/config_limiter.t index 8c83aca8..f4d99080 100644 --- a/t/config_limiter.t +++ b/t/config_limiter.t @@ -1,15 +1,14 @@ -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use warnings; -use Test::More; -use PublicInbox::Config; -my $cfgpfx = "publicinbox.test"; +use v5.12; +use PublicInbox::TestCommon; +my $tmpdir = tmpdir; { - my $config = PublicInbox::Config->new(\<lookup_name('test'); my $git = $ibx->git; @@ -24,11 +23,13 @@ EOF } { - my $config = PublicInbox::Config->new(\<lookup_name('test'); my $git = $ibx->git; diff --git a/t/inbox_idle.t b/t/inbox_idle.t index 51379764..0ccffab7 100644 --- a/t/inbox_idle.t +++ b/t/inbox_idle.t @@ -1,10 +1,8 @@ #!perl -w -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use v5.10.1; +use v5.12; use PublicInbox::TestCommon; -use PublicInbox::Config; require_git 2.6; require_mods(qw(DBD::SQLite)); require PublicInbox::SearchIdx; @@ -26,10 +24,11 @@ for my $V (1, 2) { $sidx->idx_release; # allow watching on lockfile }; my $obj = InboxIdleTestObj->new; - my $pi_cfg = PublicInbox::Config->new(\<{-primary_address} + my $pi_cfg = cfg_new $tmpdir, <{-primary_address} EOF my $ident = 'whatever'; $pi_cfg->each_inbox(sub { shift->subscribe_unlock($ident, $obj) }); diff --git a/t/psgi_bad_mids.t b/t/psgi_bad_mids.t index 8e531b54..ac0eb3c3 100644 --- a/t/psgi_bad_mids.t +++ b/t/psgi_bad_mids.t @@ -1,11 +1,9 @@ #!perl -w -# Copyright (C) 2018-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use v5.10.1; +use v5.12; use PublicInbox::TestCommon; use PublicInbox::Eml; -use PublicInbox::Config; my @mods = qw(DBD::SQLite HTTP::Request::Common Plack::Test URI::Escape Plack::Builder); require_git 2.6; @@ -37,12 +35,12 @@ Date: Fri, 02 Oct 1993 00:00:0$i +0000 } }; -my $cfgpfx = "publicinbox.bad-mids"; -my $cfg = <{-primary_address} -$cfgpfx.inboxdir=$ibx->{inboxdir} -EOF -my $config = PublicInbox::Config->new(\$cfg); +my $tmpdir = tmpdir; +my $config = cfg_new $tmpdir, <{-primary_address} + inboxdir = $ibx->{inboxdir} +EOM my $www = PublicInbox::WWW->new($config); test_psgi(sub { $www->call(@_) }, sub { my ($cb) = @_; diff --git a/t/psgi_mount.t b/t/psgi_mount.t index 28689f11..e43b9f2d 100644 --- a/t/psgi_mount.t +++ b/t/psgi_mount.t @@ -1,14 +1,11 @@ #!perl -w -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use v5.10.1; +use v5.12; use PublicInbox::Eml; use PublicInbox::TestCommon; -use PublicInbox::Config; my ($tmpdir, $for_destroy) = tmpdir(); my $v1dir = "$tmpdir/v1.git"; -my $cfgpfx = "publicinbox.test"; my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder Plack::App::URLMap); require_mods(@mods); @@ -27,9 +24,10 @@ Date: Thu, 01 Jan 1970 00:00:00 +0000 zzzzzz EOF }; -my $cfg = PublicInbox::Config->new(\<{-primary_address} -$cfgpfx.inboxdir=$v1dir +my $cfg = cfg_new $tmpdir, <{-primary_address} + inboxdir = $v1dir EOF my $www = PublicInbox::WWW->new($cfg); my $app = builder(sub { diff --git a/t/psgi_multipart_not.t b/t/psgi_multipart_not.t index 96be1716..e7c43abf 100644 --- a/t/psgi_multipart_not.t +++ b/t/psgi_multipart_not.t @@ -1,11 +1,9 @@ #!perl -w -# Copyright (C) 2018-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use v5.10.1; +use v5.12; use PublicInbox::TestCommon; use PublicInbox::Eml; -use PublicInbox::Config; require_git 2.6; my @mods = qw(DBD::SQLite Xapian HTTP::Request::Common Plack::Test URI::Escape Plack::Builder Plack::Test); @@ -28,12 +26,12 @@ Freed^Wmultipart ain't what it used to be EOF }; -my $cfgpfx = "publicinbox.v2test"; -my $cfg = <{-primary_address} -$cfgpfx.inboxdir=$ibx->{inboxdir} +my $tmpdir = tmpdir; +my $www = PublicInbox::WWW->new(cfg_new($tmpdir, <{-primary_address} + inboxdir = $ibx->{inboxdir} EOF -my $www = PublicInbox::WWW->new(PublicInbox::Config->new(\$cfg)); my ($res, $raw); test_psgi(sub { $www->call(@_) }, sub { my ($cb) = @_; diff --git a/t/psgi_scan_all.t b/t/psgi_scan_all.t index 20b93b78..4c28b553 100644 --- a/t/psgi_scan_all.t +++ b/t/psgi_scan_all.t @@ -7,9 +7,9 @@ use PublicInbox::Eml; my @use = qw(HTTP::Request::Common Plack::Test); my @req = qw(URI::Escape DBD::SQLite); require_git v2.6; -require_mods(@use, @req, qw(PublicInbox::WWW PublicInbox::Config)); +require_mods(@use, @req, qw(PublicInbox::WWW)); $_->import for @use; -my $cfg = ''; +my $cfgtxt = ''; foreach my $i (1..2) { my $ibx = create_inbox "test-$i", version => 2, indexlevel => 'basic', sub { @@ -24,13 +24,15 @@ Date: Fri, 02 Oct 1993 00:00:00 +0000 hello world EOF }; - my $cfgpfx = "publicinbox.test-$i"; - $cfg .= "$cfgpfx.address=$ibx->{-primary_address}\n"; - $cfg .= "$cfgpfx.inboxdir=$ibx->{inboxdir}\n"; - $cfg .= "$cfgpfx.url=http://example.com/$i\n"; - + $cfgtxt .= <{-primary_address} + inboxdir = $ibx->{inboxdir} + url = http://example.com/$i +EOM } -my $www = PublicInbox::WWW->new(PublicInbox::Config->new(\$cfg)); +my $tmpdir = tmpdir; +my $www = PublicInbox::WWW->new(cfg_new($tmpdir, $cfgtxt)); test_psgi(sub { $www->call(@_) }, sub { my ($cb) = @_; diff --git a/t/psgi_search.t b/t/psgi_search.t index d7c9f183..289c34e7 100644 --- a/t/psgi_search.t +++ b/t/psgi_search.t @@ -1,12 +1,10 @@ #!perl -w # Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use v5.10.1; +use v5.12; use PublicInbox::TestCommon; use IO::Uncompress::Gunzip qw(gunzip); use PublicInbox::Eml; -use PublicInbox::Config; use PublicInbox::Inbox; my @mods = qw(DBD::SQLite Xapian HTTP::Request::Common Plack::Test URI::Escape Plack::Builder); @@ -53,10 +51,10 @@ To: git@vger.kernel.org EOF }; -my $cfgpfx = "publicinbox.test"; -my $cfg = PublicInbox::Config->new(\<{inboxdir} +my $cfg = cfg_new $tmpdir, <{inboxdir} EOF my $www = PublicInbox::WWW->new($cfg); test_psgi(sub { $www->call(@_) }, sub { diff --git a/t/psgi_text.t b/t/psgi_text.t index e4613945..25599dd9 100644 --- a/t/psgi_text.t +++ b/t/psgi_text.t @@ -1,8 +1,6 @@ -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use warnings; -use Test::More; +use v5.12; use PublicInbox::Eml; use PublicInbox::TestCommon; my ($tmpdir, $for_destroy) = tmpdir(); @@ -13,13 +11,12 @@ my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder); require_mods(@mods, 'IO::Uncompress::Gunzip'); use_ok $_ foreach @mods; use PublicInbox::Import; -use PublicInbox::Git; -use PublicInbox::Config; use_ok 'PublicInbox::WWW'; use_ok 'PublicInbox::WwwText'; -my $config = PublicInbox::Config->new(\<new($config); @@ -43,11 +40,7 @@ test_psgi(sub { $www->call(@_) }, sub { $res = $cb->($req); $content = $res->content; my $olen = $res->header('Content-Length'); - my $f = "$tmpdir/cfg"; - open my $fh, '>', $f or die; - print $fh $content or die; - close $fh or die; - my $cfg = PublicInbox::Config->new($f); + my $cfg = cfg_new $tmpdir, $content; is($cfg->{"$cfgpfx.address"}, $addr, 'got expected address in config'); $req->header('Accept-Encoding' => 'gzip'); diff --git a/t/watch_filter_rubylang.t b/t/watch_filter_rubylang.t index a6153e46..f72feb9f 100644 --- a/t/watch_filter_rubylang.t +++ b/t/watch_filter_rubylang.t @@ -1,11 +1,8 @@ -# Copyright (C) 2019-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use warnings; +use v5.12; use PublicInbox::TestCommon; -use Test::More; use PublicInbox::Eml; -use PublicInbox::Config; require_mods(qw(DBD::SQLite Xapian)); use_ok 'PublicInbox::Watch'; use_ok 'PublicInbox::Emergency'; @@ -25,7 +22,6 @@ SKIP: { for my $v (@v) { my @warn; local $SIG{__WARN__} = sub { push @warn, @_ }; - my $cfgpfx = "publicinbox.$v"; my $inboxdir = "$tmpdir/$v"; my $maildir = "$tmpdir/md-$v"; my $spamdir = "$tmpdir/spam-$v"; @@ -60,16 +56,16 @@ Date: Sat, 05 Jan 2019 04:19:17 +0000 spam EOF PublicInbox::Emergency->new($maildir)->prepare(\"$spam"); - - my $orig = <new(\$orig); + my $cfg = cfg_new $tmpdir, <lookup_name($v); $ibx->{-no_fsync} = 1; ok($ibx, 'found inbox by name'); @@ -102,7 +98,7 @@ EOF # ensure orderly destruction to avoid SQLite segfault: PublicInbox::DS->Reset; - $cfg = PublicInbox::Config->new(\$orig); + $cfg = PublicInbox::Config->new($cfg->{-f}); $ibx = $cfg->lookup_name($v); $ibx->{-no_fsync} = 1; is($ibx->search->reopen->mset('b:spam')->size, 0, 'spam removed'); diff --git a/t/watch_imap.t b/t/watch_imap.t index eeda29eb..26fd5330 100644 --- a/t/watch_imap.t +++ b/t/watch_imap.t @@ -1,16 +1,18 @@ -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use Test::More; -use PublicInbox::Config; +use v5.12; +use PublicInbox::TestCommon; # see t/imapd*.t for tests against a live IMAP server use_ok 'PublicInbox::Watch'; -my $cfg = PublicInbox::Config->new(\<new($cfg); is($watch->{imap}->{'imap://example.com/INBOX.a'}->[0]->{name}, 'i', diff --git a/t/watch_maildir.t b/t/watch_maildir.t index d0df1c1e..29e9bdc5 100644 --- a/t/watch_maildir.t +++ b/t/watch_maildir.t @@ -4,7 +4,6 @@ use v5.12; use PublicInbox::Eml; use Cwd; -use PublicInbox::Config; use PublicInbox::TestCommon; use PublicInbox::Import; my ($tmpdir, $for_destroy) = tmpdir(); @@ -13,7 +12,6 @@ my $maildir = "$tmpdir/md"; my $spamdir = "$tmpdir/spam"; use_ok 'PublicInbox::Watch'; use_ok 'PublicInbox::Emergency'; -my $cfgpfx = "publicinbox.test"; my $addr = 'test-public@example.com'; my $default_branch = PublicInbox::Import::default_branch; PublicInbox::Import::init_bare($git_dir); @@ -35,11 +33,13 @@ my $sem = PublicInbox::Emergency->new($spamdir); # create dirs { my @w; local $SIG{__WARN__} = sub { push @w, @_ }; - my $cfg = PublicInbox::Config->new(\<new($cfg); is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam'); @@ -47,10 +47,7 @@ EOF 'only got the spam folder to watch'); } -my $cfg_path = "$tmpdir/config"; -{ - open my $fh, '>', $cfg_path or BAIL_OUT $!; - print $fh <new($cfg_path); +my $cfg_path = $cfg->{-f}; PublicInbox::Watch->new($cfg)->scan('full'); my $git = PublicInbox::Git->new($git_dir); my @list = $git->qx('rev-list', $default_branch); diff --git a/t/watch_maildir_v2.t b/t/watch_maildir_v2.t index 38679836..fa86f7bf 100644 --- a/t/watch_maildir_v2.t +++ b/t/watch_maildir_v2.t @@ -1,10 +1,8 @@ -# Copyright (C) 2018-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use Test::More; +use v5.12; use PublicInbox::Eml; use Cwd; -use PublicInbox::Config; use PublicInbox::TestCommon; use PublicInbox::Import; require_git(2.6); @@ -38,13 +36,15 @@ ok(POSIX::mkfifo("$maildir/cur/fifo", 0777), my $sem = PublicInbox::Emergency->new($spamdir); # create dirs my $orig = <new(\$orig); +my $cfg = cfg_new $tmpdir, $orig; my $ibx = $cfg->lookup_name('test'); ok($ibx, 'found inbox by name'); $ibx->{-no_fsync} = 1; @@ -147,12 +147,13 @@ More majordomo info at http://vger.kernel.org/majordomo-info.html\n); my $v1pfx = "publicinbox.v1"; my $v1addr = 'v1-public@example.com'; PublicInbox::Import::init_bare($v1repo); - my $raw = <new(\$raw); my $both = < X-Mailing-List: no@example.com Message-ID: EOF - my $raw = $orig."$cfgpfx.listid=i.want.you.to.want.me\n"; PublicInbox::Emergency->new($maildir)->prepare(\$want); PublicInbox::Emergency->new($maildir)->prepare(\$do_not_want); - my $cfg = PublicInbox::Config->new(\$raw); + my $raw = <new($cfg)->scan('full'); $ibx = $cfg->lookup_name('test'); my $num = $ibx->mm->num_for('do.want@example.com'); ok(defined $num, 'List-ID matched for watch'); $num = $ibx->mm->num_for('do.not.want@example.com'); is($num, undef, 'unaccepted List-ID matched for watch'); - - $raw = $orig."$cfgpfx.watchheader=X-Mailing-List:no\@example.com\n"; - $cfg = PublicInbox::Config->new(\$raw); + $raw .= "\twatchheader = X-Mailing-List:no\@example.com\n"; + $cfg = cfg_new $tmpdir, $raw; PublicInbox::Watch->new($cfg)->scan('full'); $ibx = $cfg->lookup_name('test'); $num = $ibx->mm->num_for('do.not.want@example.com'); diff --git a/t/watch_multiple_headers.t b/t/watch_multiple_headers.t index 13dd3452..9585da2b 100644 --- a/t/watch_multiple_headers.t +++ b/t/watch_multiple_headers.t @@ -1,8 +1,6 @@ -# Copyright (C) 2020-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ -use strict; -use Test::More; -use PublicInbox::Config; +use v5.12; use PublicInbox::TestCommon; require_git(2.6); require_mods(qw(Xapian DBD::SQLite)); @@ -54,14 +52,15 @@ PublicInbox::Emergency->new($maildir)->prepare(\$msg_to); PublicInbox::Emergency->new($maildir)->prepare(\$msg_cc); PublicInbox::Emergency->new($maildir)->prepare(\$msg_none); -my $raw = <new(\$raw); + PublicInbox::Watch->new($cfg)->scan('full'); my $ibx = $cfg->lookup_name('test'); ok($ibx, 'found inbox by name'); -- cgit v1.2.3-24-ge0c7