Test/File/Codeowners.pm

filter: Statementstmt 32.5% 27 / 83 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 8 / 12 Podpod 100% 2 / 2 totaltotal 27.4% 37 / 135 SCAR 47.4
CC
15
Cov
24%
__ANON__
90.0
codeowners_syntax_ok
20.0
codeowners_git_files_ok
20.0
CRAP 114.2
« File/Codeowners.pm ↑ Summary File/Codeowners/Util.pm »
1 package Test::File::Codeowners;
2 # ABSTRACT: Write tests for CODEOWNERS files
3  
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Codeowners::Util qw(find_codeowners_in_directory find_nearest_codeowners git_ls_files git_toplevel);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Codeowners;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.53'; # VERSION
15  
16 1 my $Test = Test::Builder->new;
17  
18 sub import {
19 0 ▶ my $self = shift;
Subroutine import: not called CC=1 SCAR=3.3
20 0 my $caller = caller;
21 1 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 *{$caller.'::codeowners_syntax_ok'} = \&codeowners_syntax_ok;
23 0 *{$caller.'::codeowners_git_files_ok'} = \&codeowners_git_files_ok;
24  
25 0 $Test->exported_to($caller);
26 0 $Test->plan(@_);
27 }
28  
29  
30 sub codeowners_syntax_ok {
31 0 ▶ my $filepath = shift || find_nearest_codeowners($Bin);
Subroutine codeowners_syntax_ok: not called CC=4 SCAR=30.0
Pod documented
Condition0%0/3
shift() || find_nearest_codeowners($Test::File::Codeowners::Bin)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || find_nearest_codeowners($Test::File::Codeowners::Bin)
shift() find_nearest_codeowners($Test::File::Codeowners::Bin)
Truth table0%0/3
A || B
A= shift()B= find_nearest_codeowners($Test::File::Codeowners::Bin)
ABresult
1X1
011
000
32  
33 0 ▶ if (!$filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filepath) 0 0 0
34 0 $Test->ok(0, "Check syntax: <missing>");
35 0 $Test->diag('No CODEOWNERS file could be found.');
36 0 return;
37 }
38  
39 0 eval { File::Codeowners->parse($filepath) };
40 0 my $err = $@;
41  
42 0 $Test->ok(!$err, "Check syntax: $filepath");
43 0 ▶ $Test->diag($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
44 }
45  
46  
47 sub codeowners_git_files_ok {
48 0 ▶ my $repopath = shift || $Bin;
Subroutine codeowners_git_files_ok: not called CC=4 SCAR=30.0
Pod documented
Condition0%0/3
shift() || $Test::File::Codeowners::Bin
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Test::File::Codeowners::Bin
shift() $Test::File::Codeowners::Bin
Truth table0%0/3
A || B
A= shift()B= $Test::File::Codeowners::Bin
ABresult
1X1
011
000
49  
50 0 my $git_toplevel = git_toplevel($repopath);
51 0 ▶ if (!$git_toplevel) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($git_toplevel) 0 0 0
52 0 $Test->skip('No git repo could be found.');
53 0 return;
54 }
55  
56 0 my $filepath = find_codeowners_in_directory($git_toplevel);
57 0 ▶ if (!$filepath) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filepath) 0 0 0
58 0 $Test->ok(0, "Check syntax: <missing>");
59 0 $Test->diag("No CODEOWNERS file could be found in repo $repopath.");
60 0 return;
61 }
62  
63 $Test->subtest('codeowners_git_files_ok' => sub {
64 0 ▶ local $Test::Builder::Level = $Test::Builder::Level + 3;
Subroutine __ANON__: not called CC=9 SCAR=45.0
65  
66 0 my $codeowners = eval { File::Codeowners->parse($filepath) };
67 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
68 0 $Test->plan(tests => 1);
69 0 $Test->ok(0, "Parse $filepath");
70 0 $Test->diag($err);
71 0 return;
72 }
73  
74 0 my ($proc, @files) = git_ls_files($git_toplevel);
75 0 ▶ if ($proc->wait != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->wait != 0) 0 0 0
76 0 $Test->plan(skip_all => 'git ls-files failed');
77 0 return;
78 }
79  
80 0 $Test->plan(tests => scalar @files);
81  
82 0 for my $filepath (@files) {
83 0 my $msg = encode('UTF-8', "Check file: $filepath");
84  
85 0 my $match = $codeowners->match($filepath);
86 0 my $is_unowned = $codeowners->is_unowned($filepath);
87  
88 0 ▶ if (!$match && !$is_unowned) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $match || $is_unowned) { } 0 0 0
elsif ($match and $is_unowned) { } 0 0 0
Condition0%0/3
$match || $is_unowned
l!l&&r!l&&!r
000
MC/DC0%0/2
$match || $is_unowned
$match $is_unowned
Truth table0%0/3
A || B
A= $matchB= $is_unowned
ABresult
1X1
011
000
89 0 $Test->ok(0, $msg);
90 0 $Test->diag("File is unowned\n");
91 }
92 ▶ elsif ($match && $is_unowned) {
Condition0%0/3
$match and $is_unowned
!ll&&!rl&&r
000
MC/DC0%0/2
$match and $is_unowned
$match $is_unowned
Truth table0%0/3
A and B
A= $matchB= $is_unowned
ABresult
0X0
100
111
93 0 $Test->ok(0, $msg);
94 0 $Test->diag("File is owned but listed as unowned\n");
95 }
96 else {
97 0 $Test->ok(1, $msg);
98 0 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 0 0
99 0 my $owners = encode('UTF-8', join(',', @{$match->{owners}}));
100 0 $Test->note("File is owned by $owners");
101 }
102 }
103 }
104 0 });
105 }
106  
107 1 1;
108  
109 __END__
« File/Codeowners.pm ↑ Summary File/Codeowners/Util.pm »