]> Dogcows Code - chaz/git-codeowners/blob - lib/Test/File/Codeowners.pm
initial commit
[chaz/git-codeowners] / lib / Test / File / Codeowners.pm
1 package Test::File::Codeowners;
2 # ABSTRACT: Write tests for CODEOWNERS files
3
4 =head1 SYNOPSIS
5
6 use Test::More;
7
8 eval 'use Test::File::Codeowners';
9 plan skip_all => 'Test::File::Codeowners required for testing CODEOWNERS' if $@;
10
11 codeowners_syntax_ok();
12 done_testing;
13
14 =head1 DESCRIPTION
15
16 This package has assertion subroutines for testing F<CODEOWNERS> files.
17
18 =cut
19
20 use warnings;
21 use strict;
22
23 use App::Codeowners::Util qw(find_nearest_codeowners git_ls_files git_toplevel);
24 use Encode qw(encode);
25 use File::Codeowners;
26 use Test::Builder;
27
28 our $VERSION = '9999.999'; # VERSION
29
30 my $Test = Test::Builder->new;
31
32 sub import {
33 my $self = shift;
34 my $caller = caller;
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;
38
39 $Test->exported_to($caller);
40 $Test->plan(@_);
41 }
42
43 =func codeowners_syntax_ok
44
45 codeowners_syntax_ok(); # search up the tree for a CODEOWNERS file
46 codeowners_syntax_ok($filepath);
47
48 Check the syntax of a F<CODEOWNERS> file.
49
50 =cut
51
52 sub codeowners_syntax_ok {
53 my $filepath = shift || find_nearest_codeowners();
54
55 eval { File::Codeowners->parse($filepath) };
56 my $err = $@;
57
58 $Test->ok(!$err, "Check syntax: $filepath");
59 $Test->diag($err) if $err;
60 }
61
62 =func codeowners_git_files_ok
63
64 codeowners_git_files_ok(); # search up the tree for a CODEOWNERS file
65 codeowners_git_files_ok($filepath);
66
67 =cut
68
69 sub codeowners_git_files_ok {
70 my $filepath = shift || find_nearest_codeowners();
71
72 $Test->subtest('codeowners_git_files_ok' => sub {
73 my $codeowners = eval { File::Codeowners->parse($filepath) };
74 if (my $err = $@) {
75 $Test->plan(tests => 1);
76 $Test->ok(0, "Parse $filepath");
77 $Test->diag($err);
78 return;
79 }
80
81 my $files = git_ls_files(git_toplevel());
82
83 $Test->plan(@$files ? (tests => scalar @$files) : (skip_all => 'git ls-files failed'));
84
85 for my $filepath (@$files) {
86 my $msg = encode('UTF-8', "Check file: $filepath");
87
88 my $match = $codeowners->match($filepath);
89 my $is_unowned = $codeowners->is_unowned($filepath);
90
91 if (!$match && !$is_unowned) {
92 $Test->ok(0, $msg);
93 $Test->diag("File is unowned\n");
94 }
95 elsif ($match && $is_unowned) {
96 $Test->ok(0, $msg);
97 $Test->diag("File is owned but listed as unowned\n");
98 }
99 else {
100 $Test->ok(1, $msg);
101 }
102 }
103 });
104 }
105
106 1;
This page took 0.035758 seconds and 4 git commands to generate.