]> Dogcows Code - chaz/git-codeowners/blob - lib/Test/File/Codeowners.pm
Version 0.46
[chaz/git-codeowners] / lib / Test / File / Codeowners.pm
1 package Test::File::Codeowners;
2 # ABSTRACT: Write tests for CODEOWNERS files
3
4
5 use warnings;
6 use strict;
7
8 use App::Codeowners::Util qw(find_nearest_codeowners git_ls_files git_toplevel);
9 use Encode qw(encode);
10 use File::Codeowners;
11 use Test::Builder;
12
13 our $VERSION = '0.46'; # VERSION
14
15 my $Test = Test::Builder->new;
16
17 sub import {
18 my $self = shift;
19 my $caller = caller;
20 no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
21 *{$caller.'::codeowners_syntax_ok'} = \&codeowners_syntax_ok;
22 *{$caller.'::codeowners_git_files_ok'} = \&codeowners_git_files_ok;
23
24 $Test->exported_to($caller);
25 $Test->plan(@_);
26 }
27
28
29 sub codeowners_syntax_ok {
30 my $filepath = shift || find_nearest_codeowners();
31
32 eval { File::Codeowners->parse($filepath) };
33 my $err = $@;
34
35 $Test->ok(!$err, "Check syntax: $filepath");
36 $Test->diag($err) if $err;
37 }
38
39
40 sub codeowners_git_files_ok {
41 my $filepath = shift || find_nearest_codeowners();
42
43 $Test->subtest('codeowners_git_files_ok' => sub {
44 my $codeowners = eval { File::Codeowners->parse($filepath) };
45 if (my $err = $@) {
46 $Test->plan(tests => 1);
47 $Test->ok(0, "Parse $filepath");
48 $Test->diag($err);
49 return;
50 }
51
52 my ($proc, @files) = git_ls_files(git_toplevel());
53
54 $Test->plan($proc->wait == 0 ? (tests => scalar @files) : (skip_all => 'git ls-files failed'));
55
56 for my $filepath (@files) {
57 my $msg = encode('UTF-8', "Check file: $filepath");
58
59 my $match = $codeowners->match($filepath);
60 my $is_unowned = $codeowners->is_unowned($filepath);
61
62 if (!$match && !$is_unowned) {
63 $Test->ok(0, $msg);
64 $Test->diag("File is unowned\n");
65 }
66 elsif ($match && $is_unowned) {
67 $Test->ok(0, $msg);
68 $Test->diag("File is owned but listed as unowned\n");
69 }
70 else {
71 $Test->ok(1, $msg);
72 }
73 }
74 });
75 }
76
77 1;
78
79 __END__
80
81 =pod
82
83 =encoding UTF-8
84
85 =head1 NAME
86
87 Test::File::Codeowners - Write tests for CODEOWNERS files
88
89 =head1 VERSION
90
91 version 0.46
92
93 =head1 SYNOPSIS
94
95 use Test::More;
96
97 eval 'use Test::File::Codeowners';
98 plan skip_all => 'Test::File::Codeowners required for testing CODEOWNERS' if $@;
99
100 codeowners_syntax_ok();
101 done_testing;
102
103 =head1 DESCRIPTION
104
105 This package has assertion subroutines for testing F<CODEOWNERS> files.
106
107 =head1 FUNCTIONS
108
109 =head2 codeowners_syntax_ok
110
111 codeowners_syntax_ok(); # search up the tree for a CODEOWNERS file
112 codeowners_syntax_ok($filepath);
113
114 Check the syntax of a F<CODEOWNERS> file.
115
116 =head2 codeowners_git_files_ok
117
118 codeowners_git_files_ok(); # search up the tree for a CODEOWNERS file
119 codeowners_git_files_ok($filepath);
120
121 =head1 BUGS
122
123 Please report any bugs or feature requests on the bugtracker website
124 L<https://github.com/chazmcgarvey/git-codeowners/issues>
125
126 When submitting a bug or request, please include a test-file or a
127 patch to an existing test-file that illustrates the bug or desired
128 feature.
129
130 =head1 AUTHOR
131
132 Charles McGarvey <chazmcgarvey@brokenzipper.com>
133
134 =head1 COPYRIGHT AND LICENSE
135
136 This software is copyright (c) 2019 by Charles McGarvey.
137
138 This is free software; you can redistribute it and/or modify it under
139 the same terms as the Perl 5 programming language system itself.
140
141 =cut
This page took 0.04269 seconds and 4 git commands to generate.