]> Dogcows Code - chaz/git-codeowners/blob - t/file-codeowners.t
add rename_project function to File::Codeowners
[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 'handling projects', sub {
98 my $file = File::Codeowners->parse("$Bin/samples/kitchensink.CODEOWNERS");
99 my $r;
100
101 is_deeply($r = $file->projects, [
102 'Transportation',
103 ], 'projects listed') or diag explain $r;
104
105 $file->rename_project('Transportation', 'Getting Around');
106 is_deeply($r = $file->projects, [
107 'Getting Around',
108 ], 'project renamed') or diag explain $r;
109
110 is_deeply($r = [@{$file->_lines}[-3 .. -1]], [
111 {comment => ' Project: Getting Around', project => 'Getting Around'},
112 {},
113 {pattern => '/vehicles/**/batmobile.cad', 'owners' => ['@"Lucius Fox"'], project => 'Getting Around'},
114 ], 'renaming project properly modifies lines') or diag explain $r;
115
116 $file->update_owners_by_project('Getting Around', '@twoface');
117 ok( scalar grep { $_ eq '@twoface' } @{$file->owners}, 'updating owner adds new owner');
118 ok(!scalar grep { $_ eq '@"Lucius Fox"' } @{$file->owners}, 'updating owner removes old owner');
119 };
120
121 subtest 'editing and writing files', sub {
122 my $file = File::Codeowners->parse("$Bin/samples/basic.CODEOWNERS");
123 my $r;
124
125 $file->update_owners('*' => [qw(@foo @bar @baz)]);
126 is_deeply($r = $file->_lines, [
127 {comment => 'wat'},
128 {pattern => '*', owners => [qw(@foo @bar @baz)]},
129 ], 'update owners for a pattern') or diag explain $r;
130 is_deeply($r = $file->owners, [qw(@bar @baz @foo)], 'got updated owners') or diag explain $r;
131
132 $file->update_owners('no/such/pattern' => [qw(@wuf)]);
133 is_deeply($r = $file->_lines, [
134 {comment => 'wat'},
135 {pattern => '*', owners => [qw(@foo @bar @baz)]},
136 ], 'no change when updating nonexistent pattern') or diag explain $r;
137
138 $file->prepend(comment => 'start');
139 $file->append(pattern => 'end', owners => ['@qux']);
140 is_deeply($r = $file->_lines, [
141 {comment => 'start'},
142 {comment => 'wat'},
143 {pattern => '*', owners => [qw(@foo @bar @baz)]},
144 {pattern => 'end', owners => [qw(@qux)]},
145 ], 'prepand and append') or diag explain $r;
146
147 $file->add_unowned('lonely', 'afraid');
148 is_deeply($r = $file->unowned, [qw(afraid lonely)], 'set unowned files') or diag explain $r;
149
150 $file->remove_unowned('afraid');
151 is_deeply($r = $file->unowned, [qw(lonely)], 'remove unowned files') or diag explain $r;
152
153 is_deeply($r = $file->write_to_array, [
154 '#start',
155 '#wat',
156 '* @foo @bar @baz',
157 'end @qux',
158 '',
159 '### UNOWNED (File::Codeowners)',
160 '# lonely',
161 ], 'format file') or diag explain $r;
162
163 $file->clear_unowned;
164 is_deeply($r = $file->unowned, [], 'clear unowned files') or diag explain $r;
165 };
166
167 done_testing;
This page took 0.046701 seconds and 4 git commands to generate.