utils/perl-hook

filter: Statementstmt 82.5% 90 / 109 Branchbran 65.0% 26 / 40 Conditioncond 45.8% 11 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 71.1% 143 / 201 CC 33 SCAR 26.2
CC
33
Cov
73%
main
32.6
write_atomic
10.5
format_files
7.3
CRAP 13.8
« utils/commit-msg-hook ↑ Summary lib/Perl/Critic/PJCJ/Fixer.pm »
1 #!/usr/bin/env perl
2  
3 12 ▶use v5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 my $Unreadable = 0;
13  
14 21 ▶sub is_perl ($file) {
Subroutine is_perl: called CC=4 SCAR=13.9
15 21 ▶ return 1 if $file =~ /\.(?:pm|pl|t|psgi)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.(?:pm|pl|t|psgi)\z/u 14 7 21
16 7 my $fh;
17 7 ▶ unless (open $fh, "<", $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 4 3 7
18 4 warn "Cannot read $file: $!\n";
19 4 $Unreadable = 1;
20 4 return 0;
21 }
22 3 ▶ my $first = <$fh> // "";
Condition50%1/2
readline $fh // ""
l!l
30
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
23 3 $first =~ /\A#!.*\bperl\b/
24 }
25  
26 9 ▶sub tidy_source ($source) {
Subroutine tidy_source: called CC=3 SCAR=11.0
27 9 require Perl::Tidy;
28 9 my $tidied = "";
29 9 my $error = eval {
30 9 Perl::Tidy::perltidy(
31 source => \$source,
32 destination => \$tidied,
33 stderr => \my $stderr,
34 errorfile => \my $errors,
35 perltidyrc => ".perltidyrc",
36 argv => [],
37 )
38 };
39 9 ▶ return if !defined $error || $error;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $error or $error 1 8 9
Condition66%2/3
not defined $error or $error
l!l&&r!l&&!r
018
MC/DC50%1/2
not defined $error or $error
not defined $error $error
Truth table66%2/3
not(A) or B
A= not defined $errorB= $error
ABresult
1X1
011
000
40 8 $tidied
41 }
42  
43 7 ▶sub check_tidy ($files) {
Subroutine check_tidy: called CC=5 SCAR=16.1
44 7 my $status = 0;
45 7 for my $file (@$files) {
46 8 my $fh;
47 8 ▶ unless (open $fh, "<", $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 1 7 8
48 1 say "Cannot read $file: $!";
49 1 $status = 1;
50 1 next;
51 }
52 7 my $source = do { local $/; <$fh> };
53 7 my $tidied = tidy_source($source);
54 7 ▶ unless (defined $tidied) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tidied) 1 6 7
55 1 say "perltidy failed: $file";
56 1 $status = 1;
57 1 next;
58 }
59 6 ▶ next if $tidied eq $source;
Branch: 1 branch
BranchTrueFalseTotal
if $tidied eq $source 2 4 6
60 4 say "Not tidy: $file";
61 4 $status = 1;
62 }
63 $status
64 7 }
65  
66 1 ▶sub write_atomic ($file, $content, $mode) {
Subroutine write_atomic: called CC=6 SCAR=23.5
67 my ($out, $temp)
68 1 = eval { tempfile("perl-hook-XXXXXX", DIR => dirname($file), UNLINK => 0) };
69 1 ▶ unless ($out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 1 1
70 0 say "Cannot create temporary file for $file: $@";
71 0 return 0;
72 }
73 1 my $ok = print {$out} $content;
74 1 ▶ $ok &&= close $out;
Condition33%1/3
$ok &&= close $out
!ll&&!rl&&r
001
MC/DC0%0/2
$ok &&= close $out
$ok close $out
Truth table33%1/3
A &&= B
A= $okB= close $out
ABresult
0X0
100
111
75 1 ▶ $ok &&= chmod $mode, $temp;
Condition33%1/3
$ok &&= chmod($mode, $temp)
!ll&&!rl&&r
001
MC/DC0%0/2
$ok &&= chmod($mode, $temp)
$ok chmod($mode, $temp)
Truth table33%1/3
A &&= B
A= $okB= chmod($mode, $temp)
ABresult
0X0
100
111
76 1 ▶ $ok &&= rename $temp, $file;
Condition33%1/3
$ok &&= rename($temp, $file)
!ll&&!rl&&r
001
MC/DC0%0/2
$ok &&= rename($temp, $file)
$ok rename($temp, $file)
Truth table33%1/3
A &&= B
A= $okB= rename($temp, $file)
ABresult
0X0
100
111
77 1 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
78 0 my $error = $!;
79 0 unlink $temp;
80 0 say "Cannot write $file: $error";
81 0 0
82 }
83  
84 2 ▶sub format_files ($files) {
Subroutine format_files: called CC=6 SCAR=19.9
85 2 my $status = 0;
86 2 for my $file (@$files) {
87 2 my $fh;
88 2 ▶ unless (open $fh, "<", $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 0 2 2
89 0 say "Cannot read $file: $!";
90 0 $status = 1;
91 0 next;
92 }
93 2 my $source = do { local $/; <$fh> };
94 2 my $mode = (stat $fh)[2] & 07777;
95 2 my $tidied = tidy_source($source);
96 2 ▶ unless (defined $tidied) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tidied) 0 2 2
97 0 say "perltidy failed: $file";
98 0 $status = 1;
99 0 next;
100 }
101 2 ▶ next if $tidied eq $source;
Branch: 1 branch
BranchTrueFalseTotal
if $tidied eq $source 1 1 2
102 1 ▶ $status = 1 unless write_atomic($file, $tidied, $mode);
Branch: 1 branch
BranchTrueFalseTotal
unless write_atomic($file, $tidied, $mode) 0 1 1
103 }
104 $status
105 2 }
106  
107 12 ▶sub main () {
Subroutine main: called CC=14 SCAR=34.9
108 12 ▶ my $mode = shift @ARGV // "";
Condition50%1/2
shift @ARGV // ""
l!l
120
MC/DC0%0/1
shift @ARGV // ""
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
Aresult
11
00
109 12 ▶ $mode =~ /\A(?:tidy|critic|list|format)\z/
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /\A(?:tidy|critic|list|format)\z/u or die "usage: perl-hook tidy|critic|list|format file ...\n" 12 0 12
110 or die "usage: perl-hook tidy|critic|list|format file ...\n";
111  
112 12 my @perl = grep is_perl($_), @ARGV;
113 12 ▶ exit $Unreadable unless @perl;
Branch: 1 branch
BranchTrueFalseTotal
unless @perl 0 12 12
114  
115 12 ▶ if ($mode eq "list") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "list") 3 9 12
116 3 say for @perl;
117 3 exit $Unreadable;
118 }
119 9 ▶ if ($mode eq "critic") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "critic") 0 9 9
120 0 my @cmd = (
121 "perl", "-Ilib", "-S", "perlcritic",
122 "--profile", ".perlcriticrc", @perl,
123 );
124 0 ▶ unless ($Unreadable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Unreadable) 0 0 0
125 0 ▶ exec @cmd or die "exec perlcritic failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
exec @cmd or die "exec perlcritic failed: $!\n" 0 0 0
126 }
127 0 my $rc = system @cmd;
128 0 ▶ die "run perlcritic failed: $!\n" if $rc == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == -1 0 0 0
129 0 ▶ my $status = ($rc >> 8) || 1;
Condition0%0/2
$rc >> 8 || 1
l!l
00
MC/DC0%0/1
$rc >> 8 || 1
$rc >> 8
Truth table0%0/2
A
A= $rc >> 8
Aresult
11
00
130 0 exit $status;
131 }
132 9 ▶ if ($mode eq "format") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "format") 2 7 9
133 2 ▶ my $status = format_files(\@perl) || $Unreadable;
Condition66%2/3
format_files(\@perl) || $Unreadable
l!l&&r!l&&!r
011
MC/DC50%1/2
format_files(\@perl) || $Unreadable
format_files(\@perl) $Unreadable
Truth table66%2/3
A || B
A= format_files(\@perl)B= $Unreadable
ABresult
1X1
011
000
134 2 exit $status;
135 }
136 7 ▶ my $status = check_tidy(\@perl) || $Unreadable;
Condition66%2/3
check_tidy(\@perl) || $Unreadable
l!l&&r!l&&!r
502
MC/DC50%1/2
check_tidy(\@perl) || $Unreadable
check_tidy(\@perl) $Unreadable
Truth table66%2/3
A || B
A= check_tidy(\@perl)B= $Unreadable
ABresult
1X1
011
000
137 7 exit $status;
138 }
139  
140 12 main();
141  
142 __END__
« utils/commit-msg-hook ↑ Summary lib/Perl/Critic/PJCJ/Fixer.pm »