ShipIt/Util.pm

filter: Statementstmt 46.5% 40 / 86 Branchbran 10.5% 4 / 38 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 9 / 18 Podpod 0.0% 0 / 8 totaltotal 31.9% 53 / 166 CC 26 SCAR 30.0
CC
26
Cov
33%
bool_prompt
72.0
in_dir
20.0
__ANON__
12.0
CRAP 20.0
« ShipIt/VC.pm ↑ Summary ShipIt/VC/SVN.pm »
1 package ShipIt::Util;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 require Exporter;
5 2 our @ISA = qw(Exporter);
6 2 our @EXPORT_OK = qw(slurp write_file bool_prompt edit_file $term make_var tempdir_obj
7 find_subclasses in_dir);
8 2 ▶use Term::ReadLine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $term = eval {
14 2 Term::ReadLine->new("prompt");
15 };
16  
17 7 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=7.0
Pod undocumented
18 7 my ($file) = @_;
19 7 ▶ open (my $fh, $file) or confess "Failed to open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or confess("Failed to open ${file}: $!\n") 7 0 7
20 7 return do { local $/; binmode $fh; <$fh>; }
21 }
22  
23 7 ▶sub write_file {
Subroutine write_file: called CC=4 SCAR=14.3
Pod undocumented
24 7 my ($file, $contents) = @_;
25 7 ▶ open (my $fh, ">", $file) or confess "Failed to open $file for write: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or confess("Failed to open $file for write: $!\n") 7 0 7
26 7 binmode $fh;
27 7 print $fh $contents;
28 7 ▶ close($fh) or confess "Close failed";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or confess("Close failed") 7 0 7
29 7 ▶ die "assert" unless -s $file == length($contents);
Branch: 1 branch
BranchTrueFalseTotal
unless -s $file == length $contents 0 7 7
30 7 return 1;
31 }
32  
33 0 ▶sub bool_prompt {
Subroutine bool_prompt: not called CC=8 SCAR=42.8
Pod undocumented
34 0 my ($q, $def) = @_;
35 0 ▶ $def = uc($def || "");
Condition0%0/2
$def || ''
l!l
00
MC/DC0%0/1
$def || ''
$def
Truth table0%0/2
A
A= $def
Aresult
11
00
36 0 ▶ die "bogus default" unless $def =~ /^[YN]?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $def =~ /^[YN]?$/ 0 0 0
37 0 ▶ return $def if !$term;
Branch: 1 branch
BranchTrueFalseTotal
unless $ShipIt::Util::term 0 0 0
38 0 my $opts = " [y/n]";
39 0 ▶ $opts = " [Y/n]" if $def eq "Y";
Branch: 1 branch
BranchTrueFalseTotal
if $def eq "Y" 0 0 0
40 0 ▶ $opts = " [y/N]" if $def eq "N";
Branch: 1 branch
BranchTrueFalseTotal
if $def eq "N" 0 0 0
41 0 ▶ my $to_bool = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
42 0 my $yn = shift;
43 0 ▶ return 1 if $yn =~ /^y/i;
Branch: 1 branch
BranchTrueFalseTotal
if $yn =~ /^y/i 0 0 0
44 0 ▶ return 0 if $yn =~ /^n/i;
Branch: 1 branch
BranchTrueFalseTotal
if $yn =~ /^n/i 0 0 0
45 0 return undef;
46 0 };
47 0 while (1) {
48 0 my $ans = $term->readline("$q$opts ");
49 0 ▶ my $bool = $to_bool->($ans || $def);
Condition0%0/3
$ans || $def
l!l&&r!l&&!r
000
MC/DC0%0/2
$ans || $def
$ans $def
Truth table0%0/3
A || B
A= $ansB= $def
ABresult
1X1
011
000
50 0 ▶ return $bool if defined $bool;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bool 0 0 0
51 0 warn "Please answer 'y' or 'n'\n";
52 }
53 }
54  
55 0 ▶sub edit_file {
Subroutine edit_file: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my ($file) = @_;
57 0 ▶ my $editor = $ENV{"EDITOR"} || "vi";
Condition0%0/2
$ENV{'EDITOR'} || 'vi'
l!l
00
MC/DC0%0/1
$ENV{'EDITOR'} || 'vi'
$ENV{'EDITOR'}
Truth table0%0/2
A
A= $ENV{'EDITOR'}
Aresult
11
00
58 0 system($editor, $file);
59 }
60  
61 0 ▶sub make_var {
Subroutine make_var: not called CC=2 SCAR=17.9
Pod undocumented
62 0 my $var = shift;
63 0 my $file = slurp("Makefile");
64 0 ▶ return undef unless $file =~ /^\Q$var\E\s*=\s*(.+)\s*/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^\Q$var\E\s*=\s*(.+)\s*/m 0 0 0
65 0 return $1;
66 }
67  
68 0 ▶sub find_subclasses {
Subroutine find_subclasses: not called CC=4 SCAR=23.7
Pod undocumented
69 # search for any other custom project type modules
70 0 my $class = shift;
71 0 my @classes = ();
72 0 for my $dir (@INC) {
73 0 ▶ for my $file (glob("$dir/" . join("/", split(/::/, $class)) . "/*.pm")) {
Subroutine BEGIN: called CC=1 SCAR=0.2
74 0 ▶ if($file =~ /\/(\w+)\.pm/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[/(\w+)\.pm]) 0 0 0
75 0 push(@classes, "$class" . "::" . "$1");
76 }
77 }
78 }
79 0 return @classes;
80 }
81  
82 # returns either $obj or ($obj->dir, $obj), when in list context.
83 # when $obj goes out of scope, all temp directory contents are wiped.
84 0 ▶sub tempdir_obj {
Subroutine tempdir_obj: not called CC=3 SCAR=24.8
Pod undocumented
85 0 ▶ my $dir = File::Temp::tempdir() or
Branch: 1 branch
BranchTrueFalseTotal
my $dir = File::Temp::tempdir() or die "Failed to create temp directory: $!\n" 0 0 0
86 die "Failed to create temp directory: $!\n";
87 0 my $obj = bless {
88 dir => $dir,
89 }, "ShipIt::Util::TempDir";
90 0 ▶ return wantarray ? ($dir, $obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
91 }
92  
93 # run a coderef in another directory, then return to old directory,
94 # even if $code dies.
95 0 ▶sub in_dir {
Subroutine in_dir: not called CC=4 SCAR=30.0
Pod undocumented
96 0 my ($dir, $code) = @_;
97 0 my $old_cwd = getcwd;
98 0 ▶ chdir($dir) or die "chdir to dir $dir failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "chdir to dir $dir failed: $!\n" 0 0 0
99 0 my $rv = eval { $code->(); };
100 0 my $err = $@;
101 0 ▶ chdir($old_cwd) or die "chdir back to $old_cwd failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $old_cwd or die "chdir back to $old_cwd failed: $!\n" 0 0 0
102 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
103 0 return $rv;
104 }
105  
106  
107 ############################################################################
108  
109 package ShipIt::Util::TempDir;
110 0 ▶sub directory { $_[0]{dir} };
Subroutine directory: not called CC=1 SCAR=6.9
111 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
112 0 my $self = shift;
113 0 ▶ File::Path::rmtree($self->{dir}) if $self->{dir} && -d $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dir"} and -d $self->{"dir"} 0 0 0
Condition0%0/3
$self->{"dir"} and -d $self->{"dir"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"dir"} and -d $self->{"dir"}
$self->{"dir"} -d $self->{"dir"}
Truth table0%0/3
A and B
A= $self->{"dir"}B= -d $self->{"dir"}
ABresult
0X0
100
111
114 }
115  
116  
117 2 1;
« ShipIt/VC.pm ↑ Summary ShipIt/VC/SVN.pm »