Skip to content

Commit 2e10e22

Browse files
committed
relative_root
1 parent 9e53c0d commit 2e10e22

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

agg

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ or to check whether specific tests can run successfully under Test2::Aggregate.
2323
--prove, -p : Force prove (default is yath/Test2 if installed).
2424
--verbose, -v : Verbose (passed to yath/prove)
2525
--absolute, -a : Use absolute paths in generated files. Disabled with -r.
26-
--root <s> -r <s> : Define a custom root dir (default is current dir).
26+
--root <s> -r <s> : Define a custom root dir.
2727
--include <s>, -I <s> : Library paths to include.
2828
--test_warnings, -w : Fail tests on warnings.
2929
--test_bundle <s>, -t : Test bundle (def: Test2::V0 or Test::More if -p). Can be comma-separated list.
@@ -71,15 +71,15 @@ pod2usage({-verbose => 1, -output => \*STDOUT, -noperldoc => 1})
7171

7272
Test2::Aggregate::Helper::setup_options(\%opt);
7373

74-
my $file = $opt{out} ? path($opt{out}.'.t') : Path::Tiny->tempfile(
74+
$opt{file} = $opt{out} ? path($opt{out}.'.t') : Path::Tiny->tempfile(
7575
TEMPLATE => "aggXXXXXX",
7676
SUFFIX => '.t',
7777
UNLINK => 0
7878
);
7979

80-
print "Writing output to $file\n" if $opt{verbose};
80+
print "Writing output to $opt{file}\n" if $opt{verbose};
8181

8282
my $code = Test2::Aggregate::Helper::generate_code(\%opt, @ARGV);
83-
$file->spew_utf8($code);
83+
$opt{file}->spew_utf8($code);
8484

85-
exec(Test2::Aggregate::Helper::create_command(\%opt, $file));
85+
exec(Test2::Aggregate::Helper::create_command(\%opt, $opt{file}));

lib/Test2/Aggregate.pm

+36-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package Test2::Aggregate;
33
use strict;
44
use warnings;
55

6+
use File::Basename;
67
use File::Find;
78
use File::Path;
89
use Path::Tiny;
@@ -84,6 +85,7 @@ have less issues with L<Test2::Suite> (see notes).
8485
absolute => 0, # optional
8586
bail_on_fail => 0, # optional, requires Test2::Plugin::BailOnFail
8687
no_dot => 0, # optional
88+
relative_root => 0, # optional
8789
allow_errors => 0, # optional
8890
pre_eval => $code_to_eval, # optional
8991
dry_run => 0, # optional
@@ -135,9 +137,14 @@ Applied after C<exclude>.
135137
136138
=item * C<root> (optional)
137139
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.
141148
142149
=item * C<load_modules> (optional)
143150
@@ -289,19 +296,22 @@ sub run_tests {
289296

290297
@dirs = @{$args{dirs}} if $args{dirs};
291298
$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#^/#;
292301

293302
if ($args{root} && ! -e $args{root}) {
294303
warn "Root '$args{root}' does not exist, no tests are loaded."
295304
} else {
296305
foreach my $file (@{$args{lists}}) {
297306
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}));
300310
}
301311

302312
find(
303313
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
305315
)
306316
if @dirs;
307317
}
@@ -353,11 +363,21 @@ sub run_tests {
353363
return $args{stats};
354364
}
355365

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+
356374
sub _read_file {
375+
my $root = shift;
357376
my $path = shift;
358377
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;
361381
}
362382

363383
sub _process_run_order {
@@ -412,6 +432,7 @@ sub _run_tests {
412432
foreach my $test (@$tests) {
413433
my $test_nm =
414434
$args->{absolute} ? $test : _test_name($test, $args->{root});
435+
415436
warn "$test_nm->Test2::Aggregate\n" if $args->{test_warnings};
416437

417438
$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
730751
the tests listed will be excluded (you will have them running aggregated through
731752
their own C<.t> files using L<Test2::Aggregate>).
732753
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+
733761
=head1 AUTHOR
734762
735763
Dimitrios Kechagias, C<< <dkechag at cpan.org> >>

t/dirs.t

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use Test2::V0;
22
use Test2::Aggregate;
33

4-
plan(5);
4+
plan(7);
55

66
my $root = (grep {/^\.$/i} @INC) ? undef : './';
77

@@ -23,3 +23,12 @@ Test2::Aggregate::run_tests(
2323
reverse => 1,
2424
root => $root
2525
);
26+
27+
Test2::Aggregate::run_tests(
28+
dirs => ['xt/aggregate'],
29+
load_modules => ['Test2::V0'],
30+
package => 1,
31+
reverse => 1,
32+
root => '../',
33+
relative_root => 1
34+
);

t/warn.t

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ like(
3737
slow => 1
3838
);
3939
Test2::Aggregate::run_tests(
40-
dirs => ['xt/aggregate'],
41-
root => '/xx/',
40+
dirs => ['xt/aggregate'],
41+
root => '/xx/',
42+
relative_root => 1
4243
);
4344
},
4445
[qr/Root .* does not exist/],
@@ -86,7 +87,7 @@ done_testing;
8687

8788
sub check_output {
8889
my $run = shift;
89-
my $abs = shift;
90+
my $abs = shift || 0;
9091
my $msg = shift;
9192
my $r = shift || $root || '';
9293
my %warn = ();
@@ -111,6 +112,6 @@ sub check_output {
111112
'pass_perc' => 100
112113
}
113114
},
114-
"Correct output - $msg"
115+
"Correct output - abs:$abs $msg"
115116
);
116117
}

0 commit comments

Comments
 (0)