]> Sergey Matveev's repositories - public-inbox.git/commitdiff
tests: make require_git and require_cmd easier-to-use
authorEric Wong <e@80x24.org>
Mon, 30 Jan 2023 22:50:07 +0000 (22:50 +0000)
committerEric Wong <e@80x24.org>
Tue, 31 Jan 2023 00:27:05 +0000 (00:27 +0000)
We'll rely on defined(wantarray) to implicitly skip subtests,
and memoize these to reduce syscalls, since tests should
be short-lived enough to not be affected by new installations or
removals of git/xapian-compact/curl/etc...

15 files changed:
lib/PublicInbox/TestCommon.pm
t/admin.t
t/config.t
t/convert-compact.t
t/git.t
t/imapd.t
t/import.t
t/index-git-times.t
t/init.t
t/nntpd.t
t/v2mirror.t
xt/git-http-backend.t
xt/httpd-async-stream.t
xt/imapd-mbsync-oimap.t
xt/pop3d-mpop.t

index b36c71a64cded042ea4d81449359ccbb5454fc93..1fe7931e13f241e1cf0a6e20c0af3960fcb2a270 100644 (file)
@@ -93,31 +93,35 @@ sub tcp_connect {
 }
 
 sub require_cmd ($;$) {
-       my ($cmd, $maybe) = @_;
+       my ($cmd, $nr) = @_;
        require PublicInbox::Spawn;
-       my $bin = PublicInbox::Spawn::which($cmd);
+       state %CACHE;
+       my $bin = $CACHE{$cmd} //= PublicInbox::Spawn::which($cmd);
        return $bin if $bin;
-       $maybe ? 0 : plan(skip_all => "$cmd missing from PATH for $0");
+       return plan(skip_all => "$cmd missing from PATH for $0") if !$nr;
+       defined(wantarray) ? undef : skip("$cmd missing, skipping $nr tests")
 }
 
