From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/6] tests: add require_cmd, require curl when needed
Date: Mon, 13 Sep 2021 20:53:50 +0000 [thread overview]
Message-ID: <20210913205355.13002-2-e@80x24.org> (raw)
In-Reply-To: <20210913205355.13002-1-e@80x24.org>
t/v2mirror.t and t/lei-mirror.t are now skipped when curl
is missing (instead of failing in appropriate places).
A bunch of which() checks are updated to use require_cmd
to avoid explicitly loading Spawn.
---
lib/PublicInbox/TestCommon.pm | 20 +++++++++++++-------
t/ds-leak.t | 9 +++------
t/hl_mod.t | 9 ++-------
t/httpd-corner.t | 11 ++++-------
t/imapd.t | 1 -
t/lei-daemon.t | 1 -
t/lei-externals.t | 8 ++++----
t/lei-import-http.t | 3 +--
t/lei-mirror.t | 1 +
t/lei-q-remote-import.t | 1 +
t/lei.t | 4 +---
t/nntpd.t | 7 ++-----
t/nodatacow.t | 10 ++++------
t/solver_git.t | 4 ++--
t/v2mirror.t | 6 ++----
t/v2reindex.t | 8 ++------
t/www_altid.t | 8 +++-----
t/www_listing.t | 8 ++------
18 files changed, 47 insertions(+), 72 deletions(-)
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 628beaf1..d8346673 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -18,7 +18,7 @@ BEGIN {
run_script start_script key2sub xsys xsys_e xqx eml_load tick
have_xapian_compact json_utf8 setup_public_inboxes create_inbox
tcp_host_port test_lei lei lei_ok $lei_out $lei_err $lei_opt
- test_httpd xbail);
+ test_httpd xbail require_cmd);
require Test::More;
my @methods = grep(!/\W/, @Test::More::EXPORT);
eval(join('', map { "*$_=\\&Test::More::$_;" } @methods));
@@ -87,6 +87,18 @@ sub tcp_connect {
$s;
}
+sub require_cmd ($;$) {
+ my ($cmd, $maybe) = @_;
+ require PublicInbox::Spawn;
+ my $bin = PublicInbox::Spawn::which($cmd);
+ return $bin if $bin;
+ $maybe ? 0 : plan(skip_all => "$cmd missing from PATH for $0");
+}
+
+sub have_xapian_compact () {
+ require_cmd($ENV{XAPIAN_COMPACT} || 'xapian-compact', 1);
+}
+
sub require_git ($;$) {
my ($req, $maybe) = @_;
my ($req_maj, $req_min, $req_sub) = split(/\./, $req);
@@ -467,12 +479,6 @@ sub start_script {
PublicInboxTestProcess->new($pid, $tail_pid);
}
-sub have_xapian_compact () {
- require PublicInbox::Spawn;
- # $ENV{XAPIAN_COMPACT} is used by PublicInbox/Xapcmd.pm, too
- PublicInbox::Spawn::which($ENV{XAPIAN_COMPACT} || 'xapian-compact');
-}
-
# favor lei() or lei_ok() over $lei for new code
sub lei (@) {
my ($cmd, $env, $xopt) = @_;
diff --git a/t/ds-leak.t b/t/ds-leak.t
index 57d9cd72..4c211639 100644
--- a/t/ds-leak.t
+++ b/t/ds-leak.t
@@ -3,14 +3,11 @@
# License: GPL-1.0+ or Artistic-1.0-Perl
# <https://www.gnu.org/licenses/gpl-1.0.txt>
# <https://dev.perl.org/licenses/artistic.html>
-use strict;
-use warnings;
-use Test::More;
-use PublicInbox::TestCommon;
+use strict; use v5.10.1; use PublicInbox::TestCommon;
use_ok 'PublicInbox::DS';
if ('close-on-exec for epoll and kqueue') {
- use PublicInbox::Spawn qw(spawn which);
+ use PublicInbox::Spawn qw(spawn);
my $pid;
my $evfd_re = qr/(?:kqueue|eventpoll)/i;
@@ -32,7 +29,7 @@ if ('close-on-exec for epoll and kqueue') {
is($l, undef, 'cloexec works and sleep(1) is running');
SKIP: {
- my $lsof = which('lsof') or skip 'lsof missing', 1;
+ my $lsof = require_cmd('lsof', 1) or skip 'lsof missing', 1;
my $rdr = { 2 => \(my $null) };
my @of = grep(/$evfd_re/, xqx([$lsof, '-p', $pid], {}, $rdr));
my $err = $?;
diff --git a/t/hl_mod.t b/t/hl_mod.t
index ffe9e784..96878d51 100644
--- a/t/hl_mod.t
+++ b/t/hl_mod.t
@@ -1,12 +1,7 @@
#!/usr/bin/perl -w
# Copyright (C) 2019-2021 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 PublicInbox::Spawn qw(which);
-use PublicInbox::TestCommon;
-use IO::Handle; # ->autoflush
+use strict; use v5.10.1; use PublicInbox::TestCommon; use IO::Handle; # ->autoflush
use Fcntl qw(:seek);
eval { require highlight } or
plan skip_all => "failed to load highlight.pm for $0";
@@ -30,7 +25,7 @@ my $orig = $str;
is($$ref, $$lref, 'do_hl_lang matches do_hl');
SKIP: {
- my $w3m = which('w3m') or
+ my $w3m = require_cmd('w3m', 1) or
skip('w3m(1) missing to check output', 1);
my $cmd = [ $w3m, qw(-T text/html -dump -config /dev/null) ];
my $in = '<pre>' . $$ref . '</pre>';
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 296e1dc1..5dc5734e 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -2,12 +2,9 @@
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# note: our HTTP server should be standalone and capable of running
# generic PSGI/Plack apps.
-use strict;
-use warnings;
-use Test::More;
+use strict; use v5.10.1; use PublicInbox::TestCommon;
use Time::HiRes qw(gettimeofday tv_interval);
-use PublicInbox::Spawn qw(which spawn popen_rd);
-use PublicInbox::TestCommon;
+use PublicInbox::Spawn qw(spawn popen_rd);
require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
use Digest::SHA qw(sha1_hex);
use IO::Handle ();
@@ -303,7 +300,7 @@ my $check_self = sub {
};
SKIP: {
- my $curl = which('curl') or skip('curl(1) missing', 4);
+ my $curl = require_cmd('curl', 1) or skip('curl(1) missing', 4);
my $base = 'http://'.tcp_host_port($sock);
my $url = "$base/sha1";
my ($r, $w);
@@ -606,7 +603,7 @@ SKIP: {
SKIP: {
skip 'only testing lsof(8) output on Linux', 1 if $^O ne 'linux';
- my $lsof = which('lsof') or skip 'no lsof in PATH', 1;
+ my $lsof = require_cmd('lsof', 1) or skip 'no lsof in PATH', 1;
my $null_in = '';
my $rdr = { 2 => \(my $null_err), 0 => \$null_in };
my @lsof = xqx([$lsof, '-p', $td->{pid}], undef, $rdr);
diff --git a/t/imapd.t b/t/imapd.t
index 906b6926..bd8ad7e5 100644
--- a/t/imapd.t
+++ b/t/imapd.t
@@ -7,7 +7,6 @@ use Test::More;
use Time::HiRes ();
use PublicInbox::TestCommon;
use PublicInbox::Config;
-use PublicInbox::Spawn qw(which);
require_mods(qw(-imapd Mail::IMAPClient));
my $imap_client = 'Mail::IMAPClient';
my $can_compress = $imap_client->can('compress');
diff --git a/t/lei-daemon.t b/t/lei-daemon.t
index b0c94a87..b60c7ce6 100644
--- a/t/lei-daemon.t
+++ b/t/lei-daemon.t
@@ -3,7 +3,6 @@
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
-use PublicInbox::Spawn qw(which);
test_lei({ daemon_only => 1 }, sub {
my $send_cmd = PublicInbox::Spawn->can('send_cmd4') // do {
diff --git a/t/lei-externals.t b/t/lei-externals.t
index 5e3c67bc..fed57789 100644
--- a/t/lei-externals.t
+++ b/t/lei-externals.t
@@ -3,7 +3,6 @@
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
use Fcntl qw(SEEK_SET);
-use PublicInbox::Spawn qw(which);
require_git 2.6;
require_mods(qw(json DBD::SQLite Search::Xapian));
use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
@@ -17,9 +16,10 @@ my $test_external_remote = sub {
my ($url, $k) = @_;
SKIP: {
skip "$k unset", 1 if !$url;
- state $curl = which('curl');
- $curl or skip 'no curl', 1;
- which('torsocks') or skip 'no torsocks', 1 if $url =~ m!\.onion/!;
+ require_cmd 'curl', 1 or skip 'curl missing', 1;
+ if ($url =~ m!\.onion/!) {
+ require_cmd 'torsocks', 1 or skip 'no torsocks', 1;
+ }
my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
my @cmd = ('q', '--only', $url, '-q', "m:$mid");
lei_ok(@cmd, \"query $url");
diff --git a/t/lei-import-http.t b/t/lei-import-http.t
index e9eec1f7..d113d479 100644
--- a/t/lei-import-http.t
+++ b/t/lei-import-http.t
@@ -2,9 +2,8 @@
# Copyright (C) 2021 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::Spawn qw(which);
require_mods(qw(lei -httpd));
-which('curl') or plan skip_all => "curl required for $0";
+require_cmd('curl');
my ($ro_home, $cfg_path) = setup_public_inboxes;
my ($tmpdir, $for_destroy) = tmpdir;
my $sock = tcp_server;
diff --git a/t/lei-mirror.t b/t/lei-mirror.t
index 4f4c49c1..44acbe95 100644
--- a/t/lei-mirror.t
+++ b/t/lei-mirror.t
@@ -4,6 +4,7 @@
use strict; use v5.10.1; use PublicInbox::TestCommon;
use PublicInbox::Inbox;
require_mods(qw(-httpd lei));
+require_cmd('curl');
my $sock = tcp_server();
my ($tmpdir, $for_destroy) = tmpdir();
my $http = 'http://'.tcp_host_port($sock);
diff --git a/t/lei-q-remote-import.t b/t/lei-q-remote-import.t
index aaf56e27..9131c01b 100644
--- a/t/lei-q-remote-import.t
+++ b/t/lei-q-remote-import.t
@@ -3,6 +3,7 @@
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
require_mods(qw(lei -httpd));
+require_cmd 'curl';
use PublicInbox::MboxReader;
my ($ro_home, $cfg_path) = setup_public_inboxes;
my $sock = tcp_server;
diff --git a/t/lei.t b/t/lei.t
index d1f1cbc0..c8f47576 100644
--- a/t/lei.t
+++ b/t/lei.t
@@ -3,11 +3,9 @@
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
use File::Path qw(rmtree);
-use PublicInbox::Spawn qw(which);
# this only tests the basic help/config/init/completion bits of lei;
# actual functionality is tested in other t/lei-*.t tests
-my $curl = which('curl');
my $home;
my $home_trash = [];
my $cleanup = sub { rmtree([@$home_trash, @_]) };
@@ -156,7 +154,7 @@ my $test_fail = sub {
lei_ok('sucks', \'yes, but hopefully less every day');
like($lei_out, qr/loaded features/, 'loaded features shown');
SKIP: {
- skip 'no curl', 3 unless which('curl');
+ skip 'no curl', 3 unless require_cmd('curl', 1);
lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m));
is($? >> 8, 3, 'got curl exit for bogus URL');
lei(qw(q --only http://127.0.0.1:99999/bogus/ t:m -o), "$home/junk");
diff --git a/t/nntpd.t b/t/nntpd.t
index 67205fe5..3c171a3b 100644
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -1,10 +1,7 @@
#!perl -w
# Copyright (C) 2015-2021 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::Spawn qw(which);
+use strict; use v5.10.1; use PublicInbox::TestCommon;
require_mods(qw(DBD::SQLite));
use PublicInbox::Eml;
use Socket qw(IPPROTO_TCP TCP_NODELAY);
@@ -17,7 +14,7 @@ use Digest::SHA;
my $version = $ENV{PI_TEST_VERSION} || 1;
require_git('2.6') if $version == 2;
use_ok 'PublicInbox::Msgmap';
-my $lsof = which('lsof');
+my $lsof = require_cmd('lsof', 1);
my $fast_idle = eval { require Linux::Inotify2; 1 } //
eval { require IO::KQueue; 1 };
diff --git a/t/nodatacow.t b/t/nodatacow.t
index 9b67c521..19247c10 100644
--- a/t/nodatacow.t
+++ b/t/nodatacow.t
@@ -1,11 +1,8 @@
#!perl -w
# Copyright (C) 2020-2021 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 strict; use v5.10.1; use PublicInbox::TestCommon;
use File::Temp 0.19;
-use PublicInbox::TestCommon;
-use PublicInbox::Spawn qw(which);
use_ok 'PublicInbox::NDC_PP';
SKIP: {
@@ -13,8 +10,9 @@ SKIP: {
skip 'test is Linux-only', $nr if $^O ne 'linux';
my $dir = $ENV{BTRFS_TESTDIR};
skip 'BTRFS_TESTDIR not defined', $nr unless defined $dir;
- skip 'chattr(1) not installed', $nr unless which('chattr');
- my $lsattr = which('lsattr') or skip 'lsattr(1) not installed', $nr;
+ require_cmd('chattr', 1) or skip 'chattr(1) not installed', $nr;
+ my $lsattr = require_cmd('lsattr', 1) or
+ skip 'lsattr(1) not installed', $nr;
my $tmp = File::Temp->newdir('nodatacow-XXXX', DIR => $dir);
my $dn = $tmp->dirname;
diff --git a/t/solver_git.t b/t/solver_git.t
index f5cc592c..d5f4c4a0 100644
--- a/t/solver_git.t
+++ b/t/solver_git.t
@@ -7,7 +7,7 @@ use PublicInbox::TestCommon;
use Cwd qw(abs_path);
require_git(2.6);
use PublicInbox::ContentHash qw(git_sha);
-use PublicInbox::Spawn qw(popen_rd which);
+use PublicInbox::Spawn qw(popen_rd);
require_mods(qw(DBD::SQLite Search::Xapian Plack::Util));
my $git_dir = xqx([qw(git rev-parse --git-dir)], undef, {2 => \(my $null)});
$? == 0 or plan skip_all => "$0 must be run from a git working tree";
@@ -270,7 +270,7 @@ EOF
my $url = "http://$h:$p";
local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = $url;
Plack::Test::ExternalServer::test_psgi(client => $client);
- skip 'no curl', 1 unless which('curl');
+ require_cmd('curl', 1) or skip 'no curl', 1;
mkdir "$tmpdir/ext" // xbail "mkdir $!";
test_lei({tmpdir => "$tmpdir/ext"}, sub {
diff --git a/t/v2mirror.t b/t/v2mirror.t
index 2bb3238b..8bcffc29 100644
--- a/t/v2mirror.t
+++ b/t/v2mirror.t
@@ -1,19 +1,17 @@
# Copyright (C) 2018-2021 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::TestCommon;
use File::Path qw(remove_tree);
use Cwd qw(abs_path);
require_git(2.6);
+require_cmd('curl');
local $ENV{HOME} = abs_path('t');
# Integration tests for HTTP cloning + mirroring
require_mods(qw(Plack::Util Plack::Builder
HTTP::Date HTTP::Status Search::Xapian DBD::SQLite));
-use IO::Socket;
-use POSIX qw(dup2);
use_ok 'PublicInbox::V2Writable';
use PublicInbox::InboxWritable;
use PublicInbox::Eml;
diff --git a/t/v2reindex.t b/t/v2reindex.t
index e9f2b73b..cafe8648 100644
--- a/t/v2reindex.t
+++ b/t/v2reindex.t
@@ -1,12 +1,9 @@
# Copyright (C) 2018-2021 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 strict; use v5.10.1; use PublicInbox::TestCommon;
use PublicInbox::Eml;
use PublicInbox::ContentHash qw(content_digest);
use File::Path qw(remove_tree);
-use PublicInbox::TestCommon;
require_git(2.6);
require_mods(qw(DBD::SQLite Search::Xapian));
use_ok 'PublicInbox::V2Writable';
@@ -551,9 +548,8 @@ ok(run_script([qw(-index --reindex --xapian-only), $inboxdir], $env, $rdr),
is($err, '', 'no errors from --xapian-only');
undef $for_destroy;
SKIP: {
- use PublicInbox::Spawn qw(which);
skip 'only testing lsof(8) output on Linux', 1 if $^O ne 'linux';
- my $lsof = which('lsof') or skip 'no lsof in PATH', 1;
+ my $lsof = require_cmd('lsof', 1) or skip 'no lsof in PATH', 1;
my $rdr = { 2 => \(my $null_err) };
my @d = grep(m!/xap[0-9]+/!, xqx([$lsof, '-p', $$], undef, $rdr));
is_deeply(\@d, [], 'no deleted index files') or diag explain(\@d);
diff --git a/t/www_altid.t b/t/www_altid.t
index 7c2b6b21..94a2e807 100644
--- a/t/www_altid.t
+++ b/t/www_altid.t
@@ -1,12 +1,10 @@
#!perl -w
# Copyright (C) 2020-2021 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 strict; use v5.10.1; use PublicInbox::TestCommon;
use PublicInbox::Config;
-use PublicInbox::Spawn qw(which spawn);
-which('sqlite3') or plan skip_all => 'sqlite3 binary missing';
+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));
use_ok($_) for qw(Plack::Test HTTP::Request::Common);
diff --git a/t/www_listing.t b/t/www_listing.t
index 5f90139a..eb77969b 100644
--- a/t/www_listing.t
+++ b/t/www_listing.t
@@ -2,11 +2,7 @@
# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# manifest.js.gz generation and grok-pull integration test
-use strict;
-use v5.10.1;
-use Test::More;
-use PublicInbox::Spawn qw(which);
-use PublicInbox::TestCommon;
+use strict; use v5.10.1; use PublicInbox::TestCommon;
use PublicInbox::Import;
use IO::Uncompress::Gunzip qw(gunzip);
require_mods(qw(json URI::Escape Plack::Builder Digest::SHA HTTP::Tiny));
@@ -134,7 +130,7 @@ SKIP: {
tiny_test($json, $host, $port, 1);
undef $sock;
- my $grok_pull = which('grok-pull') or
+ my $grok_pull = require_cmd('grok-pull', 1) or
skip('grok-pull not available', 12);
my ($grok_version) = (xqx([$grok_pull, "--version"])
=~ /(\d+)\.(?:\d+)(?:\.(\d+))?/);
next prev parent reply other threads:[~2021-09-13 20:53 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-13 20:53 [PATCH 0/6] a batch of misc fixes Eric Wong
2021-09-13 20:53 ` Eric Wong [this message]
2021-09-13 20:53 ` [PATCH 2/6] lei: stop_pager: restore stdout when done Eric Wong
2021-09-13 20:53 ` [PATCH 3/6] use POSIX::PIPE_BUF (instead of _POSIX_PIPE_BUF) Eric Wong
2021-09-13 20:53 ` [PATCH 4/6] lei up: localize %ENV in redispatch Eric Wong
2021-09-13 20:53 ` [PATCH 5/6] lei_xsearch: sensible errors for missing/broken externals Eric Wong
2021-09-13 20:53 ` [PATCH 6/6] lei up: fix --mua with single output Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210913205355.13002-2-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).