1 # Helper functions for Perl test programs in Automake distributions.
3 # This module provides a collection of helper functions used by test programs
4 # written in Perl and included in C source distributions that use Automake.
5 # They embed knowledge of how I lay out my source trees and test suites with
6 # Autoconf and Automake. They may be usable by others, but doing so will
7 # require closely following the conventions implemented by the rra-c-util
10 # All the functions here assume that C_TAP_BUILD and C_TAP_SOURCE are set in
11 # the environment. This is normally done via the C TAP Harness runtests
14 # SPDX-License-Identifier: MIT
16 package Test::RRA::Automake;
19 use base qw(Exporter);
24 use File::Find qw(find);
27 use Test::RRA::Config qw($LIBRARY_PATH);
29 # Used below for use lib calls.
30 my ($PERL_BLIB_ARCH, $PERL_BLIB_LIB);
32 # Determine the path to the build tree of any embedded Perl module package in
33 # this source package. We do this in a BEGIN block because we're going to use
34 # the results in a use lib command below.
36 $PERL_BLIB_ARCH = File::Spec->catdir(qw(perl blib arch));
37 $PERL_BLIB_LIB = File::Spec->catdir(qw(perl blib lib));
39 # If C_TAP_BUILD is set, we can come up with better values.
40 if (defined($ENV{C_TAP_BUILD})) {
41 my ($vol, $dirs) = File::Spec->splitpath($ENV{C_TAP_BUILD}, 1);
42 my @dirs = File::Spec->splitdir($dirs);
44 $PERL_BLIB_ARCH = File::Spec->catdir(@dirs, qw(perl blib arch));
45 $PERL_BLIB_LIB = File::Spec->catdir(@dirs, qw(perl blib lib));
49 # Prefer the modules built as part of our source package. Otherwise, we may
50 # not find Perl modules while testing, or find the wrong versions.
51 use lib $PERL_BLIB_ARCH;
52 use lib $PERL_BLIB_LIB;
54 # Declare variables that should be set in BEGIN for robustness.
55 our (@EXPORT_OK, $VERSION);
57 # Set $VERSION and everything export-related in a BEGIN block for robustness
58 # against circular module loading (not that we load any modules, but
59 # consistency is good).
62 all_files automake_setup perl_dirs test_file_path test_tmpdir
65 # This version should match the corresponding rra-c-util release, but with
66 # two digits for the minor version, including a leading zero if necessary,
67 # so that it will sort properly.
71 # Directories to skip globally when looking for all files, or for directories
72 # that could contain Perl files.
74 .git .pc _build autom4te.cache build-aux perl/_build perl/blib
77 # Additional paths to skip when building a list of all files in the
78 # distribution. This primarily skips build artifacts that aren't interesting
79 # to any of the tests. These match any path component.
81 .deps .dirstamp .libs aclocal.m4 config.h config.h.in config.h.in~
82 config.log config.status configure configure~
85 # The temporary directory created by test_tmpdir, if any. If this is set,
86 # attempt to remove the directory stored here on program exit (but ignore
90 # Returns a list of all files in the distribution.
92 # Returns: List of files
96 # Turn the skip lists into hashes for ease of querying.
97 my %skip = map { $_ => 1 } @GLOBAL_SKIP;
98 my %files_skip = map { $_ => 1 } @FILES_SKIP;
100 # Wanted function for find. Prune anything matching either of the skip
101 # lists, or *.lo files, and then add all regular files to the list.
104 my $path = $File::Find::name;
105 $path =~ s{ \A [.]/ }{}xms;
106 if ($skip{$path} || $files_skip{$file} || $file =~ m{ [.]lo\z }xms) {
107 $File::Find::prune = 1;
115 # Do the recursive search and return the results.
120 # Perform initial test setup for running a Perl test in an Automake package.
121 # This verifies that C_TAP_BUILD and C_TAP_SOURCE are set and then changes
122 # directory to the C_TAP_SOURCE directory by default. Sets LD_LIBRARY_PATH if
123 # the $LIBRARY_PATH configuration option is set. Calls BAIL_OUT if
124 # C_TAP_BUILD or C_TAP_SOURCE are missing or if anything else fails.
126 # $args_ref - Reference to a hash of arguments to configure behavior:
127 # chdir_build - If set to a true value, changes to C_TAP_BUILD instead of
134 # Bail if C_TAP_BUILD or C_TAP_SOURCE are not set.
135 if (!$ENV{C_TAP_BUILD}) {
136 BAIL_OUT('C_TAP_BUILD not defined (run under runtests)');
138 if (!$ENV{C_TAP_SOURCE}) {
139 BAIL_OUT('C_TAP_SOURCE not defined (run under runtests)');
142 # C_TAP_BUILD or C_TAP_SOURCE will be the test directory. Change to the
145 if ($args_ref->{chdir_build}) {
146 $start = $ENV{C_TAP_BUILD};
148 $start = $ENV{C_TAP_SOURCE};
150 my ($vol, $dirs) = File::Spec->splitpath($start, 1);
151 my @dirs = File::Spec->splitdir($dirs);
154 # Simplify relative paths at the end of the directory.
157 while ($i > 2 && $dirs[$i] eq File::Spec->updir) {
165 my $root = File::Spec->catpath($vol, File::Spec->catdir(@dirs), q{});
166 chdir($root) or BAIL_OUT("cannot chdir to $root: $!");
168 # If C_TAP_BUILD is a subdirectory of C_TAP_SOURCE, add it to the global
170 my ($buildvol, $builddirs) = File::Spec->splitpath($ENV{C_TAP_BUILD}, 1);
171 my @builddirs = File::Spec->splitdir($builddirs);
173 if ($buildvol eq $vol && @builddirs == @dirs + 1) {
174 while (@dirs && $builddirs[0] eq $dirs[0]) {
178 if (@builddirs == 1) {
179 push(@GLOBAL_SKIP, $builddirs[0]);
183 # Set LD_LIBRARY_PATH if the $LIBRARY_PATH configuration option is set.
184 ## no critic (Variables::RequireLocalizedPunctuationVars)
185 if (defined($LIBRARY_PATH)) {
186 @builddirs = File::Spec->splitdir($builddirs);
188 my $libdir = File::Spec->catdir(@builddirs, $LIBRARY_PATH);
189 my $path = File::Spec->catpath($buildvol, $libdir, q{});
190 if (-d "$path/.libs") {
193 if ($ENV{LD_LIBRARY_PATH}) {
194 $ENV{LD_LIBRARY_PATH} .= ":$path";
196 $ENV{LD_LIBRARY_PATH} = $path;
202 # Returns a list of directories that may contain Perl scripts and that should
203 # be passed to Perl test infrastructure that expects a list of directories to
204 # recursively check. The list will be all eligible top-level directories in
205 # the package except for the tests directory, which is broken out to one
206 # additional level. Calls BAIL_OUT on any problems
208 # $args_ref - Reference to a hash of arguments to configure behavior:
209 # skip - A reference to an array of directories to skip
211 # Returns: List of directories possibly containing Perl scripts to test
215 # Add the global skip list. We also ignore the perl directory if it
216 # exists since, in my packages, it is treated as a Perl module
217 # distribution and has its own standalone test suite.
218 my @skip = $args_ref->{skip} ? @{ $args_ref->{skip} } : ();
219 push(@skip, @GLOBAL_SKIP, 'perl');
221 # Separate directories to skip under tests from top-level directories.
222 my @skip_tests = grep { m{ \A tests/ }xms } @skip;
223 @skip = grep { !m{ \A tests }xms } @skip;
224 for my $skip_dir (@skip_tests) {
225 $skip_dir =~ s{ \A tests/ }{}xms;
228 # Convert the skip lists into hashes for convenience.
229 my %skip = map { $_ => 1 } @skip, 'tests';
230 my %skip_tests = map { $_ => 1 } @skip_tests;
232 # Build the list of top-level directories to test.
233 opendir(my $rootdir, q{.}) or BAIL_OUT("cannot open .: $!");
234 my @dirs = grep { -d && !$skip{$_} } readdir($rootdir);
236 @dirs = File::Spec->no_upwards(@dirs);
238 # Add the list of subdirectories of the tests directory.
240 opendir(my $testsdir, q{tests}) or BAIL_OUT("cannot open tests: $!");
242 # Skip if found in %skip_tests or if not a directory.
243 my $is_skipped = sub {
245 return 1 if $skip_tests{$dir};
246 $dir = File::Spec->catdir('tests', $dir);
247 return -d $dir ? 0 : 1;
250 # Build the filtered list of subdirectories of tests.
251 my @test_dirs = grep { !$is_skipped->($_) } readdir($testsdir);
253 @test_dirs = File::Spec->no_upwards(@test_dirs);
255 # Add the tests directory to the start of the directory name.
256 push(@dirs, map { File::Spec->catdir('tests', $_) } @test_dirs);
261 # Find a configuration file for the test suite. Searches relative to
262 # C_TAP_BUILD first and then C_TAP_SOURCE and returns whichever is found
263 # first. Calls BAIL_OUT if the file could not be found.
265 # $file - Partial path to the file
267 # Returns: Full path to the file
271 for my $base ($ENV{C_TAP_BUILD}, $ENV{C_TAP_SOURCE}) {
272 next if !defined($base);
273 if (-e "$base/$file") {
274 return "$base/$file";
277 BAIL_OUT("cannot find $file");
281 # Create a temporary directory for tests to use for transient files and return
282 # the path to that directory. The directory is automatically removed on
283 # program exit. The directory permissions use the current umask. Calls
284 # BAIL_OUT if the directory could not be created.
286 # Returns: Path to a writable temporary directory
290 # If we already figured out what directory to use, reuse the same path.
291 # Otherwise, create a directory relative to C_TAP_BUILD if set.
292 if (defined($TMPDIR)) {
296 if (defined($ENV{C_TAP_BUILD})) {
297 $base = $ENV{C_TAP_BUILD};
299 $base = File::Spec->curdir;
301 $path = File::Spec->catdir($base, 'tmp');
304 # Create the directory if it doesn't exist.
306 if (!mkdir($path, 0777)) {
307 BAIL_OUT("cannot create directory $path: $!");
311 # Store the directory name for cleanup and return it.
316 # On program exit, remove $TMPDIR if set and if possible. Report errors with
317 # diag but otherwise ignore them.
319 if (defined($TMPDIR) && -d $TMPDIR) {
321 if (!rmdir($TMPDIR)) {
322 diag("cannot remove temporary directory $TMPDIR: $!");
331 Allbery Automake Automake-aware Automake-based rra-c-util ARGS subdirectories
332 sublicense MERCHANTABILITY NONINFRINGEMENT umask
336 Test::RRA::Automake - Automake-aware support functions for Perl tests
340 use Test::RRA::Automake qw(automake_setup perl_dirs test_file_path);
341 automake_setup({ chdir_build => 1 });
343 # Paths to directories that may contain Perl scripts.
344 my @dirs = perl_dirs({ skip => [qw(lib)] });
346 # Configuration for Kerberos tests.
347 my $keytab = test_file_path('config/keytab');
351 This module collects utility functions that are useful for test scripts
352 written in Perl and included in a C Automake-based package. They assume the
353 layout of a package that uses rra-c-util and C TAP Harness for the test
356 Loading this module will also add the directories C<perl/blib/arch> and
357 C<perl/blib/lib> to the Perl library search path, relative to C_TAP_BUILD if
358 that environment variable is set. This is harmless for C Automake projects
359 that don't contain an embedded Perl module, and for those projects that do,
360 this will allow subsequent C<use> calls to find modules that are built as part
361 of the package build process.
363 The automake_setup() function should be called before calling any other
364 functions provided by this module.
368 None of these functions are imported by default. The ones used by a script
369 should be explicitly imported. On failure, all of these functions call
370 BAIL_OUT (from Test::More).
376 Returns a list of all "interesting" files in the distribution that a test
377 suite may want to look at. This excludes various products of the build system,
378 the build directory if it's under the source directory, and a few other
379 uninteresting directories like F<.git>. The returned paths will be paths
380 relative to the root of the package.
382 =item automake_setup([ARGS])
384 Verifies that the C_TAP_BUILD and C_TAP_SOURCE environment variables are set
385 and then changes directory to the top of the source tree (which is one
386 directory up from the C_TAP_SOURCE path, since C_TAP_SOURCE points to the top
387 of the tests directory).
389 If ARGS is given, it should be a reference to a hash of configuration options.
390 Only one option is supported: C<chdir_build>. If it is set to a true value,
391 automake_setup() changes directories to the top of the build tree instead.
393 =item perl_dirs([ARGS])
395 Returns a list of directories that may contain Perl scripts that should be
396 tested by test scripts that test all Perl in the source tree (such as syntax
397 or coding style checks). The paths will be simple directory names relative to
398 the current directory or two-part directory names under the F<tests>
399 directory. (Directories under F<tests> are broken out separately since it's
400 common to want to apply different policies to different subdirectories of
403 If ARGS is given, it should be a reference to a hash of configuration options.
404 Only one option is supported: C<skip>, whose value should be a reference to an
405 array of additional top-level directories or directories starting with
406 C<tests/> that should be skipped.
408 =item test_file_path(FILE)
410 Given FILE, which should be a relative path, locates that file relative to the
411 test directory in either the source or build tree. FILE will be checked for
412 relative to the environment variable C_TAP_BUILD first, and then relative to
413 C_TAP_SOURCE. test_file_path() returns the full path to FILE or calls
414 BAIL_OUT if FILE could not be found.
418 Create a temporary directory for tests to use for transient files and return
419 the path to that directory. The directory is created relative to the
420 C_TAP_BUILD environment variable, which must be set. Permissions on the
421 directory are set using the current umask. test_tmpdir() returns the full
422 path to the temporary directory or calls BAIL_OUT if it could not be created.
424 The directory is automatically removed if possible on program exit. Failure
425 to remove the directory on exit is reported with diag() and otherwise ignored.
435 The root of the tests directory in Automake build directory for this package,
436 used to find files as documented above.
440 The root of the tests directory in the source tree for this package, used to
441 find files as documented above.
447 Russ Allbery <eagle@eyrie.org>
449 =head1 COPYRIGHT AND LICENSE
451 Copyright 2014-2015, 2018-2021 Russ Allbery <eagle@eyrie.org>
453 Copyright 2013 The Board of Trustees of the Leland Stanford Junior University
455 Permission is hereby granted, free of charge, to any person obtaining a copy
456 of this software and associated documentation files (the "Software"), to deal
457 in the Software without restriction, including without limitation the rights
458 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
459 copies of the Software, and to permit persons to whom the Software is
460 furnished to do so, subject to the following conditions:
462 The above copyright notice and this permission notice shall be included in all
463 copies or substantial portions of the Software.
465 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
466 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
467 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
468 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
469 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
470 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
475 Test::More(3), Test::RRA(3), Test::RRA::Config(3)
477 This module is maintained in the rra-c-util package. The current version is
478 available from L<https://www.eyrie.org/~eagle/software/rra-c-util/>.
480 The C TAP Harness test driver and libraries for TAP-based C testing are
481 available from L<https://www.eyrie.org/~eagle/software/c-tap-harness/>.
486 # copyright-at-end-flag: t