]> Dogcows Code - chaz/git-codeowners/blob - t/file-codeowners.t
initial commit
[chaz/git-codeowners] / t / file-codeowners.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use FindBin '$Bin';
7
8 use File::Codeowners;
9 use Test::More;
10
11 subtest 'parse CODEOWNERS files', sub {
12 my @basic_arr = ('#wat', '* @whatever');
13 my $basic_str = "#wat\n* \@whatever\n";
14 my $expected = [
15 {comment => 'wat'},
16 {pattern => '*', owners => ['@whatever']},
17 ];
18 my $r;
19
20 my $file = File::Codeowners->parse_from_filepath("$Bin/samples/basic.CODEOWNERS");
21 is_deeply($r = $file->_lines, $expected, 'parse from filepath') or diag explain $r;
22
23 $file = File::Codeowners->parse_from_array(\@basic_arr);
24 is_deeply($r = $file->_lines, $expected, 'parse from array') or diag explain $r;
25
26 $file = File::Codeowners->parse_from_string(\$basic_str);
27 is_deeply($r = $file->_lines, $expected, 'parse from string') or diag explain $r;
28
29 open(my $fh, '<', \$basic_str) or die "open failed: $!";
30 $file = File::Codeowners->parse_from_fh($fh);
31 is_deeply($r = $file->_lines, $expected, 'parse from filehandle') or diag explain $r;
32 close($fh);
33 };
34
35 subtest 'query information from CODEOWNERS', sub {
36 my $file = File::Codeowners->parse("$Bin/samples/kitchensink.CODEOWNERS");
37 my $r;
38
39 is_deeply($r = $file->owners, [
40 '@"Lucius Fox"',
41 '@bane',
42 '@batman',
43 '@joker',
44 '@robin',
45 '@the-penguin',
46 'alfred@waynecorp.example.com',
47 ], 'list all owners') or diag explain $r;
48
49 is_deeply($r = $file->owners('tricks/Grinning/'), [qw(
50 @joker
51 @the-penguin
52 )], 'list owners matching pattern') or diag explain $r;
53
54 is_deeply($r = $file->patterns, [qw(
55 *
56 /a/b/c/deep
57 /vehicles/**/batmobile.cad
58 mansion.txt
59 tricks/Explosions.doc
60 tricks/Grinning/
61 )], 'list all patterns') or diag explain $r;
62
63 is_deeply($r = $file->patterns('@joker'), [qw(
64 tricks/Explosions.doc
65 tricks/Grinning/
66 )], 'list patterns matching owner') or diag explain $r;
67
68 is_deeply($r = $file->unowned, [qw(
69 lightcycle.cad
70 )], 'list unowned') or diag explain $r;
71
72 is_deeply($r = $file->match('whatever'), {
73 owners => [qw(@batman @robin)],
74 pattern => '*',
75 }, 'match solitary wildcard') or diag explain $r;
76 is_deeply($r = $file->match('subdir/mansion.txt'), {
77 owners => ['alfred@waynecorp.example.com'],
78 pattern => 'mansion.txt',
79 }, 'match filename') or diag explain $r;
80 is_deeply($r = $file->match('vehicles/batmobile.cad'), {
81 owners => ['@"Lucius Fox"'],
82 pattern => '/vehicles/**/batmobile.cad',
83 project => 'Transportation',
84 }, 'match double asterisk') or diag explain $r;
85 is_deeply($r = $file->match('vehicles/extra/batmobile.cad'), {
86 owners => ['@"Lucius Fox"'],
87 pattern => '/vehicles/**/batmobile.cad',
88 project => 'Transportation',
89 }, 'match double asterisk again') or diag explain $r;
90 };
91
92 subtest 'parse errors', sub {
93 eval { File::Codeowners->parse(\q{meh}) };
94 like($@, qr/^Parse error on line 1/, 'parse error');
95 };
96
97 subtest 'editing and writing files', sub {
98 my $file = File::Codeowners->parse("$Bin/samples/basic.CODEOWNERS");
99 my $r;
100
101 $file->update_owners('*' => [qw(@foo @bar @baz)]);
102 is_deeply($r = $file->_lines, [
103 {comment => 'wat'},
104 {pattern => '*', owners => [qw(@foo @bar @baz)]},
105 ], 'update owners for a pattern') or diag explain $r;
106 is_deeply($r = $file->owners, [qw(@bar @baz @foo)], 'got updated owners') or diag explain $r;
107
108 $file->update_owners('no/such/pattern' => [qw(@wuf)]);
109 is_deeply($r = $file->_lines, [
110 {comment => 'wat'},
111 {pattern => '*', owners => [qw(@foo @bar @baz)]},
112 ], 'no change when updating nonexistent pattern') or diag explain $r;
113
114 $file->prepend(comment => 'start');
115 $file->append(pattern => 'end', owners => ['@qux']);
116 is_deeply($r = $file->_lines, [
117 {comment => 'start'},
118 {comment => 'wat'},
119 {pattern => '*', owners => [qw(@foo @bar @baz)]},
120 {pattern => 'end', owners => [qw(@qux)]},
121 ], 'prepand and append') or diag explain $r;
122
123 $file->add_unowned('lonely', 'afraid');
124 is_deeply($r = $file->unowned, [qw(afraid lonely)], 'set unowned files') or diag explain $r;
125
126 $file->remove_unowned('afraid');
127 is_deeply($r = $file->unowned, [qw(lonely)], 'remove unowned files') or diag explain $r;
128
129 is_deeply($r = $file->write_to_array, [
130 '#start',
131 '#wat',
132 '* @foo @bar @baz',
133 'end @qux',
134 '',
135 '### UNOWNED (File::Codeowners)',
136 '# lonely',
137 ], 'format file') or diag explain $r;
138
139 $file->clear_unowned;
140 is_deeply($r = $file->unowned, [], 'clear unowned files') or diag explain $r;
141 };
142
143 done_testing;
This page took 0.041477 seconds and 4 git commands to generate.