blib/lib/Test2/Aggregate/Helper.pm

filter: Statementstmt 96.1% 125 / 130 Branchbran 76.8% 63 / 82 Conditioncond 46.4% 13 / 28 MC/DCmcdc 17.6% 3 / 17 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 81.0% 227 / 280 CC 58 SCAR 24.7
CC
58
Cov
84%
setup_options
20.4
generate_code
20.2
create_command
10.0
CRAP 11.8
« lib/Test2/Aggregate.pm ↑ Summary
1 package Test2::Aggregate::Helper;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Test2::Aggregate::Helper - Common functions for Test2::Aggregate helper scripts
13  
14 =head1 SYNOPSIS
15  
16 See L<agg> and L<aggit> helper scripts.
17  
18 =cut
19  
20 1 our $VERSION = '1.01';
21  
22 =head1 FUNCTIONS
23  
24 =head2 setup_options(\%opt)
25  
26 Normalizes command-line options used by the helper scripts. This sets defaults,
27 validates paths and regexes, prepares include/exclude patterns, and falls back to
28 C<prove> when L<Test2::Harness> is unavailable. An C<exclude_list> is resolved
29 to canonical paths (relative entries against C<root> or the current directory).
30  
31 =head2 is_excluded(\%opt, $test)
32  
33 Returns true if C<$test> (a path to an existing test file) is one of the
34 entries of the C<exclude_list> processed by C<setup_options>.
35  
36 =head2 canonical_path($path)
37  
38 Returns the absolute path with symlinks and C<..> resolved where possible.
39  
40 =head2 create_command(\%opt, $file, $args_only)
41  
42 Builds the C<prove> or C<yath> command used to run the generated aggregate test
43 file. C<include> may be a single path or an array ref of paths. When
44 C<$args_only> is true, only the arguments are returned.
45  
46 =head2 generate_code(\%opt, @paths)
47  
48 Generates a standalone aggregate test file from normalized helper options and
49 the supplied test files, directories, or list files.
50  
51 =cut
52  
53 8 ▶sub setup_options {
Subroutine setup_options: called CC=17 SCAR=30.2
Pod documented
54 8 my $opt = shift;
55  
56 8 ▶ unless ($opt->{prove}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt->{"prove"}) 2 6 8
57 0 ▶ eval "use Test2::Harness";
Subroutine BEGIN: called CC=1 SCAR=1.7
Subroutine BEGIN: called CC=1 SCAR=1.7
58 2 ▶ $opt->{prove} = 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 0 2
59 }
60 8 ▶ $opt->{test_bundle} ||= $opt->{prove} ? 'Test::More' : 'Test2::V0';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'prove'} ? : 7 0 7
Condition66%2/3
$opt->{"test_bundle"} ||= $opt->{"prove"} ? "Test::More" : "Test2::V0"
l!l&&r!l&&!r
170
MC/DC0%0/2
$opt->{"test_bundle"} ||= $opt->{"prove"} ? "Test::More" : "Test2::V0"
$opt->{"test_bundle"} $opt->{"prove"} ? "Test::More" : "Test2::V0"
Truth table66%2/3
A ||= B
A= $opt->{"test_bundle"}B= $opt->{"prove"} ? "Test::More" : "Test2::V0"
ABresult
1X1
011
000
61 8 ▶ $opt->{stats_output} = $opt->{pass_output} if $opt->{pass_output};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"pass_output"} 0 8 8
62 8 ▶ $opt->{out} =~ s/\.t$// if $opt->{out};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"out"} 0 8 8
63 8 ▶ delete $opt->{event_timeout} if $opt->{prove};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"prove"} 8 0 8
64 $opt->{exclude_pattern} = _create_exclude_pattern($opt)
65 8 ▶ if $opt->{exclude_regex};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"exclude_regex"} 1 7 8
66 $opt->{include_pattern} = _create_include_pattern($opt)
67 8 ▶ if $opt->{include_regex};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"include_regex"} 2 6 8
68  
69 7 ▶ if ($opt->{root}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'root'}) { } 2 5 7
elsif (not $opt->{'absolute'}) { } 4 1 5
70 2 $opt->{absolute} = undef;
71 2 ▶ croak "Specified --root does not exist" unless -d $opt->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $opt->{"root"} 0 2 2
72 2 $opt->{root} = "" . path($opt->{root})->absolute;
73 2 foreach (@ARGV) {
74 1 ▶ my $path = path($_)->is_absolute ? path($_) : path($opt->{root}, $_);
Branch: 1 branch
BranchTrueFalseTotal
path($_)->is_absolute ? : 1 0 1
75 1 ▶ croak "$path doesn't exist" unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 1 1
76 }
77 } elsif (!$opt->{absolute}) {
78 4 $opt->{root} = "./";
79 4 $opt->{relative_root} = 1;
80 }
81  
82 7 ▶ $opt->{exclude_paths} = _read_exclude_list($opt) if $opt->{exclude_list};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"exclude_list"} 3 4 7
83 }
84  
85 8 ▶sub is_excluded {
Subroutine is_excluded: called CC=3 SCAR=11.0
Pod documented
86 8 my $opt = shift;
87 8 my $test = shift;
88  
89 8 ▶ return 0 unless $opt->{exclude_paths};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"exclude_paths"} 0 8 8
90 8 ▶ return $opt->{exclude_paths}->{canonical_path($test)} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'exclude_paths'}{canonical_path($test)} ? : 6 2 8
91 }
92  
93 11 ▶sub canonical_path {
Subroutine canonical_path: called CC=2 SCAR=7.4
Pod documented
94 11 my $path = path(shift)->absolute;
95 11 my $real = eval { $path->realpath };
96 11 ▶ return "" . ($real || $path);
Condition33%1/3
$real || $path
l!l&&r!l&&!r
1100
MC/DC0%0/2
$real || $path
$real $path
Truth table33%1/3
A || B
A= $realB= $path
ABresult
1X1
011
000
97 }
98  
99 6 ▶sub create_command {
Subroutine create_command: called CC=10 SCAR=23.0
Pod documented
100 6 my $opt = shift;
101 6 my $file = shift;
102 6 my $args_only = shift;
103  
104 6 ▶ my $cmd = $opt->{prove} ? 'prove' : 'yath';
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'prove'} ? : 3 3 6
105 1 ▶ my @inc = ref $opt->{include} ? @{$opt->{include}}
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'include'} ? : 4 1 5
106 6 ▶ : defined $opt->{include} ? $opt->{include} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $opt->{'include'} ? : 1 5 6
107 6 my @args = ($file);
108 6 ▶ unshift @args, '-v' if $opt->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"verbose"} 5 1 6
109 6 unshift @args, map { "-I$_" } grep { length } @inc;
110 unshift @args, '--et', $opt->{event_timeout}
111 6 ▶ if !$opt->{prove} && defined $opt->{event_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not $opt->{"prove"} and defined $opt->{"event_timeout"} 3 3 6
Condition66%2/3
not $opt->{"prove"} and defined $opt->{"event_timeout"}
!ll&&!rl&&r
303
MC/DC50%1/2
not $opt->{"prove"} and defined $opt->{"event_timeout"}
not $opt->{"prove"} defined $opt->{"event_timeout"}
Truth table66%2/3
not(A) and B
A= not $opt->{"prove"}B= defined $opt->{"event_timeout"}
ABresult
0X0
100
111
112 6 ▶ unshift @args, 'test' unless $opt->{prove} || $args_only;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"prove"} or $args_only 2 4 6
Condition100%3/3
$opt->{"prove"} or $args_only
l!l&&r!l&&!r
312
MC/DC100%2/2
$opt->{"prove"} or $args_only
$opt->{"prove"} $args_only
Truth table100%3/3
A or B
A= $opt->{"prove"}B= $args_only
ABresult
1X1
011
000
113  
114 6 ▶ return @args if $args_only;
Branch: 1 branch
BranchTrueFalseTotal
if $args_only 1 5 6
115 5 return $cmd, @args;
116 }
117  
118 3 ▶sub generate_code {
Subroutine generate_code: called CC=15 SCAR=30.1
Pod documented
119 3 my $opt = { %{shift()} };
120 3 ▶ $opt->{root} ||= "" . path('.')->absolute if $opt->{lists} && $opt->{absolute};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"lists"} and $opt->{"absolute"} 0 3 3
Condition16%1/6
$opt->{"lists"} and $opt->{"absolute"}
!ll&&!rl&&r
300
$opt->{"root"} ||= '' . path(".")->absolute
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"lists"} and $opt->{"absolute"}
$opt->{"lists"} $opt->{"absolute"}
MC/DC0%0/2
$opt->{"root"} ||= '' . path(".")->absolute
$opt->{"root"} '' . path(".")->absolute
Truth table33%1/3
A and B
A= $opt->{"lists"}B= $opt->{"absolute"}
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $opt->{"root"}B= '' . path(".")->absolute
ABresult
1X1
011
000
121 my $code = _test_bundle_use($opt->{test_bundle})
122 3 . "\nuse Test2::Aggregate;\n\nTest2::Aggregate::run_tests(\n";
123 3 for (qw/test_warnings reverse shuffle sort dry_run bail_on_fail relative_root absolute/) {
124 24 ▶ $code .= "\t$_ => 1,\n" if $opt->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{$_} 3 21 24
125 }
126 3 for (qw/stats_output root _progress_file/) {
127 9 ▶ next unless $opt->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{$_} 8 1 9
128 1 my $value = $opt->{$_};
129 1 ▶ if ($_ eq 'root' && $opt->{lists} && $opt->{relative_root}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "root" and $opt->{"lists"} and $opt->{"relative_root"}) 0 1 1
Condition33%2/6
$_ eq "root" and $opt->{"lists"} and $opt->{"relative_root"}
!ll&&!rl&&r
100
$_ eq "root" and $opt->{"lists"}
!ll&&!rl&&r
010
MC/DC0%0/3
$_ eq "root" and $opt->{"lists"} and $opt->{"relative_root"}
$_ eq "root" $opt->{"lists"} $opt->{"relative_root"}
Truth table25%1/4
A and B and C
A= $_ eq "root"B= $opt->{"lists"}C= $opt->{"relative_root"}
ABCresult
0XX0
10X0
1100
1111
130 0 $value = path($value)->absolute->relative($opt->{file}->parent->absolute);
131 }
132 1 $code .= "\t$_ => " . _quote_perl_string($value) . ",\n";
133 }
134 $code .= "\texclude => " . _quote_perl_string($opt->{exclude_pattern}) . ",\n"
135 3 ▶ if $opt->{exclude_pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"exclude_pattern"} 1 2 3
136 $code .= "\tinclude => " . _quote_perl_string($opt->{include_pattern}) . ",\n"
137 3 ▶ if $opt->{include_pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"include_pattern"} 1 2 3
138 3 ▶ $code .= "\tpass_only => 1,\n" if $opt->{pass_output};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"pass_output"} 0 3 3
139 3 ▶ $code .= $opt->{lists} ? "\tlists" : "\tdirs";
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'lists'} ? : 0 3 3
140 3 $code .= " => [\n\t\t";
141 3 $code .= join(",\n\t\t", map {_quote_perl_string($_)} _absolute_paths($opt, @_));
142 3 $code .= "\n\t]\n);\n\ndone_testing();\n";
143 3 return $code;
144 }
145  
146 3 ▶sub _test_bundle_use {
Subroutine _test_bundle_use: called CC=2 SCAR=6.9
147 3 my @modules = split /,/, shift;
148 3 my $str = '';
149 3 $str .= "use $_;\n" for @modules;
150 3 return $str;
151 }
152  
153 3 ▶sub _absolute_paths {
Subroutine _absolute_paths: called CC=7 SCAR=19.8
154 3 my $opt = shift;
155 3 my $t_file = $opt->{file}->parent->absolute;
156 3 my @norm;
157 3 foreach my $input (@_) {
158 3 my $path = $input;
159 3 ▶ unless (File::Spec->file_name_is_absolute($path)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($path)) 3 0 3
160 3 ▶ if ($opt->{relative_root}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'relative_root'}) { } 1 2 3
elsif ($opt->{'absolute'}) { } 2 0 2
161 1 my $abs = path($opt->{root}, $path)->absolute;
162 # List entries also use root, so retain the input root for lists.
163 1 ▶ my $base = $opt->{lists} ? path($opt->{root})->absolute : $t_file;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'lists'} ? : 0 1 1
164 1 $path = $abs->relative($base);
165 } elsif ($opt->{absolute}) {
166 2 ▶ $path = path($opt->{root} || '.', $path)->absolute;
Condition50%1/2
$opt->{'root'} || '.'
l!l
02
MC/DC0%0/1
$opt->{'root'} || '.'
$opt->{'root'}
Truth table50%1/2
A
A= $opt->{'root'}
Aresult
11
00
167 }
168 }
169 3 push @norm, $path;
170 }
171  
172 3 return @norm;
173 }
174  
175 1 ▶sub _create_exclude_pattern {
Subroutine _create_exclude_pattern: called CC=2 SCAR=7.0
176 1 my ($opt) = @_;
177  
178 1 eval { qr/$opt->{exclude_regex}/ };
179 1 ▶ croak "Invalid --exclude_regex '$opt->{exclude_regex}': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
180  
181 1 return "(?:$opt->{exclude_regex})";
182 }
183  
184 # Exclude list entries are resolved against --root (or the current directory)
185 # to canonical paths, so they match regardless of where the generated runner
186 # lives (e.g. a temp directory) and of how the discovered path is spelled.
187 3 ▶sub _read_exclude_list {
Subroutine _read_exclude_list: called CC=7 SCAR=19.7
188 3 my ($opt) = @_;
189  
190 3 ▶ croak "Specified --exclude_list does not exist" unless -e $opt->{exclude_list};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $opt->{"exclude_list"} 1 2 3
191 2 ▶ my $base = path($opt->{root} || '.')->absolute;
Condition50%1/2
$opt->{'root'} || '.'
l!l
20
MC/DC0%0/1
$opt->{'root'} || '.'
$opt->{'root'}
Truth table50%1/2
A
A= $opt->{'root'}
Aresult
11
00
192 2 my %paths;
193 2 foreach my $line (split /\r?\n/, path($opt->{exclude_list})->slurp_utf8) {
194 4 ▶ next if $line =~ /^\s*(?:#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(?:#|$)/ 1 3 4
195 3 $line =~ s/^\s+|\s+$//g;
196 3 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 3 3
197  
198 3 ▶ my $test = path($line)->is_absolute ? path($line) : path($base, $line);
Branch: 1 branch
BranchTrueFalseTotal
path($line)->is_absolute ? : 0 3 3
199 3 $paths{canonical_path($test)} = 1;
200 }
201  
202 2 return \%paths;
203 }
204  
205 2 ▶sub _create_include_pattern {
Subroutine _create_include_pattern: called CC=2 SCAR=6.9
206 2 my ($opt) = @_;
207  
208 2 eval { qr/$opt->{include_regex}/ };
209 2 ▶ croak "Invalid --include_regex '$opt->{include_regex}': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
210  
211 1 return "(?:$opt->{include_regex})";
212 }
213  
214 6 ▶sub _quote_perl_string {
Subroutine _quote_perl_string: called CC=1 SCAR=0.0
215 6 my ($value) = @_;
216 6 $value = "$value";
217 6 $value =~ s/\\/\\\\/g;
218 6 $value =~ s/'/\\'/g;
219 6 return "'$value'";
220 }
221  
222 1 1;
« lib/Test2/Aggregate.pm ↑ Summary