]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
test_common: improve psgi test setup + loading
authorEric Wong <e@80x24.org>
Mon, 16 Sep 2024 21:03:00 +0000 (21:03 +0000)
committerEric Wong <e@80x24.org>
Tue, 17 Sep 2024 23:35:40 +0000 (23:35 +0000)
Since we have many PSGI tests nowadays, put in a `psgi' shortcut
like we do for many other components for `require_mods' to make
it easier to load a consistent set of modules.

We'll also cut down on `require_ok' and `use_ok' tests since
they should be limited to code maintained in our source tree,
not 3rd-party dependencies.

21 files changed:
lib/PublicInbox/TestCommon.pm
t/clone-coderepo.t
t/extindex-psgi.t
t/httpd-corner.t
t/httpd-https.t
t/httpd-unix.t
t/httpd.t
t/plack.t
t/psgi_attach.t
t/psgi_bad_mids.t
t/psgi_mount.t
t/psgi_multipart_not.t
t/psgi_scan_all.t
t/psgi_search.t
t/psgi_text.t
t/psgi_v2.t
t/solver_git.t
t/v2mirror.t
t/www_altid.t
t/www_listing.t
t/www_static.t

index 195dc3eba43b750e78138342c87fadd61c9f6e55..913aa7e9b2f77b9d24c3b30d2b13025167638fb7 100644 (file)
@@ -196,10 +196,11 @@ sub need_accept_filter ($) {
                skip "$af not loaded: kldload $af", 1;
 }
 
-sub require_mods {
+sub require_mods (@) {
        my @mods = @_;
        my $maybe = pop @mods if $mods[-1] =~ /\A[0-9]+\z/;
-       my @need;
+       my (@need, @use);
+
        while (my $mod = shift(@mods)) {
                if ($mod eq 'lei') {
                        require_git(2.6, $maybe ? $maybe : ());
@@ -212,6 +213,12 @@ sub require_mods {
                        push @mods, qw(Plack::Builder Plack::Util
                                HTTP::Date HTTP::Status);
                        next;
+               } elsif ($mod eq 'psgi') {
+                       my @m = qw(Plack::Test HTTP::Request::Common
+                               Plack::Builder);
+                       push @use, @m;
+                       push @mods, qw(Plack::Util), @m;
+                       next;
                } elsif ($mod eq '-imapd') {
                        push @mods, qw(Parse::RecDescent DBD::SQLite);
                        next;
@@ -263,7 +270,14 @@ sub require_mods {
                        $ENV{TEST_IPV4_ONLY} = 1 if !eval { $mod->VERSION($v) };
                }
        }
-       return unless @need;
+       unless (@need) {
+               for my $mod (@use) {
+                       my ($pkg) = caller(0);
+                       eval "package $pkg; $mod->import";
+                       xbail "$mod->import: $@" if $@;
+               }
+               return;
+       }
        my $m = join(', ', @need)." missing for $0";
        $m =~ s/\bEmail::MIME\b/Email::MIME (development purposes only)/;
        skip($m, $maybe) if $maybe;
@@ -941,8 +955,8 @@ sub test_httpd ($$;$$) {
        };
        for (qw(PI_CONFIG)) { $env->{$_} or BAIL_OUT "$_ unset" }
        SKIP: {
-               require_mods(qw(Plack::Test::ExternalServer LWP::UserAgent),
-                               $skip // 1);
+               require_mods qw(Plack::Test::ExternalServer LWP::UserAgent
+                               -httpd), $skip // 1;
                my $sock = tcp_server() or die;
                my ($out, $err) = map { "$env->{TMPDIR}/std$_.log" } qw(out err);
                my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
index c6180fc431c05922049a317de43fdf59e98e88b9..8986351ee3bf82bafa4288e3199e784fb3e14d2b 100644 (file)
@@ -8,9 +8,9 @@ use File::Temp;
 use File::Path qw(remove_tree);
 use PublicInbox::SHA qw(sha1_hex);
 use PublicInbox::IO;
-require_mods(qw(json Plack::Builder HTTP::Date HTTP::Status));
+require_mods qw(json psgi -httpd);
 require_git_http_backend;
-require_git '1.8.5';
+require_git v1.8.5;
 require_cmd 'curl';
 require_ok 'PublicInbox::LeiMirror';
 my ($tmpdir, $for_destroy) = tmpdir();
@@ -201,7 +201,6 @@ ok(-e "$tmpdir/dst/objstore", 'objstore created');
        xsys_e([qw(git clone -q), "${url}c.git", "$tmpdir/dst/c.git"]);
        SKIP: {
                require_mods(qw(Plack::Test::ExternalServer LWP::UserAgent), 1);
-               use_ok($_) for qw(HTTP::Request::Common);
                chop(my $uri = $url) eq '/' or xbail "BUG: no /";
                local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $uri;
                my %opt = (ua => LWP::UserAgent->new);
index 896c46ffc5c1c50c6f207ba6af3e05464cecf655..5fa0b99d1f8a82633d2fdf7df6f06c8eeffb0660 100644 (file)
@@ -8,9 +8,7 @@ use PublicInbox::Config;
 use File::Copy qw(cp);
 use IO::Handle ();
 require_git(2.6);
-require_mods(qw(json DBD::SQLite Xapian
-               HTTP::Request::Common Plack::Test URI::Escape Plack::Builder));
-use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
+require_mods qw(json DBD::SQLite Xapian psgi);
 use IO::Uncompress::Gunzip qw(gunzip);
 require PublicInbox::WWW;
 my ($ro_home, $cfg_path) = setup_public_inboxes;
index 7539573c9fb64472299e1885a0048f5d87894827..125610d60839b5ecfd47e2cb1f3411a6a66f8d1d 100644 (file)
@@ -7,7 +7,7 @@ use v5.12; use PublicInbox::TestCommon;
 use Time::HiRes qw(gettimeofday tv_interval);
 use autodie qw(getsockopt setsockopt);
 use PublicInbox::Spawn qw(spawn popen_rd);
-require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
+require_mods '-httpd';
 use PublicInbox::SHA qw(sha1_hex);
 use IO::Handle ();
 use IO::Socket::UNIX;
@@ -658,9 +658,7 @@ SKIP: {
 
 # ensure compatibility with other PSGI servers
 SKIP: {
-       require_mods(@zmods, qw(Plack::Test HTTP::Request::Common), 3);
-       use_ok 'HTTP::Request::Common';
-       use_ok 'Plack::Test';
+       require_mods @zmods, 'psgi', 3;
        STDERR->flush;
        open my $olderr, '>&', \*STDERR or die "dup stderr: $!";
        open my $tmperr, '+>', undef or die;
index bf0861238f15eb04f43832b0820fe147ab8f7a40..142a7a56dafe6e4bd4dfd8f44eb60e94f2d050ed 100644 (file)
@@ -6,7 +6,7 @@ use Socket qw(SOCK_STREAM IPPROTO_TCP SOL_SOCKET);
 use PublicInbox::TestCommon;
 use File::Copy qw(cp);
 # IO::Poll is part of the standard library, but distros may split them off...
-require_mods(qw(IO::Socket::SSL IO::Poll Plack::Util));
+require_mods qw(IO::Socket::SSL IO::Poll -httpd);
 my @certs = qw(certs/server-cert.pem certs/server-key.pem
        certs/server2-cert.pem certs/server2-key.pem);
 if (scalar(grep { -r $_ } @certs) != scalar(@certs)) {
index 0b620bd6eea41693aa71cf9c8525443e841fafe8..d4d50528ef076a50eb85e995e45ee53685917c7e 100644 (file)
@@ -8,7 +8,7 @@ use Errno qw(EADDRINUSE);
 use Cwd qw(abs_path);
 use Carp qw(croak);
 use autodie qw(close);
-require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
+require_mods qw(-httpd);
 use IO::Socket::UNIX;
 use POSIX qw(mkfifo);
 require PublicInbox::Sigfd;
index c0fbaa22be3e2bd8595e9aa44b7ed1b3b71795ea..39382b85f61de644600eabfcf67931f93a3ef13a 100644 (file)
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -1,12 +1,12 @@
 #!perl -w
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use v5.10.1;
 use PublicInbox::TestCommon;
 use PublicInbox::Eml;
 use Socket qw(IPPROTO_TCP SOL_SOCKET);
-require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
+require_mods '-httpd';
 require_git_http_backend;
 
 # FIXME: too much setup
index 07cab12a319fc5704b4f703c09719d105d3216fd..131ce43240e2957188d2350ae78837615027e6b3 100644 (file)
--- a/t/plack.t
+++ b/t/plack.t
@@ -5,9 +5,7 @@ use strict;
 use v5.10.1;
 use PublicInbox::TestCommon;
 my $psgi = "./examples/public-inbox.psgi";
-my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
-require_mods(@mods);
-foreach my $mod (@mods) { use_ok $mod; }
+require_mods 'psgi';
 ok(-f $psgi, "psgi example file found");
 my ($tmpdir, $for_destroy) = tmpdir();
 my $pfx = 'http://example.com/test';
index db551696d166d6b05001647ad77cf7506dffda11..6c769d85f4197129f77fde579fda852d9f0b6305 100644 (file)
@@ -4,9 +4,7 @@
 use strict;
 use v5.10.1;
 use PublicInbox::TestCommon;
-my @mods = qw(HTTP::Request::Common Plack::Builder Plack::Test URI::Escape);
-require_mods(@mods);
-use_ok $_ foreach @mods;
+require_mods 'psgi';
 use_ok 'PublicInbox::WWW';
 use PublicInbox::Config;
 use PublicInbox::Eml;
index ac0eb3c3d05ba4aeb9818adfd2bbfb4dc281d40a..56381a354d536402949ca2e10fe1554d787189bb 100644 (file)
@@ -4,11 +4,8 @@
 use v5.12;
 use PublicInbox::TestCommon;
 use PublicInbox::Eml;
-my @mods = qw(DBD::SQLite HTTP::Request::Common Plack::Test
-               URI::Escape Plack::Builder);
-require_git 2.6;
-require_mods(@mods);
-use_ok($_) for @mods;
+require_git v2.6;
+require_mods qw(DBD::SQLite psgi);
 use_ok 'PublicInbox::WWW';
 my $msgs = <<'';
 F1V5OR6NMF.3M649JTLO9IXD@tux.localdomain/hehe1"'<foo
index e43b9f2d8f77886b975ed426f608ffb48e2b749b..7117cda3090e1355f0b44b5c11e903ca7e074ba1 100644 (file)
@@ -6,10 +6,7 @@ use PublicInbox::Eml;
 use PublicInbox::TestCommon;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $v1dir = "$tmpdir/v1.git";
-my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape
-       Plack::Builder Plack::App::URLMap);
-require_mods(@mods);
-use_ok $_ foreach @mods;
+require_mods 'psgi';
 use_ok 'PublicInbox::WWW';
 my $ibx = create_inbox 'test', tmpdir => $v1dir, sub {
        my ($im, $ibx) = @_;
index e7c43abf2e059ebe6ef06390323400ee8fa9db0b..4b3b93fac4c1dbf4199f6bbf58673ab9ab92620c 100644 (file)
@@ -5,10 +5,7 @@ use v5.12;
 use PublicInbox::TestCommon;
 use PublicInbox::Eml;
 require_git 2.6;
-my @mods = qw(DBD::SQLite Xapian HTTP::Request::Common
-              Plack::Test URI::Escape Plack::Builder Plack::Test);
-require_mods(@mods);
-use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
+require_mods qw(DBD::SQLite Xapian psgi);
 use_ok 'PublicInbox::WWW';
 my $ibx = create_inbox 'v2', version => 2, sub {
        my ($im) = @_;
index 4c28b553dc2289d62a7e318783b34f0677fadace..7c697854060f2f25aae88aa53041b5dbc0fd4597 100644 (file)
@@ -4,11 +4,10 @@
 use v5.12;
 use PublicInbox::TestCommon;
 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));
-$_->import for @use;
+require_mods qw(DBD::SQLite psgi);
+use_ok 'PublicInbox::WWW';
 my $cfgtxt = '';
 foreach my $i (1..2) {
        my $ibx = create_inbox "test-$i", version => 2, indexlevel => 'basic',
index 759dab781c9b6ba75c5f1065586652de1649a47e..8d47356e7427312a3360c60d05344be170e35982 100644 (file)
@@ -6,10 +6,7 @@ use PublicInbox::TestCommon;
 use IO::Uncompress::Gunzip qw(gunzip);
 use PublicInbox::Eml;
 use PublicInbox::Inbox;
-my @mods = qw(DBD::SQLite Xapian HTTP::Request::Common Plack::Test
-               URI::Escape Plack::Builder);
-require_mods(@mods);
-use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
+require_mods qw(DBD::SQLite Xapian psgi);
 use_ok 'PublicInbox::WWW';
 use_ok 'PublicInbox::SearchIdx';
 my ($tmpdir, $for_destroy) = tmpdir();
index b8b1bc48a4802e9fc6026bf4c4d6808744574d38..deb41f9f63233155e5ad514bffa55779e87a3103 100644 (file)
@@ -8,8 +8,7 @@ my ($tmpdir, $for_destroy) = tmpdir();
 my $maindir = "$tmpdir/main.git";
 my $addr = 'test-public@example.com';
 my $cfgpfx = "publicinbox.test";
-my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape Plack::Builder);
-use_ok $_ foreach @mods;
+require_mods qw(psgi);
 use PublicInbox::Import;
 use_ok 'PublicInbox::WWW';
 use_ok 'PublicInbox::WwwText';
index 2b678fd8e96f00cf623e1e6bc5e1a23014fa3d79..2027037dc42026957dcec2660892a10e3d7a86f7 100644 (file)
@@ -10,9 +10,7 @@ use PublicInbox::Eml;
 use PublicInbox::Config;
 use PublicInbox::MID qw(mids);
 use autodie qw(kill rename);
-require_mods(qw(DBD::SQLite Xapian HTTP::Request::Common Plack::Test
-               URI::Escape Plack::Builder HTTP::Date));
-use_ok($_) for (qw(HTTP::Request::Common Plack::Test));
+require_mods qw(DBD::SQLite Xapian psgi);
 use_ok 'PublicInbox::WWW';
 my ($tmpdir, $for_destroy) = tmpdir();
 my $enc_dup = 'ref-20150309094050.GO3427@x1.example';
index db6729040c96eb19b8d274ca4d10c0949e6059d9..694e2671feca65f7763fd675e976b2220106512f 100644 (file)
@@ -7,7 +7,7 @@ use Cwd qw(abs_path);
 require_git v2.6;
 use PublicInbox::ContentHash qw(git_sha);
 use PublicInbox::Spawn qw(run_qx);
-require_mods(qw(DBD::SQLite Xapian URI::Escape));
+require_mods(qw(DBD::SQLite Xapian));
 require PublicInbox::SolverGit;
 my $rdr = { 2 => \(my $null) };
 my $git_dir = xqx([qw(git rev-parse --git-common-dir)], undef, $rdr);
@@ -208,10 +208,8 @@ my $hinted = $res;
 shift @$res; shift @$hinted;
 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
 
-my @psgi = qw(HTTP::Request::Common Plack::Test Plack::Builder);
 SKIP: {
-       require_mods(@psgi, 7 + scalar(@psgi));
-       use_ok($_) for @psgi;
+       require_mods qw(psgi 1);
        require PublicInbox::WWW;
        my $binfoo = "$ibx->{inboxdir}/binfoo.git";
        my $l = "$ibx->{inboxdir}/inbox.lock";
index b88241826984d78cd352e9d8102d3443aa602359..dd4d9a6ca1a3d106d5fc0637e1e9f499fb71d5b9 100644 (file)
@@ -13,8 +13,7 @@ local $ENV{HOME} = abs_path('t');
 use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
 
 # Integration tests for HTTP cloning + mirroring
-require_mods(qw(Plack::Util Plack::Builder
-               HTTP::Date HTTP::Status Xapian DBD::SQLite));
+require_mods qw(psgi -httpd Xapian DBD::SQLite);
 use_ok 'PublicInbox::V2Writable';
 use PublicInbox::InboxWritable;
 use PublicInbox::Eml;
index 6f0f0c61ade6c82235f4bc864e1f86095a3611b2..c06e34d953202a29babab603bac26eef7b5b3377 100644 (file)
@@ -5,12 +5,12 @@ use strict; use v5.10.1; use PublicInbox::TestCommon;
 use PublicInbox::Config;
 use PublicInbox::Spawn qw(spawn);
 require_cmd('sqlite3');
-require_mods(qw(DBD::SQLite HTTP::Request::Common Plack::Test URI::Escape
-       Plack::Builder IO::Uncompress::Gunzip Xapian));
+require_mods qw(DBD::SQLite psgi Xapian);
 use_ok($_) for qw(Plack::Test HTTP::Request::Common);
 require_ok 'PublicInbox::Msgmap';
 require_ok 'PublicInbox::AltId';
 require_ok 'PublicInbox::WWW';
+require IO::Uncompress::Gunzip;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $aid = 'xyz';
 my $cfgpath;
index e0d21161882a905c9afd2caad77d7d82564fc614..ff75655dfc4a8ff4bb230f473e0f9b5fc07446c9 100644 (file)
@@ -5,7 +5,7 @@
 use v5.12; use PublicInbox::TestCommon;
 use PublicInbox::Import;
 use IO::Uncompress::Gunzip qw(gunzip);
-require_mods(qw(json URI::Escape Plack::Builder HTTP::Tiny));
+require_mods qw(json URI::Escape psgi -httpd HTTP::Tiny);
 my $curl = require_cmd 'curl';
 require PublicInbox::WwwListing;
 require PublicInbox::ManifestJsGz;
index 3281751c5cdb95dc79a40cda2b5f62ddaccf1762..83a96a5e6a94d9905f6edc2127d3358108daef39 100644 (file)
@@ -6,8 +6,8 @@ use Test::More;
 use PublicInbox::TestCommon;
 my ($tmpdir, $for_destroy) = tmpdir();
 my @mods = qw(HTTP::Request::Common Plack::Test URI::Escape);
-require_mods(@mods, 'IO::Uncompress::Gunzip');
-use_ok $_ foreach @mods;
+require_mods qw(psgi);
+require IO::Uncompress::Gunzip;
 use_ok 'PublicInbox::WwwStatic';
 
 my $app = sub {