Skip to content

Commit 9e53c0d

Browse files
committed
aggit, no_dot, bail_on_fail
1 parent 4469a4d commit 9e53c0d

File tree

7 files changed

+77
-113
lines changed

7 files changed

+77
-113
lines changed

Changes

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Revision history for Test2-Aggregate
22

3-
0.18 2024-07-01
3+
0.18 2024-07-03
44
agg helper script.
5+
aggit helper script.
56
'pass_only' option.
7+
'bail_on_fail' option.
68
Write stats per test file (repeat==1) to avoid losing them on SIG exit.
79
Test names without $root for stats, 'absolute' option for old behaviour.
10+
Add './' to start of paths when required, 'no_dot' option for old behaviour.
811
'reverse' applied after 'sort'.
912

1013
0.17 2022-02-10

MANIFEST

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
agg
2+
aggit
23
Changes
34
lib/Test2/Aggregate.pm
5+
lib/Test2/Aggregate/Helper.pm
46
Makefile.PL
57
MANIFEST This list of files
68
MANIFEST.SKIP

Makefile.PL

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ my %WriteMakefileArgs = (
1919
CONFIGURE_REQUIRES => {
2020
'ExtUtils::MakeMaker' => '0',
2121
},
22-
EXE_FILES => ["agg"],
22+
EXE_FILES => ["agg", "aggit"],
2323
TEST_REQUIRES => {
2424
'Test::Output' => '0',
2525
'Test2::V0' => '0'

agg

+10-79
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
=head1 NAME
44

5-
agg - Test2::Aggregate harness wrapper
5+
agg - Test2::Aggregate harness wrapper / helper script
66

77
=head1 DESCRIPTION
88

99
Pass a list of Perl test files/directories and they will run aggregated via yath
10-
(or prove if specified).
10+
(or prove if specified). It is not meant to be used for .t files that use Test2::Aggregate
11+
themselves.
1112

1213
It is useful either to speed up a test run for tests you know can be aggregated,
13-
or to check whether specific tests can pass under Test2::Aggregate.
14+
or to check whether specific tests can run successfully under Test2::Aggregate.
1415

1516
=head1 SYNOPSIS
1617

@@ -40,12 +41,10 @@ use warnings;
4041

4142
use lib 'lib';
4243

43-
use Carp;
44-
use Cwd;
45-
use File::Temp 'tempfile';
4644
use Getopt::Long;
4745
use Path::Tiny;
4846
use Pod::Usage;
47+
use Test2::Aggregate::Helper;
4948

5049
my %opt = (framework => 'Test2::V0');
5150
GetOptions(
@@ -70,85 +69,17 @@ GetOptions(
7069
pod2usage({-verbose => 1, -output => \*STDOUT, -noperldoc => 1})
7170
if $opt{help} || !@ARGV;
7271

73-
$opt{test_bundle} ||= $opt{prove} ? 'Test::More' : 'Test2::V0';
74-
$opt{stats_output} = $opt{pass_output} if $opt{pass_output};
75-
76-
if ($opt{root}) {
77-
$opt{absolute} = undef;
78-
croak "Specified --root does not exist" unless -e $opt{root};
79-
my $slash = '';
80-
$slash = '/' unless $opt{root} =~ m#/$#;
81-
foreach (@ARGV) {
82-
croak "$opt{root}$slash$_ doesn't exist" unless -e "$opt{root}$slash$_";
83-
}
84-
} elsif (!$opt{absolute}) {
85-
$opt{root} = "./";
86-
}
87-
88-
unless ($opt{prove}) {
89-
eval "use Test2::Harness";
90-
$opt{prove} = 1 if $@;
91-
}
92-
93-
my $file = $opt{out} ? path($opt{out}) : Path::Tiny->tempfile(
72+
Test2::Aggregate::Helper::setup_options(\%opt);
73+
74+
my $file = $opt{out} ? path($opt{out}.'.t') : Path::Tiny->tempfile(
9475
TEMPLATE => "aggXXXXXX",
9576
SUFFIX => '.t',
9677
UNLINK => 0
9778
);
9879

9980
print "Writing output to $file\n" if $opt{verbose};
10081

101-
my $cwd = $opt{root} || getcwd();
102-
my $code = test_bundle_use($opt{test_bundle});
103-
104-
$code .= "
105-
use Test2::Aggregate;
106-
107-
my \$stats = Test2::Aggregate::run_tests(
108-
";
109-
$code .= "\ttest_warnings => 1,\n" if $opt{test_warnings};
110-
$code .= "\tstats_output => '$opt{stats_output}',\n" if $opt{stats_output};
111-
$code .= "\tpass_only => 1,\n" if $opt{pass_output};
112-
$code .= "\treverse => 1,\n" if $opt{reverse};
113-
$code .= "\tshuffle => 1,\n" if $opt{shuffle};
114-
$code .= "\tsort => 1,\n" if $opt{sort};
115-
$code .= "\troot => '$cwd',\n\t";
116-
$code .= $opt{lists} ? "lists" : "dirs";
117-
$code .= " => [\n";
118-
$code .= join(",\n", map {"'$_'"} absolute_paths($cwd, $opt{absolute}, @ARGV));
119-
$code .= "\n\t],
120-
);
121-
done_testing();
122-
";
123-
82+
my $code = Test2::Aggregate::Helper::generate_code(\%opt, @ARGV);
12483
$file->spew_utf8($code);
12584

126-
my $cmd = $opt{prove} ? 'prove' : 'yath';
127-
my @args = ($file);
128-
unshift @args, '-v' if $opt{verbose};
129-
unshift @args, "-I$opt{include}" if $opt{include};
130-
unshift @args, 'test' unless $opt{prove};
131-
132-
exec($cmd, @args);
133-
134-
sub test_bundle_use {
135-
my @modules = split /,/, shift;
136-
my $str = '';
137-
$str .= "use $_;\n" for @modules;
138-
return $str;
139-
}
140-
141-
sub absolute_paths {
142-
my $cwd = shift;
143-
my $abs = shift;
144-
return @_ unless $abs;
145-
146-
my @paths = @_;
147-
my @norm;
148-
149-
$cwd =~ s#/?$#/#;
150-
151-
push @norm, (substr($_, 0, 1) eq '/' ? $_ : "$cwd$_") for @paths;
152-
153-
return @norm;
154-
}
85+
exec(Test2::Aggregate::Helper::create_command(\%opt, $file));

lib/Test2/Aggregate.pm

+55-30
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Test2::Aggregate - Aggregate tests for increased speed
1919
use Test2::V0; # Or 'use Test::More' etc if your suite uses an other framework
2020
2121
Test2::Aggregate::run_tests(
22-
dirs => \@test_dirs
22+
dirs => [@test_dirs_or_files]
2323
);
2424
2525
done_testing();
@@ -76,12 +76,14 @@ have less issues with L<Test2::Suite> (see notes).
7676
unique => 1, # optional
7777
repeat => 1, # optional, requires Test2::Plugin::BailOnFail for < 0
7878
slow => 0, # optional
79+
test_warnings => 0, # optional
7980
override => \%override, # optional, requires Sub::Override
8081
stats_output => $stats_output_path, # optional
8182
extend_stats => 0, # optional
8283
pass_only => 0, # optional
8384
absolute => 0, # optional
84-
test_warnings => 0, # optional
85+
bail_on_fail => 0, # optional, requires Test2::Plugin::BailOnFail
86+
no_dot => 0, # optional
8587
allow_errors => 0, # optional
8688
pre_eval => $code_to_eval, # optional
8789
dry_run => 0, # optional
@@ -242,6 +244,12 @@ but still allow additions in future versions that will only be written with the
242244
C<extend_stats> option enabled.
243245
Additions with C<extend_stats> as of the current version:
244246
247+
=over 4
248+
249+
- starting date/time in ISO_8601.
250+
251+
=back
252+
245253
=item * C<pass_only> (optional)
246254
247255
Modifies C<stats_output> by making it only print out a list of passing tests.
@@ -252,11 +260,15 @@ Has no effect if C<stats_output> is not defined.
252260
253261
Matches pre-v0.18 behaviour by including C<root> to the output of stats.
254262
255-
=over 4
263+
=item * C<no_dot> (optional)
256264
257-
- starting date/time in ISO_8601.
265+
Current version will add a C<./> at the start of a test file if the full path does
266+
not start with C</> or C<.> to avoid the C<'.' is no longer in @INC> error.
267+
Setting C<no_dot> to true matches pre-v0.18 behaviour that did not do this.
258268
259-
=back
269+
=item * C<bail_on_fail> (optional)
270+
271+
Will bail (exit) on first test failure.
260272
261273
=back
262274
@@ -320,6 +332,7 @@ sub run_tests {
320332
}
321333
}
322334
} elsif ($args{test_warnings}) {
335+
eval 'use Test2::Plugin::BailOnFail' if $args{bail_on_fail};
323336
$warnings = _process_warnings(
324337
Test2::V0::warnings { _run_tests(\@tests, \%args) },
325338
\%args
@@ -330,6 +343,7 @@ sub run_tests {
330343
'No warnings in the aggregate tests.'
331344
);
332345
} else {
346+
eval 'use Test2::Plugin::BailOnFail' if $args{bail_on_fail};
333347
_run_tests(\@tests, \%args);
334348
}
335349

@@ -411,9 +425,12 @@ sub _run_tests {
411425
if ($args->{dry_run}) {
412426
Test2::V0::ok($test);
413427
} else {
428+
my $t= $test;
429+
$t = "./$test"
430+
unless $args->{no_dot} || $test =~ m#^[./]#;
414431
$args->{package}
415-
? eval "package Test::$i" . '::' . "$count; do '$test';"
416-
: do $test;
432+
? eval "package Test::$i" . '::' . "$count; do '$t';"
433+
: do $t;
417434
$exec_error = $@;
418435
}
419436
Test2::V0::is($exec_error, '', 'Execution should not fail/warn')
@@ -482,6 +499,7 @@ sub _stats_fh {
482499
my $file = $args->{stats_output}."/".$args->{caller}."-"._timestamp().".txt";
483500
open($fh, '>', $file) or die "Can't open > $file: $!";
484501
}
502+
select($fh); $| = 1; select(STDOUT);
485503

486504
$args->{total_time} = 0;
487505
my $extra = $args->{extend_stats} ? ' TIMESTAMP' : '';
@@ -530,6 +548,36 @@ sub _timestamp {
530548
return sprintf "%04d%02d%02dT%02d%02d%02d", $Y+1900, $M+1, $D, $h, $m, $s;
531549
}
532550

551+
=head1 HELPER SCRIPTS
552+
553+
=head2 agg (Test2::Aggregate harness wrapper)
554+
555+
agg [options] <file/dir1 ...>
556+
557+
Pass a list of Perl test files/directories and they will run aggregated via yath
558+
(or prove if specified). It is not meant to be used for .t files that use Test2::Aggregate
559+
themselves.
560+
561+
It is useful either to speed up a test run for tests you know can be aggregated,
562+
or to check whether specific tests can run successfully under Test2::Aggregate.
563+
564+
Options:
565+
--out <s>, -o <s> : Specify the test file to be created (tmp file by default).
566+
--lists, -l : Use files specified as lists.
567+
--prove, -p : Force prove (default is yath/Test2 if installed).
568+
--verbose, -v : Verbose (passed to yath/prove)
569+
--absolute, -a : Use absolute paths in generated files. Disabled with -r.
570+
--root <s> -r <s> : Define a custom root dir (default is current dir).
571+
--include <s>, -I <s> : Library paths to include.
572+
--test_warnings, -w : Fail tests on warnings.
573+
--test_bundle <s>, -t : Test bundle (def: Test2::V0 or Test::More if -p). Can be comma-separated list.
574+
--pass_output <s>, -po <s> : Output directory for list of 100% passing tests.
575+
--reverse : Reverse the test order.
576+
--shuffle : Shuffle the test order.
577+
--sort : Run the tests alphabetically.
578+
--stats_output <s>, -so <s> : Stats output directory (does not combine with pass_output).
579+
--help -h : Show basic help and exit.
580+
533581
=head1 USAGE NOTES
534582
535583
Not all tests can be modified to run under the aggregator, it is not intended
@@ -613,29 +661,6 @@ disable warnings on redefines only for tests that run aggregated:
613661
Another idea is to make the test die when it is run under the aggregator, if, at
614662
design time, you know it is not supposed to run aggregated.
615663
616-
=head2 agg helper script
617-
618-
agg [options] <file/dir1 ...>
619-
620-
Pass a list of Perl test files/directories and they will run aggregated via yath
621-
(or prove if specified).
622-
623-
Options:
624-
--out <s>, -o <s> : Specify the test file to be created (tmp file by default).
625-
--lists, -l : Use files specified as lists.
626-
--prove, -p : Force prove (default is yath/Test2 if detected).
627-
--verbose, -v : Verbose (passed to yath/prove)
628-
--absolute, -a : Use absolute paths in generated files.
629-
--include <s>, -I <s> : Library paths to include.
630-
--test_warnings, -w : Fail tests on warnings.
631-
--test_bundle <s>, -t : Test bundle (def: Test2::V0 or Test::More if -p). Can be comma-separated list.
632-
--pass_output <s>, -po <s> : Output directory for list of 100% passing tests.
633-
--reverse : Reverse the test order.
634-
--shuffle : Shuffle the test order.
635-
--sort : Run the tests alphabetically.
636-
--stats_output <s>, -so <s> : Stats output directory (does not combine with pass_list).
637-
--help -h : Show basic help and exit.
638-
639664
=head2 Example aggregating strategy
640665
641666
There are many approaches you could do to use C<Test2::Aggregate> with an existing

t/dirs.t

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ my $root = (grep {/^\.$/i} @INC) ? undef : './';
88
Test2::Aggregate::run_tests();
99

1010
my $stats = Test2::Aggregate::run_tests(
11-
dirs => ['xt/aggregate'],
12-
root => $root
11+
dirs => ['xt/aggregate'],
12+
no_dot => 1,
13+
bail_on_fail => 1,
14+
root => $root
1315
);
1416

1517
is(scalar(keys %$stats), 2, 'subtests');

t/unique.t

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $run = Test2::Aggregate::run_tests(
1717
lists => ['xt/aggregate/aggregate.lst'],
1818
root => $root,
1919
unique => 0,
20+
bail_on_fail => 1,
2021
test_warnings => 1
2122
);
2223

0 commit comments

Comments
 (0)