lib/Perl/Critic/TestUtils.pm

filter: Statementstmt 50.4% 109 / 216 Branchbran 5.5% 5 / 90 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.9% 30 / 38 Podpod 100% 14 / 14 totaltotal 43.4% 159 / 366 CC 49 SCAR 47.1
CC
49
Cov
37%
_subtests_from_file
380.0
_globals_from_file
90.0
_finalize_subtest
90.0
CRAP 110.5
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitSingleCharAlternation.pm ↑ Summary lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm »
1 package Perl::Critic::TestUtils;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use File::Spec::Unix ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 40 ▶use Perl::Critic;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use Perl::Critic::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Perl::Critic::Exception::Fatal::Generic qw( throw_generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use Perl::Critic::Exception::Fatal::Internal qw( throw_internal );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use Perl::Critic::UserProfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Perl::Critic::Utils qw( :data_conversion );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::PolicyFactory (-test => 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 40 our $VERSION = '1.156';
27  
28 40 Readonly::Array our @EXPORT_OK => qw(
29 pcritique pcritique_with_violations
30 critique critique_with_violations
31 fcritique fcritique_with_violations
32 subtests_in_tree
33 should_skip_author_tests
34 get_author_test_skip_message
35 starting_points_including_examples
36 bundled_policy_names
37 names_of_policies_willing_to_work
38 );
39  
40 39 ▶sub assert_version {
Subroutine assert_version: called CC=2 SCAR=8.4
Pod documented
41 39 my $expected_version = shift;
42  
43 39 ▶ if ( $expected_version ne $Perl::Critic::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected_version ne $Perl::Critic::VERSION) 0 39 39
44 0 require Carp;
45 0 Carp::confess( "Expected Perl::Critic $expected_version but it is actually $Perl::Critic::VERSION" );
46 }
47  
48 39 return;
49 }
50  
51 #-----------------------------------------------------------------------------
52 # If the user already has an existing perlcriticrc file, it will get
53 # in the way of these test. This little tweak to ensures that we
54 # don't find the perlcriticrc file.
55  
56 19 ▶sub block_perlcriticrc {
Subroutine block_perlcriticrc: called CC=1 SCAR=0.0
Pod documented
57 40 ▶ no warnings 'redefine'; ## no critic (ProhibitNoWarnings);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 19 ▶ *Perl::Critic::UserProfile::_find_profile_path = sub { return }; ## no critic (ProtectPrivateVars)
Subroutine __ANON__: called CC=1 SCAR=0.0
59 19 return 1;
60 }
61  
62 #-----------------------------------------------------------------------------
63 # Criticize a code snippet using only one policy. Returns the violations.
64  
65 23 ▶sub pcritique_with_violations {
Subroutine pcritique_with_violations: called CC=1 SCAR=0.0
Pod documented
66 23 my($policy, $code_ref, $config_ref) = @_;
67 23 my $c = Perl::Critic->new( -profile => 'NONE' );
68 23 $c->add_policy(-policy => $policy, -config => $config_ref);
69 23 return $c->critique($code_ref);
70 }
71  
72 #-----------------------------------------------------------------------------
73 # Criticize a code snippet using only one policy. Returns the number
74 # of violations
75  
76 23 ▶sub pcritique { ##no critic(ArgUnpacking)
Subroutine pcritique: called CC=1 SCAR=0.0
Pod documented
77 23 return scalar pcritique_with_violations(@_);
78 }
79  
80 #-----------------------------------------------------------------------------
81 # Criticize a code snippet using a specified config. Returns the violations.
82  
83 32 ▶sub critique_with_violations {
Subroutine critique_with_violations: called CC=1 SCAR=0.0
Pod documented
84 32 my ($code_ref, $config_ref) = @_;
85 32 my $c = Perl::Critic->new( %{$config_ref} );
86 32 return $c->critique($code_ref);
87 }
88  
89 #-----------------------------------------------------------------------------
90 # Criticize a code snippet using a specified config. Returns the
91 # number of violations
92  
93 32 ▶sub critique { ##no critic(ArgUnpacking)
Subroutine critique: called CC=1 SCAR=0.0
Pod documented
94 32 return scalar critique_with_violations(@_);
95 }
96  
97 #-----------------------------------------------------------------------------
98 # Like pcritique_with_violations, but forces a PPI::Document::File context.
99 # The $filename arg is a Unix-style relative path, like 'Foo/Bar.pm'
100  
101 40 Readonly::Scalar my $TEMP_FILE_PERMISSIONS => oct 700;
102  
103 27 ▶sub fcritique_with_violations {
Subroutine fcritique_with_violations: called CC=6 SCAR=18.8
Pod documented
104 27 my($policy, $code_ref, $filename, $config_ref) = @_;
105 27 my $c = Perl::Critic->new( -profile => 'NONE' );
106 27 $c->add_policy(-policy => $policy, -config => $config_ref);
107  
108 27 my $dir = File::Temp::tempdir( 'PerlCritic-tmpXXXXXX', TMPDIR => 1 );
109 27 ▶ $filename ||= 'Temp.pm';
Condition50%1/2
$filename ||= "Temp.pm"
l!l
027
MC/DC0%0/1
$filename ||= "Temp.pm"
$filename
Truth table50%1/2
A
A= $filename
Aresult
11
00
110 27 my @fileparts = File::Spec::Unix->splitdir($filename);
111 27 ▶ if (@fileparts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fileparts > 1) 0 27 27
112 0 my $subdir = File::Spec->catdir($dir, @fileparts[0..$#fileparts-1]);
113 0 File::Path::mkpath($subdir, 0, $TEMP_FILE_PERMISSIONS);
114 }
115 27 my $file = File::Spec->catfile($dir, @fileparts);
116 27 ▶ if (open my $fh, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, ">", $file) 27 0 27
117 27 print {$fh} ${$code_ref};
118 27 ▶ close $fh or throw_generic "unable to close $file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or throw_generic("unable to close ${file}: $Perl::Critic::TestUtils::OS_ERROR") 27 0 27
119 }
120  
121 # Use eval so we can clean up before throwing an exception in case of
122 # error.
123 27 my @v = eval {$c->critique($file)};
124 27 my $err = $EVAL_ERROR;
125 27 File::Path::rmtree($dir, 0, 1);
126 27 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 27 27
127 0 throw_generic $err;
128 }
129 27 return @v;
130 }
131  
132 #-----------------------------------------------------------------------------
133 # Like pcritique, but forces a PPI::Document::File context. The
134 # $filename arg is a Unix-style relative path, like 'Foo/Bar.pm'
135  
136 27 ▶sub fcritique { ##no critic(ArgUnpacking)
Subroutine fcritique: called CC=1 SCAR=0.0
Pod documented
137 27 return scalar fcritique_with_violations(@_);
138 }
139  
140 # Note: $include_extras is not documented in the POD because I'm not
141 # committing to the interface yet.
142 0 ▶sub subtests_in_tree {
Subroutine subtests_in_tree: not called CC=1 SCAR=6.9
Pod documented
143 0 my ($start, $include_extras) = @_;
144  
145 0 my %subtests;
146  
147 find(
148 {
149 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
150 0 ▶ return if not -f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
151  
152 0 my ($fileroot) = m{(.+)[.]run\z}xms;
153  
154 0 ▶ return if not $fileroot;
Branch: 1 branch
BranchTrueFalseTotal
unless $fileroot 0 0 0
155  
156 0 my @pathparts = File::Spec->splitdir($fileroot);
157 0 ▶ if (@pathparts < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pathparts < 2) 0 0 0
158 0 throw_internal 'confusing policy test filename ' . $_;
159 }
160  
161 0 my $policy = join q{::}, @pathparts[-2, -1]; ## no critic (MagicNumbers)
162  
163 0 my $globals = _globals_from_file( $_ );
164 0 ▶ if ( my $prerequisites = $globals->{prerequisites} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prerequisites = $globals->{"prerequisites"}) 0 0 0
165 0 foreach my $prerequisite ( keys %{$prerequisites} ) {
166 0 ▶ eval "require $prerequisite; 1" or return;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $prerequisite; 1" or return 0 0 0
167 }
168 }
169  
170 0 my @subtests = _subtests_from_file( $_ );
171  
172 0 ▶ if ($include_extras) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_extras) { } 0 0 0
173 0 $subtests{$policy} =
174 { subtests => [ @subtests ], globals => $globals };
175 }
176 else {
177 0 $subtests{$policy} = [ @subtests ];
178 }
179  
180 0 return;
181 },
182 0 no_chdir => 1,
183 },
184 $start
185 );
186  
187 0 return \%subtests;
188 }
189  
190 # Answer whether author test should be run.
191 #
192 # Note: this code is duplicated in
193 # t/tlib/Perl/Critic/TestUtilitiesWithMinimalDependencies.pm.
194 # If you change this here, make sure to change it there.
195  
196 0 ▶sub should_skip_author_tests {
Subroutine should_skip_author_tests: not called CC=1 SCAR=6.9
Pod documented
197 0 return not $ENV{TEST_AUTHOR_PERL_CRITIC};
198 }
199  
200 0 ▶sub get_author_test_skip_message {
Subroutine get_author_test_skip_message: not called CC=1 SCAR=6.9
Pod documented
201 ## no critic (RequireInterpolation);
202 0 return 'Author test. Set $ENV{TEST_AUTHOR_PERL_CRITIC} to a true value to run.';
203 }
204  
205  
206 0 ▶sub starting_points_including_examples {
Subroutine starting_points_including_examples: not called CC=2 SCAR=17.9
Pod documented
207 0 ▶ return (-e 'blib' ? 'blib' : 'lib', 'examples');
Branch: 1 branch
BranchTrueFalseTotal
-e 'blib' ? : 0 0 0
208 }
209  
210 0 ▶sub _globals_from_file {
Subroutine _globals_from_file: not called CC=9 SCAR=45.0
211 0 my $test_file = shift;
212  
213 0 my %valid_keys = hashify qw< prerequisites >;
214  
215 0 ▶ return if -z $test_file; # Skip if the Policy has a regular .t file.
Branch: 1 branch
BranchTrueFalseTotal
if -z $test_file 0 0 0
216  
217 0 my %globals;
218  
219 0 ▶ open my $handle, '<', $test_file ## no critic (RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $test_file or throw_internal("Couldn't open ${test_file}: $Perl::Critic::TestUtils::OS_ERROR") 0 0 0
220 or throw_internal "Couldn't open $test_file: $OS_ERROR";
221  
222 0 ▶ while ( my $line = <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 0 0 0
223 0 chomp;
224  
225 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /\A [#][#] [ ] global [ ] (\S+) (?:\s+(.+))? /msx) 0 0 0
226 my ($key,$value) =
227 $line =~ m<\A [#][#] [ ] global [ ] (\S+) (?:\s+(.+))? >xms
228 ) {
229 0 ▶ next if not $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
230 0 ▶ if ( not $valid_keys{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid_keys{$key}) 0 0 0
231 0 throw_internal "Unknown global key $key in $test_file";
232 }
233  
234 0 ▶ if ( $key eq 'prerequisites' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "prerequisites") 0 0 0
235 0 $value = { hashify( words_from_string($value) ) };
236 }
237 0 $globals{$key} = $value;
238 }
239 }
240 0 ▶ close $handle or throw_generic "unable to close $test_file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or throw_generic("unable to close ${test_file}: $Perl::Critic::TestUtils::OS_ERROR") 0 0 0
241  
242 0 return \%globals;
243 }
244  
245 # The internal representation of a subtest is just a hash with some
246 # named keys. It could be an object with accessors for safety's sake,
247 # but at this point I don't see why.
248 0 ▶sub _subtests_from_file {
Subroutine _subtests_from_file: not called CC=19 SCAR=59.4
249 0 my $test_file = shift;
250  
251 0 my %valid_keys = hashify qw( name failures parms TODO error filename optional_modules );
252  
253 0 ▶ return if -z $test_file; # Skip if the Policy has a regular .t file.
Branch: 1 branch
BranchTrueFalseTotal
if -z $test_file 0 0 0
254  
255 0 ▶ open my $fh, '<', $test_file ## no critic (RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $test_file or throw_internal("Couldn't open ${test_file}: $Perl::Critic::TestUtils::OS_ERROR") 0 0 0
256 or throw_internal "Couldn't open $test_file: $OS_ERROR";
257  
258 0 my @subtests;
259  
260 0 my $incode = 0;
261 0 my $cut_in_code = 0;
262 0 my $subtest;
263 my $lineno;
264 0 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
265 0 ++$lineno;
266 0 chomp;
267 0 my $inheader = /^## name/ .. /^## cut/; ## no critic (ExtendedFormatting LineBoundaryMatching DotMatchAnything)
268  
269 0 my $line = $_;
270  
271 0 ▶ if ( $inheader ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($inheader) { } 0 0 0
elsif ($subtest) { } 0 0 0
elsif (@subtests) { } 0 0 0
272 0 ▶ $line =~ m/\A [#]/xms or throw_internal "Code before cut: $test_file";
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\A [#]/msx or throw_internal("Code before cut: $test_file") 0 0 0
273 0 my ($key,$value) = $line =~ m/\A [#][#] [ ] (\S+) (?:\s+(.+))? /xms;
274 0 ▶ next if !$key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
275 0 ▶ next if $key eq 'cut';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "cut" 0 0 0
276 0 ▶ if ( not $valid_keys{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid_keys{$key}) 0 0 0
277 0 throw_internal "Unknown key $key in $test_file";
278 }
279  
280 0 ▶ if ( $key eq 'name' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "name") 0 0 0
281 0 ▶ if ( $subtest ) { # Stash any current subtest
Branch: 1 branch
BranchTrueFalseTotal
if ($subtest) 0 0 0
282 0 push @subtests, _finalize_subtest( $subtest );
283 0 undef $subtest;
284 }
285 0 $subtest->{lineno} = $lineno;
286 0 $incode = 0;
287 0 $cut_in_code = 0;
288 }
289 0 ▶ if ($incode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incode) 0 0 0
290 0 throw_internal "Header line found while still in code: $test_file";
291 }
292 0 $subtest->{$key} = $value;
293 }
294 elsif ( $subtest ) {
295 0 $incode = 1;
296 0 ▶ $cut_in_code ||= $line =~ m/ \A [#][#] [ ] cut \z /smx;
Condition0%0/3
$cut_in_code ||= $line =~ / \A [#][#] [ ] cut \z /msx
l!l&&r!l&&!r
000
MC/DC0%0/2
$cut_in_code ||= $line =~ / \A [#][#] [ ] cut \z /msx
$cut_in_code $line =~ / \A [#][#] [ ] cut \z /msx
Truth table0%0/3
A ||= B
A= $cut_in_codeB= $line =~ / \A [#][#] [ ] cut \z /msx
ABresult
1X1
011
000
297 # Don't start a subtest if we're not in one.
298 # Don't add to the test if we have seen a '## cut'.
299 0 ▶ $cut_in_code or push @{$subtest->{code}}, $line;
Branch: 1 branch
BranchTrueFalseTotal
$cut_in_code or push @{$$subtest{"code"};}, $line 0 0 0
300 }
301 elsif (@subtests) {
302 ## don't complain if we have not yet hit the first test
303 0 throw_internal "Got some code but I'm not in a subtest: $test_file";
304 }
305 }
306 0 ▶ close $fh or throw_generic "unable to close $test_file: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or throw_generic("unable to close ${test_file}: $Perl::Critic::TestUtils::OS_ERROR") 0 0 0
307 0 ▶ if ( $subtest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtest) 0 0 0
308 0 ▶ if ( $incode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incode) { } 0 0 0
309 0 push @subtests, _finalize_subtest( $subtest );
310 }
311 else {
312 0 throw_internal "Incomplete subtest in $test_file";
313 }
314 }
315  
316 0 return @subtests;
317 }
318  
319 0 ▶sub _finalize_subtest {
Subroutine _finalize_subtest: not called CC=9 SCAR=45.0
320 0 my $subtest = shift;
321  
322 0 ▶ if ( $subtest->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtest->{'code'}) { } 0 0 0
323 0 $subtest->{code} = join "\n", @{$subtest->{code}};
324 }
325 else {
326 0 throw_internal "$subtest->{name} has no code lines";
327 }
328 0 ▶ if ( !defined $subtest->{failures} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subtest->{"failures"}) 0 0 0
329 0 throw_internal "$subtest->{name} does not specify failures";
330 }
331 0 ▶ if ($subtest->{parms}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtest->{'parms'}) { } 0 0 0
332 0 $subtest->{parms} = eval $subtest->{parms}; ## no critic(StringyEval)
333 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Critic::TestUtils::EVAL_ERROR) 0 0 0
334 0 throw_internal
335 "$subtest->{name} has an error in the 'parms' property:\n"
336 . $EVAL_ERROR;
337 }
338 0 ▶ if ('HASH' ne ref $subtest->{parms}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" ne ref $subtest->{"parms"}) 0 0 0
339 0 throw_internal
340 "$subtest->{name} 'parms' did not evaluate to a hashref";
341 }
342 } else {
343 0 $subtest->{parms} = {};
344 }
345  
346 0 ▶ if (defined $subtest->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subtest->{"error"}) 0 0 0
347 0 ▶ if ( $subtest->{error} =~ m{ \A / (.*) / \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subtest->{"error"} =~ m[ \A / (.*) / \z ]msx) 0 0 0
348 0 $subtest->{error} = eval {qr/$1/}; ## no critic (ExtendedFormatting LineBoundaryMatching DotMatchAnything)
349 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Critic::TestUtils::EVAL_ERROR) 0 0 0
350 0 throw_internal
351 "$subtest->{name} 'error' has a malformed regular expression";
352 }
353 }
354 }
355  
356 0 return $subtest;
357 }
358  
359 6 ▶sub bundled_policy_names {
Subroutine bundled_policy_names: called CC=1 SCAR=0.0
Pod documented
360 6 require ExtUtils::Manifest;
361 6 my $manifest = ExtUtils::Manifest::maniread();
362 6 my @policy_paths = map {m{\A lib/(Perl/Critic/Policy/.*).pm \z}xms} keys %{$manifest};
363 6 my @policies = map { join q{::}, split m{/}xms } @policy_paths;
364 6 my @sorted_policies = sort @policies;
365 6 return @sorted_policies;
366 }
367  
368 1 ▶sub names_of_policies_willing_to_work {
Subroutine names_of_policies_willing_to_work: called CC=1 SCAR=0.0
Pod documented
369 1 my %configuration = @_;
370  
371 1 my @policies_willing_to_work =
372 Perl::Critic::Config
373 ->new( %configuration )
374 ->policies();
375  
376 1 return map { ref } @policies_willing_to_work;
377 }
378  
379 40 1;
380  
381 __END__
« lib/Perl/Critic/Policy/RegularExpressions/ProhibitSingleCharAlternation.pm ↑ Summary lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm »