blib/lib/ShipIt/Step/ChangeAllVersions.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 79.1% 19 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 20.0% 1 / 5 totaltotal 88.6% 117 / 132 CC 16 SCAR 18.1
CC
16
Cov
92%
run
11.3
__ANON__
6.0
collect_files
1.0
CRAP 6.1
↑ Summary
1 package ShipIt::Step::ChangeAllVersions;
2  
3 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '1.002';
8  
9 3 ▶use parent qw(ShipIt::Step);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Fatal qw(open close rename opendir closedir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::stat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 my $looks_like_perl = qr/\. p(?: [ml]c? | od | erl ) \z/xms;
15  
16 3 ▶sub collect_files {
Subroutine collect_files: called CC=1 SCAR=0.0
Pod undocumented
17 3 my @files;
18  
19 # from subdir
20 14 ▶ find sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
21 14 ▶ return if not ( -f $_ && not -l $_ );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ and not -l $_ 7 7 14
Condition66%2/3
-f $_ and not -l $_
!ll&&!rl&&r
707
MC/DC50%1/2
-f $_ and not -l $_
-f $_ not -l $_
Truth table66%2/3
A and not(B)
A= -f $_B= not -l $_
ABresult
0X0
100
111
22  
23 7 ▶ if($_ =~ $looks_like_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$looks_like_perl/) { } 3 4 7
24 3 push @files, $File::Find::name;
25 }
26 else {
27 4 open my $in, '<', $_;
28 4 read $in, my($buff), 2; # I don't care whether it fail or not
29  
30 4 ▶ if($buff && $buff eq '#!'){
Branch: 1 branch
BranchTrueFalseTotal
if ($buff and $buff eq "#!") 2 2 4
Condition66%2/3
$buff and $buff eq "#!"
!ll&&!rl&&r
202
MC/DC50%1/2
$buff and $buff eq "#!"
$buff $buff eq "#!"
Truth table66%2/3
A and B
A= $buffB= $buff eq "#!"
ABresult
0X0
100
111
31 2 push @files, $File::Find::name;
32 }
33 4 close $in;
34 }
35 3 }, grep{ -d } qw(lib script bin);
36  
37 # from toplevel
38 3 opendir my $dirh, '.';
39 3 push @files, grep { ! -l $_ } grep{ $_ =~ $looks_like_perl } readdir($dirh);
40 3 closedir $dirh;
41  
42 3 return @files;
43 }
44  
45 2 ▶sub run {
Subroutine run: called CC=11 SCAR=24.2
Pod documented
46 2 my ($self, $state) = @_;
47  
48 2 my $dry_run = $state->dry_run;
49  
50 2 ▶ if($dry_run){
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) 0 2 2
51 0 $self->log("*** DRY RUN, not actually updating versions.");
52 }
53  
54 2 my $current_version = quotemeta $state->pt->{version};
55 2 my $new_version = $state->version;
56  
57 2 foreach my $module ($self->collect_files) {
58 8 open my $in, '<', $module;
59 8 binmode $in;
60  
61 8 my $out;
62 8 ▶ if(!$dry_run){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 8 0 8
63 8 open $out, '>', "$module.tmp";
64 8 binmode $out;
65 }
66  
67 8 my $need_replace = 0;
68  
69 8 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 90 8 98
70  
71 # update $VERSION variable
72 90 ▶ if (s/(\$VERSION .+) \b $current_version \b/$1$new_version/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/(\$VERSION .+) \b $current_version \b/$1$new_version/msx) 6 84 90
73 6 $self->{changed_version_variable}{$module}++;
74 6 $self->log("Update \$VERSION in $module.");
75 6 $need_replace++;
76 }
77  
78 # update the VERSION section which says,
79 # "This is Foo version $ver.",
80 # or "This document descrives Foo version $ver."
81 90 ▶ if (/\A =head1 \s+ VERSION\b/xms ... /\A =\w+/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A =head1 \s+ VERSION\b/msx ... /\A =\w+/msx) 30 60 90
82 30 ▶ if (s/(version \b .+) $current_version/$1$new_version/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/(version \b .+) $current_version/$1$new_version/msx) 6 24 30
83 6 $self->{changed_version_section}{$module}++;
84 6 $self->log("Update the VERSION section in $module.");
85 6 $need_replace++;
86 }
87 }
88  
89 90 ▶ print $out $_ if defined $out;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out 90 0 90
90 }
91  
92 8 close $in;
93  
94 8 ▶ next if $dry_run;
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 8 8
95  
96 8 close $out;
97  
98 8 ▶ if($need_replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_replace) { } 8 0 8
99 8 my $mode = File::stat::stat($module)->mode;
100 8 rename $module => "$module~";
101 8 rename "$module.tmp" => $module;
102 8 chmod $mode, $module;
103  
104 8 unlink "$module~";
105 }
106 else{
107 0 unlink "$module.tmp";
108 }
109 }
110  
111 2 return 1;
112 }
113  
114 6 ▶sub changed_version_variable { # for testing
Subroutine changed_version_variable: called CC=1 SCAR=0.0
Pod undocumented
115 6 return $_[0]->{changed_version_variable};
116 }
117  
118 6 ▶sub changed_version_section { # for testing
Subroutine changed_version_section: called CC=1 SCAR=0.0
Pod undocumented
119 6 return $_[0]->{changed_version_section};
120 }
121  
122 12 ▶sub log {
Subroutine log: called CC=1 SCAR=0.0
Pod undocumented
123 12 my $self = shift;
124 12 print @_, "\n";
125 }
126  
127 3 1;
128 __END__
↑ Summary