blib/lib/Test2/Aggregate/Helper.pm

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