about summary refs log tree commit homepage
path: root/lib/PublicInbox/TestCommon.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-01-30 22:50:07 +0000
committerEric Wong <e@80x24.org>2023-01-31 00:27:05 +0000
commit5dbb060037ce3e3a3ba0812baa90c80eeee762af (patch)
tree4c61aef7f5822ede45c8c352320ed8bc5b7492b8 /lib/PublicInbox/TestCommon.pm
parent155ee9cdaffd6232ef3c774d1a9f61d708fc8d6e (diff)
downloadpublic-inbox-5dbb060037ce3e3a3ba0812baa90c80eeee762af.tar.gz
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...
Diffstat (limited to 'lib/PublicInbox/TestCommon.pm')
-rw-r--r--lib/PublicInbox/TestCommon.pm38
1 files changed, 21 insertions, 17 deletions
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index b36c71a6..1fe7931e 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -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);