@@ -3,6 +3,7 @@ package Test2::Aggregate;
3
3
use strict;
4
4
use warnings;
5
5
6
+ use File::Basename;
6
7
use File::Find;
7
8
use File::Path;
8
9
use Path::Tiny;
@@ -84,6 +85,7 @@ have less issues with L<Test2::Suite> (see notes).
84
85
absolute => 0, # optional
85
86
bail_on_fail => 0, # optional, requires Test2::Plugin::BailOnFail
86
87
no_dot => 0, # optional
88
+ relative_root => 0, # optional
87
89
allow_errors => 0, # optional
88
90
pre_eval => $code_to_eval, # optional
89
91
dry_run => 0, # optional
@@ -135,9 +137,14 @@ Applied after C<exclude>.
135
137
136
138
=item * C<root > (optional)
137
139
138
- If defined, must be a valid root directory that will prefix all C<dirs > and
139
- C<lists > items. You may want to set it to C<'./' > if you want dirs relative
140
- to the current directory and the dot is not in your C<@INC > .
140
+ If defined, must be a valid root directory that will prefix all C<dirs > and C<lists >
141
+ items. You may want to set it to C<'./' > if you want dirs relative to the current
142
+ directory and the dot is not in your C<@INC > .
143
+
144
+ =item * C<relative_root > (optional)
145
+
146
+ Set to true if you want your root to be relative to the caller script's directory
147
+ (based on C<$0 > ). It is ignored if you use an absolute root path.
141
148
142
149
=item * C<load_modules > (optional)
143
150
@@ -289,19 +296,22 @@ sub run_tests {
289
296
290
297
@dirs = @{$args {dirs }} if $args {dirs };
291
298
$args {root } .= ' /' unless !$args {root } || $args {root } =~ m # /$ # ;
299
+ $args {root } = dirname($0 ) . " /$args {root}"
300
+ if $args {relative_root } && $args {root } && $args {root } !~ m # ^/# ;
292
301
293
302
if ($args {root } && ! -e $args {root }) {
294
303
warn " Root '$args {root}' does not exist, no tests are loaded."
295
304
} else {
296
305
foreach my $file (@{$args {lists }}) {
297
306
push @dirs ,
298
- map { / ^\s *(?:#|$) / ? () : $_ }
299
- split (/ \r ?\n / , _read_file($args {root }.$file , $args {slurp_param }));
307
+ map {/ ^\s *(?:#|$) / ? () : $_ }
308
+ split (/ \r ?\n / ,
309
+ _read_file($args {root }, $file , $args {slurp_param }));
300
310
}
301
311
302
312
find(
303
313
sub {push @tests , $File::Find::name if / \. t$ / },
304
- grep {-e } map {$args {root } . $_ } @dirs
314
+ grep {-e } map {_check_abs_path( $args {root }, $_ ) } @dirs
305
315
)
306
316
if @dirs ;
307
317
}
@@ -353,11 +363,21 @@ sub run_tests {
353
363
return $args {stats };
354
364
}
355
365
366
+ sub _check_abs_path {
367
+ my $root = shift ;
368
+ my $path = shift ;
369
+ # Can't use path() below as it removes ./ which is needed for absolute option
370
+ $path = " $root$path " unless !$root || substr ($path , 0, 1) eq ' /' ;
371
+ return $path ;
372
+ }
373
+
356
374
sub _read_file {
375
+ my $root = shift ;
357
376
my $path = shift ;
358
377
my $param = shift ;
359
- my $file = path($path );
360
- return $param ? $file -> slurp_utf8 : $file -> slurp($param );
378
+ my $file = path(_check_abs_path($root , $path ));
379
+
380
+ return $param ? $file -> slurp($param ) : $file -> slurp_utf8;
361
381
}
362
382
363
383
sub _process_run_order {
@@ -412,6 +432,7 @@ sub _run_tests {
412
432
foreach my $test (@$tests ) {
413
433
my $test_nm =
414
434
$args -> {absolute } ? $test : _test_name($test , $args -> {root });
435
+
415
436
warn " $test_nm ->Test2::Aggregate\n " if $args -> {test_warnings };
416
437
417
438
$stats {$test_nm }{test_no } = $count unless $stats {$test_nm }{test_no };
@@ -730,6 +751,13 @@ You would call it with something like C<--exclude-lists=t/aggregate/*.lst>, and
730
751
the tests listed will be excluded (you will have them running aggregated through
731
752
their own C<.t > files using L<Test2::Aggregate> ).
732
753
754
+ =head1 WINDOWS SUPPORT
755
+
756
+ Only filesystems with the C</ > separator are supported, simply because the author
757
+ does not expect Windows users (many test suites don't work under StrawberryPerl
758
+ etc). However, if you want Windows filesystems to be supported it should be simple
759
+ enough, so just ask.
760
+
733
761
=head1 AUTHOR
734
762
735
763
Dimitrios Kechagias, C<< <dkechag at cpan.org> >>
0 commit comments