1 package Test
::File
::Codeowners
;
2 # ABSTRACT: Write tests for CODEOWNERS files
8 eval 'use Test::File::Codeowners';
9 plan skip_all => 'Test::File::Codeowners required for testing CODEOWNERS' if $@;
11 codeowners_syntax_ok();
16 This package has assertion subroutines for testing F<CODEOWNERS> files.
23 use App
::Codeowners
::Util
qw(find_nearest_codeowners git_ls_files git_toplevel);
24 use Encode
qw(encode);
28 our $VERSION = '9999.999'; # VERSION
30 my $Test = Test
::Builder-
>new;
35 no strict
'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
36 *{$caller.'::codeowners_syntax_ok'} = \
&codeowners_syntax_ok
;
37 *{$caller.'::codeowners_git_files_ok'} = \
&codeowners_git_files_ok
;
39 $Test->exported_to($caller);
43 =func codeowners_syntax_ok
45 codeowners_syntax_ok
(); # search up the tree for a CODEOWNERS file
46 codeowners_syntax_ok
($filepath);
48 Check the syntax of a F
<CODEOWNERS
> file
.
52 sub codeowners_syntax_ok
{
53 my $filepath = shift || find_nearest_codeowners
();
55 eval { File
::Codeowners-
>parse($filepath) };
58 $Test->ok(!$err, "Check syntax: $filepath");
59 $Test->diag($err) if $err;
62 =func codeowners_git_files_ok
64 codeowners_git_files_ok
(); # search up the tree for a CODEOWNERS file
65 codeowners_git_files_ok
($filepath);
69 sub codeowners_git_files_ok
{
70 my $filepath = shift || find_nearest_codeowners
();
72 $Test->subtest('codeowners_git_files_ok' => sub {
73 my $codeowners = eval { File
::Codeowners-
>parse($filepath) };
75 $Test->plan(tests
=> 1);
76 $Test->ok(0, "Parse $filepath");
81 my ($proc, @files) = git_ls_files
(git_toplevel
());
83 $Test->plan($proc->wait == 0 ? (tests
=> scalar @files) : (skip_all
=> 'git ls-files failed'));
85 for my $filepath (@files) {
86 my $msg = encode
('UTF-8', "Check file: $filepath");
88 my $match = $codeowners->match($filepath);
89 my $is_unowned = $codeowners->is_unowned($filepath);
91 if (!$match && !$is_unowned) {
93 $Test->diag("File is unowned\n");
95 elsif ($match && $is_unowned) {
97 $Test->diag("File is owned but listed as unowned\n");