blib/lib/App/DiffTarballs.pm

filter: Statementstmt 41.1% 28 / 68 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 33.0% 36 / 109 CC 15 SCAR 47.3
CC
15
Cov
29%
diff_tarballs
185.5
__ANON__
6.0
BEGIN
1.0
CRAP 113.8
↑ Summary
1 package App::DiffTarballs;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2021-05-10'; # DATE
5 1 our $DIST = 'App-DiffTarballs'; # DIST
6 1 our $VERSION = '0.005'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::System::Options qw(system);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our %SPEC;
17  
18 my %xcompletion_tarball = (
19 'x.completion' => [filename => {
20 0 ▶ filter => sub {-f $_[0] && $_[0] =~ /\.tar\.?/},
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
-f $_[0] and $_[0] =~ /\.tar\.?/ 0 0 0
21 1 }],
22 );
23  
24 $SPEC{diff_tarballs} = {
25 1 v => 1.1,
26 summary => 'Diff contents of two tarballs',
27 description => <<'_',
28  
29 This utility extracts the two tarballs to temporary directories and then
30 performs `diff -ruN` against the two. It deletes the temporary directories
31 afterwards.
32  
33 _
34 args => {
35 tarball1 => {
36 schema => 'filename*',
37 %xcompletion_tarball,
38 req => 1,
39 pos => 0,
40 },
41 tarball2 => {
42 schema => 'filename*',
43 %xcompletion_tarball,
44 req => 1,
45 pos => 1,
46 },
47 },
48 deps => {
49 all => [
50 {prog => 'tar'},
51 {prog => 'diff'},
52 ],
53 },
54 examples => [
55 {
56 argv => [qw/My-Dist-1.001.tar.gz My-Dist-1.002.tar.bz2/],
57 summary => 'Show diff between two Perl releases',
58 test => 0,
59 'x.doc.show_result' => 0,
60 },
61 ],
62 };
63 0 ▶sub diff_tarballs {
Subroutine diff_tarballs: not called CC=14 SCAR=52.2
Pod documented
64 0 require Cwd;
65 0 require File::Temp;
66 0 require ShellQuote::Any::Tiny;
67  
68 0 my %args = @_;
69  
70 return [404, "No such file or directory: $args{tarball1}"]
71 0 ▶ unless -f $args{tarball1};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $args{"tarball1"} 0 0 0
72 return [404, "No such file or directory: $args{tarball2}"]
73 0 ▶ unless -f $args{tarball2};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $args{"tarball2"} 0 0 0
74  
75 0 my $abs_tarball1 = Cwd::abs_path($args{tarball1});
76 0 my $abs_tarball2 = Cwd::abs_path($args{tarball2});
77  
78 return [404, "No such file or directory: $args{tarball1}"]
79 0 ▶ unless -f $args{tarball1};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $args{"tarball1"} 0 0 0
80 return [404, "No such file or directory: $args{tarball2}"]
81 0 ▶ unless -f $args{tarball2};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $args{"tarball2"} 0 0 0
82  
83 0 my $dir1 = File::Temp::tempdir(CLEANUP => 1);
84 0 my $dir2 = File::Temp::tempdir(CLEANUP => 1);
85  
86 0 $CWD = $dir1;
87 0 system({log=>1, die=>1}, "tar", "xf", $abs_tarball1);
88 0 system({log=>1, die=>1}, "tar", "xf", $abs_tarball2);
89 0 ▶ return [304, "$args{tarball1} and $args{tarball2} are the same file"]
Branch: 1 branch
BranchTrueFalseTotal
if $abs_tarball1 eq $abs_tarball2 0 0 0
90 if $abs_tarball1 eq $abs_tarball2;
91  
92 0 my $cleanup = !$ENV{DEBUG};
93  
94 0 $dir1 = File::Temp::tempdir(CLEANUP => $cleanup);
95 0 $dir2 = File::Temp::tempdir(CLEANUP => $cleanup);
96  
97 0 $CWD = $dir1;
98 0 system({log=>1, die=>1}, "tar", "xf", $abs_tarball1);
99 0 ▶ my @glob1 = glob("*");
Subroutine BEGIN: called CC=1 SCAR=0.2
100 0 ▶ unless (@glob1 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@glob1 == 1) 0 0 0
101 0 return [412, "$args{tarball1} did not extract to ".
102 "a single file/directory"];
103 }
104  
105 0 $CWD = $dir2;
106 0 system({log=>1, die=>1}, "tar", "xf", $abs_tarball2);
107 0 my @glob2 = glob("*");
108 0 ▶ unless (@glob2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@glob2 == 1) 0 0 0
109 0 return [412, "$args{tarball2} did not extract to ".
110 "a single file/directory"];
111 }
112  
113 0 my $name1 = $glob1[0];
114 0 my $name2 = $glob2[0];
115 0 ▶ $name1 .= ".0" if $name1 eq $name2;
Branch: 1 branch
BranchTrueFalseTotal
if $name1 eq $name2 0 0 0
116  
117 0 rename "$dir1/$glob1[0]", "$dir2/$name1";
118  
119 0 ▶ my $diff_cmd = $ENV{DIFF} // do {
Condition0%0/3
$ENV{"DIFF"} // do { 'diff -ruN' . (exists $ENV{"NO_COLOR"} ? " --color=never" : (defined $ENV{"COLOR"} ? $ENV{"COLOR"} ? " --color=always" : " --color=never" : "")) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"DIFF"} // do { 'diff -ruN' . (exists $ENV{"NO_COLOR"} ? " --color=never" : (defined $ENV{"COLOR"} ? $ENV{"COLOR"} ? " --color=always" : " --color=never" : "")) }
$ENV{"DIFF"} do { 'diff -ruN' . (exists $ENV{"NO_COLOR"} ? " --color=never" : (defined $ENV{"COLOR"} ? $ENV{"COLOR"} ? " --color=always" : " --color=never" : "")) }
Truth table0%0/3
A // B
A= $ENV{"DIFF"}B= do { 'diff -ruN' . (exists $ENV{"NO_COLOR"} ? " --color=never" : (defined $ENV{"COLOR"} ? $ENV{"COLOR"} ? " --color=always" : " --color=never" : "")) }
ABresult
1X1
011
000
120 "diff -ruN" .
121 (exists $ENV{NO_COLOR} ? " --color=never" :
122 0 ▶ defined $ENV{COLOR} ? ($ENV{COLOR} ? " --color=always" : " --color=never") :
Branch: 3 branches
BranchTrueFalseTotal
exists $ENV{'NO_COLOR'} ? : 0 0 0
defined $ENV{'COLOR'} ? : 0 0 0
$ENV{'COLOR'} ? : 0 0 0
123 "");
124 };
125 0 system({log=>1, shell=>1}, join(
126 " ",
127 $diff_cmd,
128 ShellQuote::Any::Tiny::shell_quote($name1),
129 ShellQuote::Any::Tiny::shell_quote($name2),
130 ));
131  
132 0 ▶ unless ($cleanup) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cleanup) 0 0 0
133 0 log_info("Not cleaning up temporary directory %s", $dir2);
134 }
135  
136 0 [200];
137 }
138  
139 1 1;
140 # ABSTRACT: Diff contents of two tarballs
141  
142 __END__
↑ Summary