lib/PublicInbox/TestCommon.pm | 38 +++++++++++++++++++++-----------------
t/admin.t | 4 ++--
t/config.t | 5 ++---
t/convert-compact.t | 5 ++---
t/git.t | 5 +++--
t/imapd.t | 5 ++---
t/import.t | 9 +++++----
t/index-git-times.t | 5 ++---
t/init.t | 8 ++++----
t/nntpd.t | 2 +-
t/v2mirror.t | 4 ++--
xt/git-http-backend.t | 8 +++-----
xt/httpd-async-stream.t | 8 ++++----
xt/imapd-mbsync-oimap.t | 9 +++++----
xt/pop3d-mpop.t | 8 ++++----
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index b36c71a64cded042ea4d81449359ccbb5454fc93..1fe7931e13f241e1cf0a6e20c0af3960fcb2a270 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -93,31 +93,35 @@ $s;
}
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 @@ my ($cb) = pop @_;
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);
diff --git a/t/admin.t b/t/admin.t
index 8d09bfc1102c9b907051851319121e91822aa9c7..20e3deb7956bc1d61dc98084b81d68de68541bab 100644
--- a/t/admin.t
+++ b/t/admin.t
@@ -1,5 +1,5 @@
#!perl -w
-# Copyright (C) 2019-2021 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
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) = @_;
diff --git a/t/config.t b/t/config.t
index 7e753cbe1bf2d5ad7a7afaf7389c040a4a73da5f..ba83e63f669eae842f1d146e1a66d4b8f001bff5 100644
--- a/t/config.t
+++ b/t/config.t
@@ -1,4 +1,4 @@
-# Copyright (C) 2014-2021 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
use strict;
use v5.10.1;
@@ -245,8 +245,7 @@ }
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 <
+# Copyright (C) all contributors
# License: AGPL-3.0+
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
--- a/t/git.t
+++ b/t/git.t
@@ -1,7 +1,8 @@
+#!perl -w
# Copyright (C) all contributors
# License: AGPL-3.0+
use strict;
-use Test::More;
+use v5.10.1;
use PublicInbox::TestCommon;
my ($dir, $for_destroy) = tmpdir();
use PublicInbox::Import;
@@ -134,7 +135,7 @@ isnt($?, 0, '$? set on failure: '.$?);
}
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);
diff --git a/t/imapd.t b/t/imapd.t
index cbd6c1b9961dec4c9c58484b108a3174c642bcb0..c7dc01a57ea3975c6ae8dca7f99fe01d865f0567 100644
--- a/t/imapd.t
+++ b/t/imapd.t
@@ -3,7 +3,7 @@ # Copyright (C) all contributors
# License: AGPL-3.0+
# 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 @@ }
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 $!;
diff --git a/t/import.t b/t/import.t
index 4ba740224aa9f1f4e025ab1f9e8bb7eda3e223ec..f1d61dae9b4f1c95208a69430098af5f0b6e0578 100644
--- a/t/import.t
+++ b/t/import.t
@@ -1,8 +1,8 @@
+#!perl -w
# Copyright (C) all contributors
# License: AGPL-3.0+
+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(+<): $!";
diff --git a/t/index-git-times.t b/t/index-git-times.t
index 52173396b1eaa78074d6bbd13ac1f80985d84ea6..ffe9223cf94db88091387309beba63fd98551a0f 100644
--- a/t/index-git-times.t
+++ b/t/index-git-times.t
@@ -1,9 +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 Test::More;
use PublicInbox::TestCommon;
use PublicInbox::Config;
use PublicInbox::Admin;
@@ -74,7 +73,7 @@ is(scalar @$res, 1, 'got one result for d:');
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';
diff --git a/t/init.t b/t/init.t
index 460c83f3dd604e2c2f48cc91cfbd7dc425aee416..46258e45df4c733d7f52c164331ac3158d64c623 100644
--- a/t/init.t
+++ b/t/init.t
@@ -1,8 +1,8 @@
-# Copyright (C) 2014-2021 all contributors
+#!perl -w
+# Copyright (C) all contributors
# License: AGPL-3.0+
use strict;
-use warnings;
-use Test::More;
+use v5.10.1;
use PublicInbox::Config;
use PublicInbox::TestCommon;
use PublicInbox::Admin;
@@ -117,7 +117,7 @@ }
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";
diff --git a/t/nntpd.t b/t/nntpd.t
index 84a605741c10f218946d60c1593a83819927344a..dbbc37b8d3e5d43a5e7180b7fd6d939ba8ed6732 100644
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -413,7 +413,7 @@ my ($tmpdir, $host_port, $group) = @_;
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 $!;
diff --git a/t/v2mirror.t b/t/v2mirror.t
index f9074e45e6634e34aa9485079de0cced946e15f6..c1c66d45a41d4c4c8f2e2ca1cf048ad5b372ddc3 100644
--- a/t/v2mirror.t
+++ b/t/v2mirror.t
@@ -1,4 +1,4 @@
-# Copyright (C) 2018-2021 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
use strict;
use v5.10.1;
@@ -330,7 +330,7 @@ skip("no detected public-inbox GIT_DIR ($err)", 1) if $?;
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!);
diff --git a/xt/git-http-backend.t b/xt/git-http-backend.t
index 1f3ba0633429856e478fa28f90749e0cbc4dcf00..d78fe79f585ffa20167913ff40c92d0c902b0812 100644
--- a/xt/git-http-backend.t
+++ b/xt/git-http-backend.t
@@ -1,14 +1,13 @@
+#!perl -w
# Copyright (C) all contributors
# License: AGPL-3.0+
#
# 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');
diff --git a/xt/httpd-async-stream.t b/xt/httpd-async-stream.t
index c7039f3e86f31ad93f78933fe724a25f442dc734..0658c691c826131a5080acba36ace71858591f86 100644
--- a/xt/httpd-async-stream.t
+++ b/xt/httpd-async-stream.t
@@ -1,17 +1,17 @@
#!perl -w
-# Copyright (C) 2020-2021 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
# 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;
diff --git a/xt/imapd-mbsync-oimap.t b/xt/imapd-mbsync-oimap.t
index 0baf5b4cc073d0421c9f030eb5b4be7f245311c4..b02811055e5336a8e4124c08f636a8a15aeaf639 100644
--- a/xt/imapd-mbsync-oimap.t
+++ b/xt/imapd-mbsync-oimap.t
@@ -1,12 +1,12 @@
#!perl -w
-# Copyright (C) 2020-2021 all contributors
+# Copyright (C) all contributors
# License: AGPL-3.0+
# 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 <', "$tmpdir/.mbsyncrc" or BAIL_OUT "open: $!";
print $fh < "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 @@
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;