1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# frozen_string_literal: true
require_relative '../../../../tooling/lib/tooling/find_codeowners'
RSpec.describe Tooling::FindCodeowners do
let(:subject) { described_class.new }
let(:root) { File.expand_path('../../fixtures/find_codeowners', __dir__) }
describe '#execute' do
before do
allow(subject).to receive(:load_config).and_return(
'[Section name]': {
'@group': {
allow: {
keywords: %w[dir0 file],
patterns: ['/%{keyword}/**/*', '/%{keyword}']
},
deny: {
keywords: %w[file0],
patterns: ['**/%{keyword}']
}
}
}
)
end
it 'prints CODEOWNERS as configured' do
expect do
Dir.chdir(root) do
subject.execute
end
end.to output(<<~CODEOWNERS).to_stdout
[Section name]
/dir0/dir1/ @group
/file @group
CODEOWNERS
end
end
describe '#load_definitions' do
before do
allow(subject).to receive(:load_config).and_return(
{
'[Authentication and Authorization]': {
'@gitlab-org/manage/authentication-and-authorization': {
allow: {
keywords: %w[password auth token],
patterns:
%w[
/{,ee/}app/**/*%{keyword}*{,/**/*}
/{,ee/}config/**/*%{keyword}*{,/**/*}
/{,ee/}lib/**/*%{keyword}*{,/**/*}
]
},
deny: {
keywords: %w[*author.* *author_* *authored*],
patterns: ['%{keyword}']
}
}
}
}
)
end
it 'expands the allow and deny list with keywords and patterns' do
subject.load_definitions.each do |section, group_defintions|
group_defintions.each do |group, definitions|
expect(definitions[:allow]).to be_an(Array)
expect(definitions[:deny]).to be_an(Array)
end
end
end
it 'expands the auth group' do
auth = subject.load_definitions.dig(
:'[Authentication and Authorization]',
:'@gitlab-org/manage/authentication-and-authorization')
expect(auth).to eq(
allow: %w[
/{,ee/}app/**/*password*{,/**/*}
/{,ee/}config/**/*password*{,/**/*}
/{,ee/}lib/**/*password*{,/**/*}
/{,ee/}app/**/*auth*{,/**/*}
/{,ee/}config/**/*auth*{,/**/*}
/{,ee/}lib/**/*auth*{,/**/*}
/{,ee/}app/**/*token*{,/**/*}
/{,ee/}config/**/*token*{,/**/*}
/{,ee/}lib/**/*token*{,/**/*}
],
deny: %w[
*author.*
*author_*
*authored*
]
)
end
end
describe '#load_config' do
it 'loads the config with symbolized keys' do
config = subject.load_config
expect_hash_keys_to_be_symbols(config)
end
context 'when YAML has safe_load_file' do
before do
allow(YAML).to receive(:respond_to?).with(:safe_load_file).and_return(true)
end
it 'calls safe_load_file' do
expect(YAML).to receive(:safe_load_file)
subject.load_config
end
end
context 'when YAML does not have safe_load_file' do
before do
allow(YAML).to receive(:respond_to?).with(:safe_load_file).and_return(false)
end
it 'calls load_file' do
expect(YAML).to receive(:safe_load)
subject.load_config
end
end
def expect_hash_keys_to_be_symbols(object)
if object.is_a?(Hash)
object.each do |key, value|
expect(key).to be_a(Symbol)
expect_hash_keys_to_be_symbols(value)
end
end
end
end
describe '#path_matches?' do
let(:pattern) { 'pattern' }
let(:path) { 'path' }
it 'passes flags we are expecting to File.fnmatch?' do
expected_flags =
::File::FNM_DOTMATCH | ::File::FNM_PATHNAME | ::File::FNM_EXTGLOB
expect(File).to receive(:fnmatch?)
.with("/**/#{pattern}", path, expected_flags)
subject.path_matches?(pattern, path)
end
end
describe '#normalize_pattern' do
it 'returns /**/* if the input is *' do
expect(subject.normalize_pattern('*')).to eq('/**/*')
end
it 'prepends /** if the input does not start with /' do
expect(subject.normalize_pattern('app')).to eq('/**/app')
end
it 'returns the pattern if the input starts with /' do
expect(subject.normalize_pattern('/app')).to eq('/app')
end
it 'appends **/* if the input ends with /' do
expect(subject.normalize_pattern('/app/')).to eq('/app/**/*')
end
end
describe '#consolidate_paths' do
before do
allow(subject).to receive(:find_dir_maxdepth_1).and_return(<<~LINES)
dir
dir/0
dir/2
dir/3
dir/1
LINES
end
context 'when the directory has the same number of entries' do
let(:input_paths) { %W[dir/0\n dir/1\n dir/2\n dir/3\n] }
it 'consolidates into the directory' do
paths = subject.consolidate_paths(input_paths)
expect(paths).to eq(["dir\n"])
end
end
context 'when the directory has different number of entries' do
let(:input_paths) { %W[dir/0\n dir/1\n dir/2\n] }
it 'returns the original paths' do
paths = subject.consolidate_paths(input_paths)
expect(paths).to eq(input_paths)
end
end
end
end
|