diff --git a/tests/auth/writeoldkey-t b/tests/auth/writeoldkey-t index 3240212942..614dcb6031 100755 --- a/tests/auth/writeoldkey-t +++ b/tests/auth/writeoldkey-t @@ -2,9 +2,11 @@ use strict; use warnings; +use lib $ENV{C_TAP_SOURCE} . "/tests-lib/perl5"; + +use afstest qw(src_path obj_path); use Test::More; use File::Temp qw/tempdir/; -use FindBin qw($Bin); use Cwd qw/abs_path/; use File::Compare; use Sys::Hostname; @@ -19,20 +21,10 @@ if (!defined(gethostbyname(hostname()))) { } plan tests => 1; -my $cmd; -if (defined($ENV{C_TAP_BUILD})) { - $cmd = $ENV{C_TAP_BUILD} . "/auth/writekeyfile"; -} else { - $cmd = $Bin . "/writekeyfile"; -} +my $cmd = obj_path("tests/auth/writekeyfile"); $cmd = abs_path($cmd); -my $keyfile; -if (defined($ENV{C_TAP_SOURCE})) { - $keyfile = $ENV{C_TAP_SOURCE} . "/auth/KeyFile.short"; -} else { - $keyfile = $Bin . "/KeyFile.short"; -} +my $keyfile = src_path("tests/auth/KeyFile.short"); $keyfile = abs_path($keyfile); my $dir = tempdir('afs_XXXXXX', CLEANUP => 1); diff --git a/tests/opr/softsig-t b/tests/opr/softsig-t index d750d98dde..66a2648278 100755 --- a/tests/opr/softsig-t +++ b/tests/opr/softsig-t @@ -24,6 +24,9 @@ use strict; use warnings; +use lib $ENV{C_TAP_SOURCE} . "/tests-lib/perl5"; + +use afstest qw(obj_path); use Test::More tests => 11; use IO::File; use POSIX qw(:signal_h); @@ -33,16 +36,7 @@ use FindBin qw($Bin); # Start up our test process, and send it various signals. Check that these # signals make it to it correctly, and are reported on the command line. -my $softsig_helper; - -# Our softsig helper should be in $TOP_OBJDIR/tests/opr. To calculate that -# path, use the C_TAP_BUILD env var if the test harness has set it; otherwise, -# our next best guess is that it's in the same dir as this script. -if (defined($ENV{C_TAP_BUILD})) { - $softsig_helper = $ENV{C_TAP_BUILD} . "/opr/softsig-helper"; -} else { - $softsig_helper = $Bin . "/softsig-helper"; -} +my $softsig_helper = obj_path("tests/opr/softsig-helper"); # This -dummy argument prevents Perl from putting an intermediate sh # -c between us and softsig-helper in the case where the build diff --git a/tests/ptserver/pt_util-t b/tests/ptserver/pt_util-t index 0f5c84ae0c..af97668922 100755 --- a/tests/ptserver/pt_util-t +++ b/tests/ptserver/pt_util-t @@ -2,17 +2,14 @@ use strict; use warnings; +use lib $ENV{C_TAP_SOURCE} . "/tests-lib/perl5"; +use afstest qw(obj_path); use File::Basename; use File::Temp qw(tempfile); use Test::More tests=>2; -my $builddir = $ENV{C_TAP_BUILD}; -if (!$builddir) { - $builddir = dirname($0)."/.."; -} - -$builddir.="/.."; +my $pt_util = obj_path("src/ptserver/pt_util"); (undef, my $prdbfile) = tempfile("prdbtest.XXXXXX", TMPDIR => 1, UNLINK => 1); @@ -35,13 +32,13 @@ EOF my $fh; -open $fh, '|-', "$builddir/src/ptserver/pt_util", '-w', '-p', $prdbfile +open $fh, '|-', "$pt_util", '-w', '-p', $prdbfile or die "Failed to start pt_util for DB creation\n"; print $fh $instructions; close($fh) or die "pt util failed while creating DB\n"; -open $fh, '-|', "$builddir/src/ptserver/pt_util", '-p', $prdbfile, +open $fh, '-|', "$pt_util", '-p', $prdbfile, '-user', '-group', '-members' or die "Failed to start pt_util for DB reading\n"; diff --git a/tests/rx/perf-t b/tests/rx/perf-t index 406c31f828..0fc2cadf68 100755 --- a/tests/rx/perf-t +++ b/tests/rx/perf-t @@ -2,14 +2,14 @@ use strict; use warnings; +use lib $ENV{C_TAP_SOURCE} . "/tests-lib/perl5"; +use afstest qw(obj_path); use Test::More tests=>4; use POSIX qw(:sys_wait_h :signal_h); my $port = 4000; -my $build = $ENV{C_TAP_BUILD}; -$build = ".." if (!defined($build)); -my $rxperf = $build."/../src/tools/rxperf/rxperf"; +my $rxperf = obj_path("src/tools/rxperf/rxperf"); # Start up an rxperf server diff --git a/tests/tests-lib/perl5/afstest.pm b/tests/tests-lib/perl5/afstest.pm new file mode 100644 index 0000000000..b2ee9bcdf6 --- /dev/null +++ b/tests/tests-lib/perl5/afstest.pm @@ -0,0 +1,61 @@ +# Copyright (c) 2020 Sine Nomine Associates. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +package afstest; +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT_OK = qw(src_path obj_path); + +sub +x_path($;$) +{ + my ($env_var, $path) = @_; + my $tdir = $ENV{$env_var}; + if (!defined($tdir)) { + # If C_TAP_SOURCE/C_TAP_BUILD isn't set, we assume we're running from + # the same cwd as one of the test programs (e.g. 'tests/foo/'). So to + # get to 'tests/', just go up one level. + $tdir = ".."; + } + + # $tdir now represents the 'tests/' dir. Go one level up to get to the + # top-level dir. + if (defined($path)) { + return "$tdir/../$path"; + } else { + return "$tdir/.."; + } +} + +sub +src_path(;$) +{ + my $path = $_[0]; + return x_path("C_TAP_SOURCE", $path); +} + +sub +obj_path(;$) +{ + my $path = $_[0]; + return x_path("C_TAP_BUILD", $path); +} diff --git a/tests/tests-lib/perl5/mancheck_utils.pm b/tests/tests-lib/perl5/mancheck_utils.pm index 62e1693cf6..540c1fc1ec 100644 --- a/tests/tests-lib/perl5/mancheck_utils.pm +++ b/tests/tests-lib/perl5/mancheck_utils.pm @@ -11,6 +11,7 @@ # use File::Basename; use Test::More; +use afstest qw(src_path obj_path); sub check_command_binary { my $c = shift(@_); @@ -99,12 +100,8 @@ sub test_command_man_pages { sub run_manpage_tests($$) { my ($subdir, $command) = @_; - # When run from 'runtests', our cwd will be TOP_OBJDIR/tests. $C_TAP_SOURCE - # is set to TOP_SRCDIR/tests, and $C_TAP_BUILD is set to TOP_OBJDIR/tests. - # We want the top-level src and obj dirs, in order to find the relevant - # binaries and manpages. - my $srcdir = $ENV{C_TAP_SOURCE} . "/.."; - my $objdir = $ENV{C_TAP_BUILD} . "/.."; + my $srcdir = src_path(); + my $objdir = obj_path(); my @sub_commands = lookup_sub_commands("$objdir/$subdir", $command); die("No subcommands found in $objdir/$subdir/$command?") unless(@sub_commands);