| 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
| |||||||||||||||||||||||||||||
| 16 | 7 | my $fh; | |||||||||||||||||||||||||||
| 17 | 7 | ▶ | unless (open $fh, "<", $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $error or $error
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 1 | say "perltidy failed: $file"; | |||||||||||||||||||||||||||
| 56 | 1 | $status = 1; | |||||||||||||||||||||||||||
| 57 | 1 | next; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 6 | ▶ | next if $tidied eq $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$ok &&= close $out
$ok
close $out
Truth table33%1/3
A &&= B
A= $okB= close $out
| |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | $ok &&= chmod $mode, $temp; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= chmod($mode, $temp)
MC/DC0%0/2
$ok &&= chmod($mode, $temp)
$ok
chmod($mode, $temp)
Truth table33%1/3
A &&= B
A= $okB= chmod($mode, $temp)
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | $ok &&= rename $temp, $file; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= rename($temp, $file)
MC/DC0%0/2
$ok &&= rename($temp, $file)
$ok
rename($temp, $file)
Truth table33%1/3
A &&= B
A= $okB= rename($temp, $file)
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | return 1 if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 97 | 0 | say "perltidy failed: $file"; | |||||||||||||||||||||||||||
| 98 | 0 | $status = 1; | |||||||||||||||||||||||||||
| 99 | 0 | next; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 2 | ▶ | next if $tidied eq $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | ▶ | $status = 1 unless write_atomic($file, $tidied, $mode); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
shift @ARGV // ""
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
| |||||||||||||||||||||||||||||
| 109 | 12 | ▶ | $mode =~ /\A(?:tidy|critic|list|format)\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | ▶ | if ($mode eq "list") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | say for @perl; | |||||||||||||||||||||||||||
| 117 | 3 | exit $Unreadable; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 9 | ▶ | if ($mode eq "critic") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | my @cmd = ( | |||||||||||||||||||||||||||
| 121 | "perl", "-Ilib", "-S", "perlcritic", | ||||||||||||||||||||||||||||
| 122 | "--profile", ".perlcriticrc", @perl, | ||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | unless ($Unreadable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | exec @cmd or die "exec perlcritic failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | my $rc = system @cmd; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | die "run perlcritic failed: $!\n" if $rc == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $status = ($rc >> 8) || 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$rc >> 8 || 1
MC/DC0%0/1
$rc >> 8 || 1
$rc >> 8
Truth table0%0/2
A
A= $rc >> 8
| |||||||||||||||||||||||||||||
| 130 | 0 | exit $status; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 9 | ▶ | if ($mode eq "format") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | my $status = format_files(\@perl) || $Unreadable; | ||||||||||||||||||||||||||
|
Condition66%2/3
format_files(\@perl) || $Unreadable
MC/DC50%1/2
format_files(\@perl) || $Unreadable
format_files(\@perl)
$Unreadable
Truth table66%2/3
A || B
A= format_files(\@perl)B= $Unreadable
| |||||||||||||||||||||||||||||
| 134 | 2 | exit $status; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 7 | ▶ | my $status = check_tidy(\@perl) || $Unreadable; | ||||||||||||||||||||||||||
|
Condition66%2/3
check_tidy(\@perl) || $Unreadable
MC/DC50%1/2
check_tidy(\@perl) || $Unreadable
check_tidy(\@perl)
$Unreadable
Truth table66%2/3
A || B
A= check_tidy(\@perl)B= $Unreadable
| |||||||||||||||||||||||||||||
| 137 | 7 | exit $status; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 12 | main(); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | __END__ | ||||||||||||||||||||||||||||