bin/eshu

filter: Statementstmt 66.8% 103 / 154 Branchbran 41.9% 47 / 112 Conditioncond 36.3% 16 / 44 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 77.7% 7 / 9 Podpod n/a totaltotal 52.7% 180 / 341 CC 69 SCAR 63.6
CC
69
Cov
54%
process_dir
1260.0
print_diff
20.1
process_file
11.3
CRAP 580.5
↑ Summary blib/arch/Eshu/Install/Files.pm »
1 #!/usr/bin/perl
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Getopt::Long qw(:config no_ignore_case bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Eshu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 my $fix = 0;
8 10 my $diff = 0;
9 10 my $check = 0;
10 10 my $lang;
11 10 my $tabs = 0;
12 10 my $spaces;
13 10 my $indent_pp = 0;
14 10 my $no_indent_pp = 0;
15 10 my $range;
16 my @exclude;
17 10 my @include;
18 10 my $verbose = 0;
19  
20 10 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("fix", \$fix, "diff", \$diff, "check", \$check, "lang=s", \$lang, "tabs", \$tabs, "spaces=i", \$spaces, "indent-pp", \$indent_pp, "no-indent-pp", \$no_indent_pp, "range=s", \$range, "exclude=s", \@exclude, "include=s", \@include, "verbose|v", \$verbose) or usage() 10 0 10
21 'fix' => \$fix,
22 'diff' => \$diff,
23 'check' => \$check,
24 'lang=s' => \$lang,
25 'tabs' => \$tabs,
26 'spaces=i' => \$spaces,
27 'indent-pp' => \$indent_pp,
28 'no-indent-pp' => \$no_indent_pp,
29 'range=s' => \$range,
30 'exclude=s' => \@exclude,
31 'include=s' => \@include,
32 'verbose|v' => \$verbose,
33 ) or usage();
34  
35 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
36 0 print STDERR <<'USAGE';
37 Usage: eshu [OPTIONS] [FILE|DIR ...]
38  
39 Options:
40 --fix Edit file(s) in-place
41 --diff Show unified diff of changes (no write)
42 --check Exit 1 if file(s) would change (for CI)
43 --lang <LANG> Override language detection
44 --tabs Use tabs for indentation (default)
45 --spaces <N> Use N spaces per indent level
46 --indent-pp Indent C preprocessor directives
47 --no-indent-pp Leave preprocessor at column 0 (default)
48 --range START,END Only reindent lines START through END (1-based)
49 --exclude <PATTERN> Skip files matching regex pattern (repeatable)
50 --include <PATTERN> Only process files matching regex pattern (repeatable)
51 --verbose, -v Print each file processed
52  
53 If no FILE is given, reads from stdin and writes to stdout.
54 If a directory is given, processes all recognised files recursively.
55 USAGE
56 0 exit 2;
57 }
58  
59 10 my %opts;
60 10 ▶if (defined $spaces) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $spaces) { } 1 9 10
elsif ($tabs) { } 1 8 9
61 1 $opts{indent_char} = ' ';
62 1 $opts{indent_width} = $spaces;
63 } elsif ($tabs) {
64 1 $opts{indent_char} = "\t";
65 1 $opts{indent_width} = 1;
66 }
67 10 ▶$opts{indent_pp} = 1 if $indent_pp;
Branch: 1 branch
BranchTrueFalseTotal
if $indent_pp 0 10 10
68 10 ▶$opts{indent_pp} = 0 if $no_indent_pp;
Branch: 1 branch
BranchTrueFalseTotal
if $no_indent_pp 0 10 10
69 10 ▶if (defined $range) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range) 0 10 10
70 0 ▶ if ($range =~ /^(\d+),(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range =~ /^(\d+),(\d+)$/) { } 0 0 0
71 0 $opts{range_start} = $1;
72 0 $opts{range_end} = $2;
73 } else {
74 0 die "eshu: --range must be START,END (e.g. --range 10,25)\n";
75 }
76 }
77  
78 10 my $exit_code = 0;
79  
80 10 ▶if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 7 3 10
81 7 for my $arg (@ARGV) {
82 7 ▶ if (-d $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $arg) { } 0 7 7
83 0 process_dir($arg);
84 } else {
85 7 process_file($arg);
86 }
87 }
88 } else {
89 3 process_stdin();
90 }
91  
92 10 exit $exit_code;
93  
94 0 ▶sub process_dir {
Subroutine process_dir: not called CC=35 SCAR=71.4
95 0 my ($dir) = @_;
96  
97 0 my %dir_opts;
98 0 ▶ $dir_opts{lang} = $lang if defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang 0 0 0
99 0 ▶ $dir_opts{fix} = 1 if $fix;
Branch: 1 branch
BranchTrueFalseTotal
if $fix 0 0 0
100 0 ▶ $dir_opts{diff} = 1 if $diff;
Branch: 1 branch
BranchTrueFalseTotal
if $diff 0 0 0
101 0 ▶ if (defined $spaces) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $spaces) { } 0 0 0
elsif ($tabs) { } 0 0 0
102 0 $dir_opts{indent_char} = ' ';
103 0 $dir_opts{indent_width} = $spaces;
104 } elsif ($tabs) {
105 0 $dir_opts{indent_char} = "\t";
106 0 $dir_opts{indent_width} = 1;
107 }
108 0 ▶ $dir_opts{indent_pp} = 1 if $indent_pp;
Branch: 1 branch
BranchTrueFalseTotal
if $indent_pp 0 0 0
109 0 ▶ $dir_opts{indent_pp} = 0 if $no_indent_pp;
Branch: 1 branch
BranchTrueFalseTotal
if $no_indent_pp 0 0 0
110 0 ▶ if (@exclude) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exclude) 0 0 0
111 0 $dir_opts{exclude} = [ map { qr/$_/ } @exclude ];
112 }
113 0 ▶ if (@include) {
Branch: 1 branch
BranchTrueFalseTotal
if (@include) 0 0 0
114 0 $dir_opts{include} = [ map { qr/$_/ } @include ];
115 }
116  
117 0 my $report = Eshu->indent_dir($dir, %dir_opts);
118  
119 0 ▶ if ($check) {
Branch: 2 branches
BranchTrueFalseTotal
if ($check) { } 0 0 0
elsif ($diff) { } 0 0 0
120 0 for my $c (@{$report->{changes}}) {
121 0 ▶ if ($c->{status} eq 'needs_fixing') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"status"} eq "needs_fixing") 0 0 0
122 0 print STDERR "$c->{file}\n";
123 0 $exit_code = 1;
124 }
125 }
126 } elsif ($diff) {
127 0 for my $c (@{$report->{changes}}) {
128 0 ▶ if ($c->{diff}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"diff"}) 0 0 0
129 0 print $c->{diff};
130 }
131 }
132 }
133  
134 0 ▶ if ($verbose || (!$check && !$diff && !$fix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose or not $check || $diff || $fix) 0 0 0
Condition0%0/9
$verbose or not $check || $diff || $fix
l!l&&r!l&&!r
000
$check || $diff || $fix
l!l&&r!l&&!r
000
$check or $diff
l!l&&r!l&&!r
000
MC/DC0%0/4
$verbose or not $check || $diff || $fix
$verbose $check $diff $fix
Truth table0%0/5
A or not(B or C || D)
A= $verboseB= $checkC= $diffD= $fix
ABCDresult
1XXX1
00001
01XX0
001X0
00010
135 0 for my $c (@{$report->{changes}}) {
136 0 ▶ my $lang_str = $c->{lang} ? "[$c->{lang}]" : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->{'lang'} ? : 0 0 0
137 0 ▶ if ($c->{status} eq 'skipped') {
Branch: 5 branches
BranchTrueFalseTotal
if ($c->{'status'} eq 'skipped') { } 0 0 0
elsif ($c->{'status'} eq 'error') { } 0 0 0
elsif ($c->{'status'} eq 'changed') { } 0 0 0
elsif ($c->{'status'} eq 'needs_fixing') { } 0 0 0
elsif ($c->{'status'} eq 'unchanged') { } 0 0 0
138 0 ▶ printf "%s: skipped (%s)\n", $c->{file}, $c->{reason} || '?'
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
Condition0%0/2
$c->{'reason'} || '?'
l!l
00
MC/DC0%0/1
$c->{'reason'} || '?'
$c->{'reason'}
Truth table0%0/2
A
A= $c->{'reason'}
Aresult
11
00
139 if $verbose;
140 } elsif ($c->{status} eq 'error') {
141 0 ▶ printf "%s: error (%s)\n", $c->{file}, $c->{error} || '?';
Condition0%0/2
$c->{'error'} || '?'
l!l
00
MC/DC0%0/1
$c->{'error'} || '?'
$c->{'error'}
Truth table0%0/2
A
A= $c->{'error'}
Aresult
11
00
142 } elsif ($c->{status} eq 'changed') {
143 0 printf "%s: %s fixed\n", $c->{file}, $lang_str;
144 } elsif ($c->{status} eq 'needs_fixing') {
145 0 printf "%s: %s needs fixing\n", $c->{file}, $lang_str;
146 } elsif ($c->{status} eq 'unchanged') {
147 0 ▶ printf "%s: %s ok\n", $c->{file}, $lang_str if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
148 }
149 }
150 printf "---\n%d files checked, %d changed, %d skipped, %d errors\n",
151 $report->{files_checked}, $report->{files_changed},
152 $report->{files_skipped}, $report->{files_errored}
153 0 ▶ if $verbose || (!$check && !$diff && !$fix);
Branch: 1 branch
BranchTrueFalseTotal
if $verbose or not $check || $diff || $fix 0 0 0
Condition0%0/9
$verbose or not $check || $diff || $fix
l!l&&r!l&&!r
000
$check || $diff || $fix
l!l&&r!l&&!r
000
$check or $diff
l!l&&r!l&&!r
000
MC/DC0%0/4
$verbose or not $check || $diff || $fix
$verbose $check $diff $fix
Truth table0%0/5
A or not(B or C || D)
A= $verboseB= $checkC= $diffD= $fix
ABCDresult
1XXX1
00001
01XX0
001X0
00010
154 }
155 }
156  
157 7 ▶sub process_file {
Subroutine process_file: called CC=11 SCAR=24.2
158 7 my ($file) = @_;
159  
160 7 ▶ open my $fh, '<', $file or die "eshu: cannot open '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "eshu: cannot open '${file}': $!\n" 7 0 7
161 7 my $src = do { local $/; <$fh> };
162 7 close $fh;
163  
164 7 ▶ my $detected = $lang || Eshu->detect_lang($file) || 'c';
Condition40%2/5
$lang || 'Eshu'->detect_lang($file) || 'c'
l!l
70
$lang or "Eshu"->detect_lang($file)
l!l&&r!l&&!r
070
MC/DC0%0/2
$lang || 'Eshu'->detect_lang($file) || 'c'
$lang "Eshu"->detect_lang($file)
Truth table33%1/3
A or B
A= $langB= "Eshu"->detect_lang($file)
ABresult
1X1
011
000
165 7 my $result = Eshu->indent_string($src, lang => $detected, %opts);
166  
167 7 ▶ if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 3 4 7
168 3 ▶ if ($result ne $src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne $src) 2 1 3
169 2 print STDERR "$file\n";
170 2 $exit_code = 1;
171 }
172 3 return;
173 }
174  
175 4 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) 2 2 4
176 2 ▶ if ($result ne $src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne $src) 1 1 2
177 1 print_diff($file, $src, $result);
178 }
179 2 return;
180 }
181  
182 2 ▶ if ($fix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix) 1 1 2
183 1 ▶ if ($result ne $src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne $src) 1 0 1
184 1 ▶ open my $out, '>', $file or die "eshu: cannot write '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $file or die "eshu: cannot write '${file}': $!\n" 1 0 1
185 1 print $out $result;
186 1 close $out;
187 }
188 1 return;
189 }
190  
191 # Default: write to stdout
192 1 print $result;
193 }
194  
195 3 ▶sub process_stdin {
Subroutine process_stdin: called CC=6 SCAR=23.5
196 3 ▶ my $detected = $lang || 'c';
Condition50%1/2
$lang || 'c'
l!l
30
MC/DC0%0/1
$lang || 'c'
$lang
Truth table50%1/2
A
A= $lang
Aresult
11
00
197 3 my $src = do { local $/; <STDIN> };
198 3 my $result = Eshu->indent_string($src, lang => $detected, %opts);
199  
200 3 ▶ if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 0 3 3
201 0 ▶ $exit_code = 1 if $result ne $src;
Branch: 1 branch
BranchTrueFalseTotal
if $result ne $src 0 0 0
202 0 return;
203 }
204  
205 3 ▶ if ($diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff) 0 3 3
206 0 ▶ print_diff('stdin', $src, $result) if $result ne $src;
Branch: 1 branch
BranchTrueFalseTotal
if $result ne $src 0 0 0
207 0 return;
208 }
209  
210 3 print $result;
211 }
212  
213 1 ▶sub print_diff {
Subroutine print_diff: called CC=20 SCAR=30.0
214 1 my ($label, $old, $new) = @_;
215 1 my @old_lines = split /^/m, $old;
216 1 my @new_lines = split /^/m, $new;
217  
218 1 print "--- a/$label\n";
219 1 print "+++ b/$label\n";
220  
221 # Simple line-by-line diff
222 1 ▶ my $max = @old_lines > @new_lines ? scalar @old_lines : scalar @new_lines;
Branch: 1 branch
BranchTrueFalseTotal
@old_lines > @new_lines ? : 0 1 1
223 1 my $hunk_start = -1;
224 1 my @hunk_old;
225 my @hunk_new;
226  
227 1 ▶ for (my $i = 0; $i <= $max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 4 1 5
228 4 ▶ my $ol = $i < @old_lines ? $old_lines[$i] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i < @old_lines ? : 3 1 4
229 4 ▶ my $nl = $i < @new_lines ? $new_lines[$i] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i < @new_lines ? : 3 1 4
230 4 ▶ my $same = defined $ol && defined $nl && $ol eq $nl;
Condition83%5/6
defined $ol && defined $nl && $ol eq $nl
!ll&&!rl&&r
112
defined $ol and defined $nl
!ll&&!rl&&r
103
MC/DC66%2/3
defined $ol && defined $nl && $ol eq $nl
defined $ol defined $nl $ol eq $nl
Truth table75%3/4
A and B && C
A= defined $olB= defined $nlC= $ol eq $nl
ABCresult
0XX0
10X0
1100
1111
231  
232 4 ▶ if (!$same && $hunk_start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $same and $hunk_start < 0) 2 2 4
Condition66%2/3
not $same and $hunk_start < 0
!ll&&!rl&&r
202
MC/DC50%1/2
not $same and $hunk_start < 0
not $same $hunk_start < 0
Truth table66%2/3
not(A) and B
A= not $sameB= $hunk_start < 0
ABresult
0X0
100
111
233 2 $hunk_start = $i;
234 2 @hunk_old = ();
235 2 @hunk_new = ();
236 }
237  
238 4 ▶ if ($hunk_start >= 0 && !$same) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hunk_start >= 0 and not $same) 2 2 4
Condition100%3/3
$hunk_start >= 0 and not $same
!ll&&!rl&&r
112
MC/DC100%2/2
$hunk_start >= 0 and not $same
$hunk_start >= 0 not $same
Truth table100%3/3
A and not(B)
A= $hunk_start >= 0B= not $same
ABresult
0X0
100
111
239 2 ▶ push @hunk_old, $ol if defined $ol;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ol 1 1 2
240 2 ▶ push @hunk_new, $nl if defined $nl;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nl 1 1 2
241 }
242  
243 4 ▶ if ($same || $i == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same or $i == $max) 3 1 4
Condition100%3/3
$same or $i == $max
l!l&&r!l&&!r
211
MC/DC100%2/2
$same or $i == $max
$same $i == $max
Truth table100%3/3
A or B
A= $sameB= $i == $max
ABresult
1X1
011
000
244 3 ▶ if ($hunk_start >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hunk_start >= 0) 2 1 3
245 2 my $old_count = scalar @hunk_old;
246 2 my $new_count = scalar @hunk_new;
247 2 printf "@@ -%d,%d +%d,%d @@\n",
248 $hunk_start + 1, $old_count,
249 $hunk_start + 1, $new_count;
250 2 for my $l (@hunk_old) {
251 1 ▶ $l = "$l\n" unless $l =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $l =~ /\n$/ 0 1 1
252 1 print "-$l";
253 }
254 2 for my $l (@hunk_new) {
255 1 ▶ $l = "$l\n" unless $l =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $l =~ /\n$/ 0 1 1
256 1 print "+$l";
257 }
258 2 $hunk_start = -1;
259 }
260 }
261 }
262 }
↑ Summary blib/arch/Eshu/Install/Files.pm »