blib/lib/Test/Manifest.pm

filter: Statementstmt 74.1% 86 / 116 Branchbran 58.6% 34 / 58 Conditioncond 71.4% 5 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 75.0% 12 / 16 Podpod 100% 6 / 6 totaltotal 70.5% 146 / 207 CC 33 SCAR 26.4
CC
33
Cov
69%
run_t_manifest
30.0
_load_test_manifest
24.0
_include_file
6.6
CRAP 14.0
↑ Summary
1 package Test::Manifest;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our @EXPORT = qw(run_t_manifest);
13 5 our @EXPORT_OK = qw(get_t_files make_test_manifest manifest_name);
14  
15 5 our $VERSION = '2.026';
16  
17 5 my %SeenInclude = ();
18 5 my %SeenTest = ();
19  
20 5 require 5.008;
21  
22 0 ▶sub MY::test_via_harness {
Subroutine test_via_harness: not called CC=1 SCAR=6.9
23 0 my($self, $perl, $tests) = @_;
24  
25 0 return qq|\t$perl "-MTest::Manifest" | .
26 qq|"-e" "run_t_manifest(\$(TEST_VERBOSE), '\$(INST_LIB)', | .
27 qq|'\$(INST_ARCHLIB)', \$(TEST_LEVEL) )"\n|;
28 };
29  
30 =encoding utf8
31  
32 =head1 NAME
33  
34 Test::Manifest - interact with a t/test_manifest file
35  
36 =head1 SYNOPSIS
37  
38 # in Makefile.PL
39 eval "use Test::Manifest 2.00";
40  
41 # in Build.PL
42 my $class = do {
43 if( eval 'use Test::Manifest 2.00; 1' ) {
44 Test::Manifest->get_module_build_subclass;
45 }
46 else {
47 'Module::Build';
48 }
49 };
50  
51 my $build = $class->new( ... )
52  
53 # in the file t/test_manifest, list the tests you want
54 # to run in the order you want to run them
55  
56 =head1 DESCRIPTION
57  
58 C<Test::Harness> assumes that you want to run all of the F<.t> files
59 in the F<t/> directory in ASCII-betical order during C<make test> or
60 C<./Build test> unless you say otherwise. This leads to some
61 interesting naming schemes for test files to get them in the desired
62 order. These interesting names ossify when they get into source
63 control, and get even more interesting as more tests show up.
64  
65 C<Test::Manifest> overrides the default test file order. Instead of
66 running all of the F<t/*.t> files in ASCII-betical order, it looks in
67 the F<t/test_manifest> file to find out which tests you want to run
68 and the order in which you want to run them. It constructs the right
69 value for the build system to do the right thing.
70  
71 In F<t/test_manifest>, simply list the tests that you want to run.
72 Their order in the file is the order in which they run. You can
73 comment lines with a C<#>, just like in Perl, and C<Test::Manifest>
74 will strip leading and trailing whitespace from each line. It also
75 checks that the specified file is actually in the F<t/> directory. If
76 the file does not exist, it does not put its name in the list of test
77 files to run and it will issue a warning.
78  
79 Optionally, you can add a number after the test name in test_manifest
80 to define sets of tests. See C<get_t_files> for more information.
81  
82 =head2 ExtUtils::MakeMaker
83  
84 To override the test order behaviour in C<MakeMaker>, C<Test::Manifest>
85 inserts itself in the C<test_via_harness> step by providing its own
86 test runner. In C<Makefile.PL>, all you have to do is load C<Test::Manifest>
87 before you call C<WriteMakefile>. To make it optional, load it in an eval:
88  
89 eval "use Test::Manifest";
90  
91 =head2 Module::Build
92  
93 Overriding parts of C<Module::Build> is tricker if you want to use the
94 subclassing mechanism and still make C<Test::Manifest> optional. If you
95 can load C<Test::Manifest> (version 2.00 or later), C<Test::Manifest> can
96 create the subclass for you.
97  
98 my $class = do {
99 if( eval 'use Test::Manifest 2.00; 1' ) {
100 Test::Manifest->get_module_build_subclass;
101 }
102 else {
103 'Module::Build' # if Test::Manifest isn't there
104 }
105 };
106  
107 $class->new( ... );
108 $class->create_build_file;
109  
110 This is a bit of a problem when you already have your own subclass.
111 C<Test::Manifest> overrides C<find_test_files>, so you can get just
112 that code to add to your own subclass code string:
113  
114 my $code = eval 'use Test::Manifest 2.00; 1'
115 ?
116 Test::Manifest->get_module_build_code_string
117 :
118 '';
119  
120 my $class = Module::Build->subclass(
121 ...,
122 code => "$code\n...your subclass code string...",
123 );
124  
125 =head2 Class methods
126  
127 =over 4
128  
129 =item get_module_build_subclass
130  
131 For C<Module::Build> only.
132  
133 Returns a C<Module::Build> subclass that overrides C<find_test_files>. If
134 you want to have your own C<Module::Build> subclass and still use
135 C<Test::Manifest>, you can get just the code string with
136 C<get_module_build_code_string>.
137  
138 =cut
139  
140 0 ▶sub get_module_build_subclass {
Subroutine get_module_build_subclass: not called CC=1 SCAR=6.9
Pod documented
141 0 my( $class ) = @_;
142  
143  
144 0 require Module::Build;
145  
146 0 my $class = Module::Build->subclass(
147 class => 'Test::Manifest::MB',
148  
149 code => $class->get_module_build_code_string,
150 );
151  
152 0 $class->log_info( "Using Test::Manifest $VERSION\n" );
153  
154 0 $class;
155 }
156  
157 =item get_module_build_code_string
158  
159 For C<Module::Build> only.
160  
161 Returns the overridden C<find_test_files> as Perl code in a string suitable
162 for the C<code> key in C<Module::Build->subclass()>. You can add this to other
163 bits you are overriding or extending.
164  
165 See C<Module::Build::Base::find_test_files> to see the base implementation.
166  
167 =cut
168  
169 0 ▶sub get_module_build_code_string {
Subroutine get_module_build_code_string: not called CC=1 SCAR=6.9
Pod documented
170 0 q{
171 sub find_test_files {
172 my $self = shift;
173 my $p = $self->{properties};
174  
175 my( $level ) = grep { defined } (
176 $ENV{TEST_LEVEL},
177 $p->{ 'testlevel' },
178 0
179 );
180  
181 $self->log_verbose( "Test level is $level\n" );
182  
183 require Test::Manifest;
184 my @files = Test::Manifest::get_t_files( $level );
185 \@files;
186 }
187 }
188 }
189  
190 =back
191  
192 =head2 Functions
193  
194 =over 4
195  
196 =item run_t_manifest( TEST_VERBOSE, INST_LIB, INST_ARCHLIB, TEST_LEVEL )
197  
198 For C<MakeMaker> only. You don't have to mess with this at the user
199 level.
200  
201 Run all of the files in F<t/test_manifest> through C<Test::Harness:runtests>
202 in the order they appear in the file. This is inserted automatically
203  
204 eval "use Test::Manifest";
205  
206 =cut
207  
208 0 ▶sub run_t_manifest {
Subroutine run_t_manifest: not called CC=5 SCAR=34.0
Pod documented
209 0 require Test::Harness;
210 0 require File::Spec;
211  
212 0 $Test::Harness::verbose = shift;
213  
214 0 local @INC = @INC;
215 0 unshift @INC, map { File::Spec->rel2abs($_) } @_[0,1];
216  
217 0 ▶ my( $level ) = $_[2] || 0;
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
218  
219 0 ▶ print STDERR "Test::Manifest $VERSION\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Harness::verbose 0 0 0
220 if $Test::Harness::verbose;
221  
222 0 ▶ print STDERR "Level is $level\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Harness::verbose 0 0 0
223 if $Test::Harness::verbose;
224  
225 0 my @files = get_t_files( $level );
226 0 ▶ print STDERR "Test::Manifest: test harness found [@files]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Harness::verbose 0 0 0
227 if $Test::Harness::verbose;
228  
229 0 Test::Harness::runtests( @files );
230 }
231  
232 =item get_t_files( [LEVEL] )
233  
234 In scalar context it returns a single string that you can use directly
235 in C<WriteMakefile()>. In list context it returns a list of the files it
236 found in F<t/test_manifest>.
237  
238 If a F<t/test_manifest> file does not exist, C<get_t_files()> returns
239 nothing.
240  
241 C<get_t_files()> warns you if it can't find F<t/test_manifest>, or if
242 entries start with F<t/>. It skips blank lines, and strips Perl
243 style comments from the file.
244  
245 Each line in F<t/test_manifest> can have three parts: the test name,
246 the test level (a floating point number), and a comment. By default,
247 the test level is 1.
248  
249 test_name.t 2 #Run this only for level 2 testing
250  
251 Without an argument, C<get_t_files()> returns all the test files it
252 finds. With an argument that is true (so you can't use 0 as a level)
253 and is a number, it skips tests with a level greater than that
254 argument. You can then define sets of tests and choose a set to
255 run. For instance, you might create a set for end users, but also
256 add on a set for deeper testing for developers.
257  
258 Experimentally, you can include a command to grab test names from
259 another file. The command starts with a C<;> to distinguish it
260 from a true filename. The filename (currently) is relative to the
261 current working directory, unlike the filenames, which are relative
262 to C<t/>. The filenames in the included are still relative to C<t/>.
263  
264 ;include t/file_with_other_test_names.txt
265  
266 Also experimentally, you can stop C<Test::Manifest> from reading
267 filenames with the C<;skip> directive. C<Test::Manifest> will skip the
268 filenames up to the C<;unskip> directive (or end of file):
269  
270 run_this1
271 ;skip
272 skip_this
273 ;unskip
274 run_this2
275  
276 To select sets of tests, specify the level in the environment variable
277 C<TEST_LEVEL>:
278  
279 make test # run all tests no matter the level
280 make test TEST_LEVEL=2 # run all tests level 2 and below
281  
282 Eventually this will end up as an option to F<Build.PL>:
283  
284 ./Build test --testlevel=2 # Not yet supported
285  
286 =cut
287  
288 10 ▶sub get_t_files {
Subroutine get_t_files: called CC=5 SCAR=16.1
Pod documented
289 10 my $upper_bound = shift;
290 10 ▶ print STDERR "# Test level is $upper_bound\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Harness::verbose 4 6 10
291 if $Test::Harness::verbose;
292  
293 10 %SeenInclude = ();
294 10 %SeenTest = ();
295  
296 10 my $Manifest = manifest_name();
297 10 ▶ carp( "$Manifest does not exist!" ) unless -e $Manifest;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $Manifest 2 8 10
298 10 my $result = _load_test_manifest( $Manifest, $upper_bound );
299 10 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 2 8 10
300 8 my @tests = @{$result};
301  
302 8 ▶ return wantarray ? @tests : join " ", @tests;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 6 8
303 }
304  
305 # Wrapper for loading test manifest files to support including other files
306 12 ▶sub _load_test_manifest {
Subroutine _load_test_manifest: called CC=18 SCAR=31.8
307 12 my $manifest = shift;
308 12 ▶ return unless open my( $fh ), '<', $manifest;
Branch: 1 branch
BranchTrueFalseTotal
unless open my $fh, "<", $manifest 2 10 12
309  
310 10 ▶ my $upper_bound = shift || 0;
Condition100%2/2
shift() || 0
l!l
37
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
311 10 my @tests = ();
312  
313 10 ▶ LINE: while( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 58 10 68
314 58 s/#.*//; s/^\s+//; s/\s+$//;
315  
316 58 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 12 46 58
317  
318 46 my( $command, $arg ) = split /\s+/, $_, 2;
319 46 ▶ if( ';' eq substr( $command, 0, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (";" eq substr($command, 0, 1)) 2 44 46
320 2 ▶ if( $command eq ';include' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command eq ';include') { } 2 0 2
elsif ($command eq ';skip') { } 0 0 0
321 2 my $result = _include_file( $arg, $., $upper_bound );
322 2 ▶ push @tests, @$result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 2 0 2
323 2 next;
324 }
325 elsif( $command eq ';skip' ) {
326 0 ▶ while( <$fh> ) { last if m/^;unskip/ }
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
if /^;unskip/ 0 0 0
327 0 next LINE;
328 }
329 else {
330 0 croak( "Unknown directive: $command" );
331 }
332 }
333  
334 44 my( $test, $level ) = ( $command, $arg );
335 44 ▶ $level = 1 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 20 24 44
336  
337 44 ▶ next if( $upper_bound and $level > $upper_bound );
Branch: 1 branch
BranchTrueFalseTotal
if $upper_bound and $level > $upper_bound 8 36 44
Condition100%3/3
$upper_bound and $level > $upper_bound
!ll&&!rl&&r
23138
MC/DC100%2/2
$upper_bound and $level > $upper_bound
$upper_bound $level > $upper_bound
Truth table100%3/3
A and B
A= $upper_boundB= $level > $upper_bound
ABresult
0X0
100
111
338  
339 36 ▶ carp( "Bad value for test [$test] level [$level]\n".
Branch: 1 branch
BranchTrueFalseTotal
unless $level =~ /^\d+(?:.\d+)?$/ 0 36 36
340 "Level should be a floating-point number\n" )
341 unless $level =~ m/^\d+(?:.\d+)?$/;
342 36 ▶ carp( "test file begins with t/ [$test]" ) if m|^t/|;
Branch: 1 branch
BranchTrueFalseTotal
if m[^t/] 0 36 36
343  
344 36 ▶ if( -e catfile( "t", $test ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e catfile('t', $test)) { } 36 0 36
345 36 $test = catfile( "t", $test )
346 }
347 else {
348 0 carp( "test file [$test] does not exist! Skipping!" );
349 0 next;
350 }
351  
352 # Make sure we don't include a test we've already seen
353 36 ▶ next if exists $SeenTest{$test};
Branch: 1 branch
BranchTrueFalseTotal
if exists $SeenTest{$test} 0 36 36
354  
355 36 $SeenTest{$test} = 1;
356 36 push @tests, $test;
357 }
358  
359 10 close $fh;
360 10 return \@tests;
361 }
362  
363 2 ▶sub _include_file {
Subroutine _include_file: called CC=5 SCAR=18.9
364 2 my( $file, $line, $upper_bound ) = @_;
365 2 ▶ print STDERR "# Including file $file at line $line\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Harness::verbose 0 2 2
366 if $Test::Harness::verbose;
367  
368 2 ▶ unless( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 2 2
369 0 carp( "$file does not exist" ) ;
370 0 return;
371 }
372  
373 2 ▶ if( exists $SeenInclude{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SeenInclude{$file}) 0 2 2
374 0 carp( "$file already loaded - skipping" ) ;
375 0 return;
376 }
377  
378 2 $SeenInclude{$file} = $line;
379  
380 2 my $result = _load_test_manifest( $file, $upper_bound );
381 2 ▶ return unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 2 2
382  
383 2 $result;
384 }
385  
386  
387 =item make_test_manifest()
388  
389 Creates the test_manifest file in the t directory by reading the
390 contents of the F<t/> directory.
391  
392 TO DO: specify tests in argument lists.
393  
394 TO DO: specify files to skip.
395  
396 =cut
397  
398 1 ▶sub make_test_manifest() {
Subroutine make_test_manifest: called CC=4 SCAR=13.9
Pod documented
399 1 ▶ carp( "t/ directory does not exist!" ) unless -d "t";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "t" 0 1 1
400 1 ▶ return unless open my( $fh ), '>', manifest_name();
Branch: 1 branch
BranchTrueFalseTotal
unless open my $fh, ">", manifest_name() 0 1 1
401  
402 1 my $count = 0;
403 5 ▶ while( my $file = glob("t/*.t") ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = glob("t/*.t"))) 8 1 9
404 8 $file =~ s|^t/||;
405 8 print $fh "$file\n";
406 8 $count++;
407 }
408 1 close $fh;
409  
410 1 return $count;
411 }
412  
413 =item manifest_name()
414  
415 Returns the name of the test manifest file, relative to F<t/>.
416  
417 =cut
418  
419 {
420 5 my $Manifest = catfile( "t", "test_manifest" );
421  
422 17 ▶sub manifest_name {
Subroutine manifest_name: called CC=1 SCAR=0.0
Pod documented
423 17 return $Manifest;
424 }
425 }
426  
427 =back
428  
429 =head1 SOURCE AVAILABILITY
430  
431 This source is in Github:
432  
433 http://github.com/briandfoy/test-manifest/
434  
435 =head1 CREDITS
436  
437 Matt Vanderpol suggested and supplied a patch for the C<;include>
438 feature.
439  
440 Olivier Mengué supplied a documentation patch.
441  
442 =head1 AUTHOR
443  
444 brian d foy, C<< <briandfoy@pobox.com> >>
445  
446 =head1 COPYRIGHT AND LICENSE
447  
448 Copyright © 2002-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
449  
450 This program is free software; you can redistribute it and/or modify
451 it under the terms of the Artistic License 2.0.
452  
453 =cut
454  
455  
456 5 1;
↑ Summary