-sub have_xapian_compact () {
-       require_cmd($ENV{XAPIAN_COMPACT} || 'xapian-compact', 1);
+sub have_xapian_compact (;$) {
+       require_cmd($ENV{XAPIAN_COMPACT} || 'xapian-compact', @_ ? $_[0] : ());
 }
 
 sub require_git ($;$) {
-       my ($req, $maybe) = @_;
-       my ($req_maj, $req_min, $req_sub) = split(/\./, $req);
-       my ($cur_maj, $cur_min, $cur_sub) = (xqx([qw(git --version)])
-                       =~ /version (\d+)\.(\d+)(?:\.(\d+))?/);
+       my ($req, $nr) = @_;
+       state ($cur_int, $cur_ver);
+       $cur_int //= do {
+               chomp($cur_ver = xqx([qw(git --version)]));
+               my @v = ($cur_ver =~ /version (\d+)\.(\d+)(?:\.(\d+))?/);
+               ($v[0] << 24) | ($v[1] << 16) | ($v[2] // 0);
+       };
 
+       my ($req_maj, $req_min, $req_sub) = split(/\./, $req);
        my $req_int = ($req_maj << 24) | ($req_min << 16) | ($req_sub // 0);
-       my $cur_int = ($cur_maj << 24) | ($cur_min << 16) | ($cur_sub // 0);
-       if ($cur_int < $req_int) {
-               return 0 if $maybe;
-               plan skip_all =>
-                       "git $req+ required, have $cur_maj.$cur_min.$cur_sub";
-       }
-       1;
+
+       return 1 if $cur_int >= $req_int;
+       return plan skip_all => "git $req+ required, have $cur_ver" if !$nr;
+       defined(wantarray) ? undef :
+               skip("git $req+ required (have $cur_ver), skipping $nr tests")
 }
 
 my %IPv6_VERSION = (
@@ -570,7 +574,7 @@ SKIP: {
        my $test_opt = shift // {};
        local $lei_cwdfh;
        opendir $lei_cwdfh, '.' or xbail "opendir .: $!";
-       require_git(2.6, 1) or skip('git 2.6+ required for lei test', 2);
+       require_git(2.6, 1);
        my $mods = $test_opt->{mods} // [ 'lei' ];
        require_mods(@$mods, 2);
 
index 8d09bfc1102c9b907051851319121e91822aa9c7..20e3deb7956bc1d61dc98084b81d68de68541bab 100644 (file)
--- a/t/admin.t
+++ b/t/admin.t
@@ -1,5 +1,5 @@
 #!perl -w
-# Copyright (C) 2019-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;
@@ -13,7 +13,7 @@ my ($res, $err, $v);
 my $v2ibx;
 SKIP: {
        require_mods(qw(DBD::SQLite), 5);
-       require_git(2.6, 1) or skip 5, 'git too old';
+       require_git(2.6, 5);
        $v2ibx = create_inbox 'v2', indexlevel => 'basic', version => 2,
                                -no_gc => 1, sub {
                my ($v2w, $ibx) = @_;
index 7e753cbe1bf2d5ad7a7afaf7389c040a4a73da5f..ba83e63f669eae842f1d146e1a66d4b8f001bff5 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2014-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;
@@ -245,8 +245,7 @@ EOF
 
 SKIP: {
        # XXX wildcard match requires git 2.26+
-       require_git('1.8.5', 2) or
-               skip 'git 1.8.5+ required for --url-match', 2;
+       require_git('1.8.5', 2);
        my $f = "$tmpdir/urlmatch";
        open my $fh, '>', $f or BAIL_OUT $!;
        print $fh <<EOF or BAIL_OUT $!;
index def09567fefdcdabf8d48100dd5ef1841d2700dd..bad6560e9cff2344d2007a0eb2d85084ff9bd545 100644 (file)
@@ -1,5 +1,5 @@
 #!perl -w
-# Copyright (C) 2018-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;
@@ -8,8 +8,7 @@ use PublicInbox::TestCommon;
 use PublicInbox::Import;
 require_git(2.6);
 require_mods(qw(DBD::SQLite Search::Xapian));
-have_xapian_compact or
-       plan skip_all => 'xapian-compact missing for '.__FILE__;
+have_xapian_compact;
 my ($tmpdir, $for_destroy) = tmpdir();
 my $ibx = create_inbox 'v1', indexlevel => 'medium', tmpdir => "$tmpdir/v1",
                pre_cb => sub {
diff --git a/t/git.t b/t/git.t
index d8957e422fd31083815fa46f6df9dfd166fc0d5b..dfa5eab21f41a2e07805088bcc1bdc4120a29ca6 100644 (file)
--- a/t/git.t
+++ b/t/git.t
@@ -1,7 +1,8 @@
+#!perl -w
 # Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
-use Test::More;
+use v5.10.1;
 use PublicInbox::TestCommon;
 my ($dir, $for_destroy) = tmpdir();
 use PublicInbox::Import;
@@ -134,7 +135,7 @@ if (1) {
 }
 
 SKIP: {
-       require_git(2.6, 7) or skip('need git 2.6+ for --batch-all-objects', 7);
+       require_git(2.6, 7);
        my ($alt, $alt_obj) = tmpdir();
        my $hash_obj = [ 'git', "--git-dir=$alt", qw(hash-object -w --stdin) ];
        PublicInbox::Import::init_bare($alt);
index cbd6c1b9961dec4c9c58484b108a3174c642bcb0..c7dc01a57ea3975c6ae8dca7f99fe01d865f0567 100644 (file)
--- a/t/imapd.t
+++ b/t/imapd.t
@@ -3,7 +3,7 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # end-to-end IMAP tests, see unit tests in t/imap.t, too
 use strict;
-use Test::More;
+use v5.10.1;
 use Time::HiRes ();
 use PublicInbox::TestCommon;
 use PublicInbox::Config;
@@ -438,8 +438,7 @@ ok($mic->logout, 'logged out');
 
 SKIP: {
        use_ok 'PublicInbox::InboxIdle';
-       require_git('1.8.5', 1) or
-               skip('git 1.8.5+ needed for --urlmatch', 4);
+       require_git '1.8.5', 4;
        my $old_env = { HOME => $ENV{HOME} };
        my $home = "$tmpdir/watch_home";
        mkdir $home or BAIL_OUT $!;
index 4ba740224aa9f1f4e025ab1f9e8bb7eda3e223ec..f1d61dae9b4f1c95208a69430098af5f0b6e0578 100644 (file)
@@ -1,8 +1,8 @@
+#!perl -w
 # Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use v5.10.1;
 use strict;
-use warnings;
-use Test::More;
 use PublicInbox::Eml;
 use PublicInbox::Smsg;
 use PublicInbox::Git;
@@ -26,10 +26,11 @@ hello world
 EOF
 
 my $v2 = require_git(2.6, 1);
-my $smsg = bless {}, 'PublicInbox::Smsg' if $v2;
+my $smsg = $v2 ? bless({}, 'PublicInbox::Smsg') : undef;
 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
 
-if ($v2) {
+SKIP: {
+       skip 'git 2.6+ required', 3 if !$v2;
        like($smsg->{blob}, qr/\A[a-f0-9]{40,64}\z/, 'got last object_id');
        my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
        open my $in, '+<', undef or BAIL_OUT "open(+<): $!";
index 52173396b1eaa78074d6bbd13ac1f80985d84ea6..ffe9223cf94db88091387309beba63fd98551a0f 100644 (file)
@@ -1,9 +1,8 @@
 #!perl -w
-# Copyright (C) 2020-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 Test::More;
 use PublicInbox::TestCommon;
 use PublicInbox::Config;
 use PublicInbox::Admin;
@@ -74,7 +73,7 @@ my $smsg;
        is($res->[0]->{ds}, $smsg->{ds}, 'Xapian search on datestamp');
 }
 SKIP: {
-       require_git(2.6, 1) or skip('git 2.6+ required for v2', 10);
+       require_git(2.6, 10);
        my $v2dir = "$tmpdir/v2";
        run_script(['-convert', $v1dir, $v2dir]) or die 'v2 conversion failed';
 
index 460c83f3dd604e2c2f48cc91cfbd7dc425aee416..46258e45df4c733d7f52c164331ac3158d64c623 100644 (file)
--- a/t/init.t
+++ b/t/init.t
@@ -1,8 +1,8 @@
-# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
+#!perl -w
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
-use warnings;
-use Test::More;
+use v5.10.1;
 use PublicInbox::Config;
 use PublicInbox::TestCommon;
 use PublicInbox::Admin;
@@ -117,7 +117,7 @@ sub quiet_fail {
 
 SKIP: {
        require_mods(qw(DBD::SQLite Search::Xapian), 2);
-       require_git(2.6, 1) or skip "git 2.6+ required", 2;
+       require_git(2.6, 2);
        use_ok 'PublicInbox::Msgmap';
        local $ENV{PI_DIR} = "$tmpdir/.public-inbox/";
        local $ENV{PI_EMERGENCY} = "$tmpdir/.public-inbox/emergency";
index 84a605741c10f218946d60c1593a83819927344a..dbbc37b8d3e5d43a5e7180b7fd6d939ba8ed6732 100644 (file)
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -413,7 +413,7 @@ sub test_watch {
        use_ok 'PublicInbox::Watch';
        use_ok 'PublicInbox::InboxIdle';
        use_ok 'PublicInbox::Config';
-       require_git('1.8.5', 1) or skip('git 1.8.5+ needed for --urlmatch', 4);
+       require_git('1.8.5', 4);
        my $old_env = { HOME => $ENV{HOME} };
        my $home = "$tmpdir/watch_home";
        mkdir $home or BAIL_OUT $!;
index f9074e45e6634e34aa9485079de0cced946e15f6..c1c66d45a41d4c4c8f2e2ca1cf048ad5b372ddc3 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-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;
@@ -330,7 +330,7 @@ SKIP: {
        require_mods('Email::MIME', 1); # for legacy revision
        # using plackup to test old PublicInbox::WWW since -httpd from
        # back then relied on some packages we no longer depend on
-       my $plackup = which('plackup') or skip('no plackup in path', 1);
+       my $plackup = require_cmd('plackup', 1) or skip('no plackup in path', 1);
        require PublicInbox::Lock;
        chomp $oldrev;
        my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
index 1f3ba0633429856e478fa28f90749e0cbc4dcf00..d78fe79f585ffa20167913ff40c92d0c902b0812 100644 (file)
@@ -1,14 +1,13 @@
+#!perl -w
 # Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Ensure buffering behavior in -httpd doesn't cause runaway memory use
 # or data corruption
 use strict;
-use warnings;
-use Test::More;
+use v5.10.1;
 use POSIX qw(setsid);
 use PublicInbox::TestCommon;
-use PublicInbox::Spawn qw(which);
 
 my $git_dir = $ENV{GIANT_GIT_DIR};
 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
@@ -77,8 +76,7 @@ SKIP: { # make sure Last-Modified + If-Modified-Since works with curl
        my $nr = 6;
        skip 'no description', $nr unless -f "$git_dir/description";
        my $mtime = (stat(_))[9];
-       my $curl = which('curl');
-       skip 'curl(1) not found', $nr unless $curl;
+       my $curl = require_cmd('curl', 1) or skip 'curl(1) not found', $nr;
        my $url = "http://$host:$port/description";
        my $dst = "$tmpdir/desc";
        is(xsys($curl, qw(-RsSf), '-o', $dst, $url), 0, 'curl -R');
index c7039f3e86f31ad93f78933fe724a25f442dc734..0658c691c826131a5080acba36ace71858591f86 100644 (file)
@@ -1,17 +1,17 @@
 #!perl -w
-# Copyright (C) 2020-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>
 # Expensive test to validate compression and TLS.
 use strict;
-use Test::More;
+use v5.10.1;
 use PublicInbox::TestCommon;
 use PublicInbox::DS qw(now);
-use PublicInbox::Spawn qw(which popen_rd);
+use PublicInbox::Spawn qw(popen_rd);
 use Digest::MD5;
 use POSIX qw(_exit);
 my $inboxdir = $ENV{GIANT_INBOX_DIR};
 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inboxdir;
-my $curl = which('curl') or plan skip_all => "curl(1) missing for $0";
+my $curl = require_cmd('curl');
 my ($tmpdir, $for_destroy) = tmpdir();
 require_mods(qw(DBD::SQLite));
 my $JOBS = $ENV{TEST_JOBS} // 4;
index 0baf5b4cc073d0421c9f030eb5b4be7f245311c4..b02811055e5336a8e4124c08f636a8a15aeaf639 100644 (file)
@@ -1,12 +1,12 @@
 #!perl -w
-# Copyright (C) 2020-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>
 # ensure mbsync and offlineimap compatibility
 use strict;
 use v5.10.1;
 use File::Path qw(mkpath);
 use PublicInbox::TestCommon;
-use PublicInbox::Spawn qw(which spawn);
+use PublicInbox::Spawn qw(spawn);
 require_mods(qw(-imapd));
 my $inboxdir = $ENV{GIANT_INBOX_DIR};
 (defined($inboxdir) && -d $inboxdir) or
@@ -42,7 +42,8 @@ my %pids;
 
 SKIP: {
        mkpath([map { "$tmpdir/oimapdir/$_" } qw(cur new tmp)]);
-       my $oimap = which('offlineimap') or skip 'no offlineimap(1)', 1;
+       my $oimap = require_cmd('offlineimap', 1) or
+               skip 'no offlineimap(1)', 1;
        open my $fh, '>', "$tmpdir/.offlineimaprc" or BAIL_OUT "open: $!";
        print $fh <<EOF or BAIL_OUT "print: $!";
 [general]
@@ -78,7 +79,7 @@ EOF
 
 SKIP: {
        mkpath([map { "$tmpdir/mbsyncdir/test/$_" } qw(cur new tmp)]);
-       my $mbsync = which('mbsync') or skip 'no mbsync(1)', 1;
+       my $mbsync = require_cmd('mbsync', 1) or skip 'no mbsync(1)', 1;
        open my $fh, '>', "$tmpdir/.mbsyncrc" or BAIL_OUT "open: $!";
        print $fh <<EOF or BAIL_OUT "print: $!";
 Create Slave
index 8648b95386f5d105a6da73eca0914024d1be05af..fc82bc6ba0ca78e10089561a00d86ece9f101bfd 100644 (file)
@@ -5,12 +5,13 @@
 use v5.12;
 use File::Path qw(make_path);
 use PublicInbox::TestCommon;
-use PublicInbox::Spawn qw(which spawn);
+use PublicInbox::Spawn qw(spawn);
 my $inboxdir = $ENV{GIANT_INBOX_DIR};
 (defined($inboxdir) && -d $inboxdir) or
        plan skip_all => "GIANT_INBOX_DIR not defined for $0";
 plan skip_all => "bad characters in $inboxdir" if $inboxdir =~ m![^\w\.\-/]!;
-my $uuidgen = which('uuidgen') or plan skip_all => 'uuidgen(1) missing';
+my $uuidgen = require_cmd('uuidgen');
+my $mpop = require_cmd('mpop');
 require_mods(qw(DBD::SQLite));
 require_git('2.6'); # for v2
 require_mods(qw(File::FcntlLock)) if $^O !~ /\A(?:linux|freebsd)\z/;
@@ -41,8 +42,7 @@ chomp(my $uuid = xqx([$uuidgen]));
 make_path("$tmpdir/home/.config/mpop",
        map { "$tmpdir/md/$_" } qw(new cur tmp));
 
-SKIP: {
-       my $mpop = which('mpop') or skip('mpop(1) missing', 1);
+{
        open my $fh, '>', "$tmpdir/home/.config/mpop/config"
                or xbail "open $!";
        chmod 0600, $fh;