-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcode_ownership_spec.rb
330 lines (281 loc) · 11.4 KB
/
code_ownership_spec.rb
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
RSpec.describe CodeOwnership do
# Look at individual validations spec to see other validations that ship with CodeOwnership
describe '.validate!' do
describe 'teams must exist validation' do
before do
write_file('config/teams/bar.yml', <<~CONTENTS)
name: Bar
CONTENTS
write_configuration
end
context 'directory with [] characters' do
before do
write_file('app/services/.codeowner', <<~CONTENTS)
Bar
CONTENTS
write_file('app/services/test/some_unowned_file.rb', '')
write_file('app/services/[test]/some_unowned_file.rb', '')
end
it 'has no validation errors' do
expect { CodeOwnership.validate!(files: ['app/services/test/some_unowned_file.rb']) }.to_not raise_error
expect { CodeOwnership.validate!(files: ['app/services/[test]/some_unowned_file.rb']) }.to_not raise_error
end
end
context 'directory with [] characters containing a .codeowner file' do
before do
write_file('app/services/[test]/.codeowner', <<~CONTENTS)
Bar
CONTENTS
write_file('app/services/[test]/some_file.rb', '')
end
it 'has no validation errors' do
expect { CodeOwnership.validate!(files: ['app/services/[test]/some_file.rb']) }.to_not raise_error
end
end
context 'file ownership with [] characters' do
before do
write_file('app/services/[test]/some_file.ts', <<~TYPESCRIPT)
// @team Bar
// Countries
TYPESCRIPT
end
it 'has no validation errors' do
expect { CodeOwnership.validate!(files: ['app/services/withoutbracket/some_other_file.rb']) }.to_not raise_error
expect { CodeOwnership.validate!(files: ['app/services/[test]/some_other_file.rb']) }.to_not raise_error
expect { CodeOwnership.validate!(files: ['app/services/*/some_other_file.rb']) }.to_not raise_error
expect { CodeOwnership.validate!(files: ['app/*/[test]/some_other_file.rb']) }.to_not raise_error
expect { CodeOwnership.validate! }.to_not raise_error
end
end
context 'invalid team in a file annotation' do
before do
write_file('app/some_file.rb', <<~CONTENTS)
# @team Foo
CONTENTS
end
it 'lets the user know the team cannot be found in the file' do
expect { CodeOwnership.validate! }.to raise_error do |e|
expect(e).to be_a StandardError
expect(e.message).to eq <<~EXPECTED.chomp
Could not find team with name: `Foo` in app/some_file.rb. Make sure the team is one of `["Bar"]`
EXPECTED
end
end
end
context 'invalid team in a package.yml' do
before do
write_file('packs/my_pack/package.yml', <<~CONTENTS)
owner: Foo
CONTENTS
end
it 'lets the user know the team cannot be found in the package.yml' do
expect { CodeOwnership.validate! }.to raise_error do |e|
expect(e).to be_a StandardError
expect(e.message).to eq <<~EXPECTED.chomp
Could not find team with name: `Foo` in packs/my_pack/package.yml. Make sure the team is one of `["Bar"]`
EXPECTED
end
end
end
context 'invalid team in a package.yml using metadata' do
before do
write_file('packs/my_pack/package.yml', <<~CONTENTS)
metadata:
owner: Foo
CONTENTS
end
it 'lets the user know the team cannot be found in the package.yml' do
expect { CodeOwnership.validate! }.to raise_error do |e|
expect(e).to be_a StandardError
expect(e.message).to eq <<~EXPECTED.chomp
Could not find team with name: `Foo` in packs/my_pack/package.yml. Make sure the team is one of `["Bar"]`
EXPECTED
end
end
end
context 'invalid team in a package.json' do
before do
write_file('frontend/javascripts/my_package/package.json', <<~CONTENTS)
{
"metadata": {
"owner": "Foo"
}
}
CONTENTS
end
it 'lets the user know the team cannot be found in the package.json' do
expect { CodeOwnership.validate! }.to raise_error do |e|
expect(e).to be_a StandardError
expect(e.message).to eq <<~EXPECTED.chomp
Could not find team with name: `Foo` in frontend/javascripts/my_package. Make sure the team is one of `["Bar"]`
EXPECTED
end
end
end
end
context 'file is unowned' do
before do
write_file('config/teams/bar.yml', <<~CONTENTS)
name: Bar
CONTENTS
write_configuration
write_file('app/services/autogenerated_code/some_unowned_file.rb', '')
end
it 'has no validation errors' do
expect { CodeOwnership.validate!(files: ['app/services/autogenerated_code/some_unowned_file.rb']) }.to raise_error do |e|
expect(e.message).to eq <<~MSG.chomp
Some files are missing ownership:
- app/services/autogenerated_code/some_unowned_file.rb
See https://github.com/rubyatscale/code_ownership#README.md for more details
MSG
end
end
context 'ignored file passed in that is ignored' do
before do
write_configuration('unowned_globs' => ['app/services/autogenerated_code/**/**', 'vendor/bundle/**/**'])
end
it 'has no validation errors' do
expect { CodeOwnership.validate!(files: ['app/services/autogenerated_code/some_unowned_file.rb']) }.to_not raise_error
end
end
end
end
# See tests for individual ownership_mappers to understand behavior for each mapper
describe '.for_file' do
describe 'path formatting expectations' do
# All file paths must be clean paths relative to the root: https://apidock.com/ruby/Pathname/cleanpath
it 'will not find the ownership of a file that is not a cleanpath' do
expect(CodeOwnership.for_file('packs/my_pack/owned_file.rb')).to eq CodeTeams.find('Bar')
expect(CodeOwnership.for_file('./packs/my_pack/owned_file.rb')).to eq nil
end
end
context '.codeowner in a directory with [] characters' do
before do
write_file('app/javascript/[test]/.codeowner', <<~CONTENTS)
Bar
CONTENTS
write_file('app/javascript/[test]/test.js', '')
end
it 'properly assigns ownership' do
expect(CodeOwnership.for_file('app/javascript/[test]/test.js')).to eq CodeTeams.find('Bar')
end
end
before { create_non_empty_application }
end
describe '.for_backtrace' do
before do
create_files_with_defined_classes
write_configuration
end
context 'excluded_teams is not passed in as an input parameter' do
it 'finds the right team' do
expect { MyFile.raise_error }.to raise_error do |ex|
expect(CodeOwnership.for_backtrace(ex.backtrace)).to eq CodeTeams.find('Bar')
end
end
end
context 'excluded_teams is passed in as an input parameter' do
it 'ignores the first part of the stack trace and finds the next viable owner' do
expect { MyFile.raise_error }.to raise_error do |ex|
team_to_exclude = CodeTeams.find('Bar')
expect(CodeOwnership.for_backtrace(ex.backtrace, excluded_teams: [team_to_exclude])).to eq CodeTeams.find('Foo')
end
end
end
end
describe '.first_owned_file_for_backtrace' do
before do
write_configuration
create_files_with_defined_classes
end
context 'excluded_teams is not passed in as an input parameter' do
it 'finds the right team' do
expect { MyFile.raise_error }.to raise_error do |ex|
expect(CodeOwnership.first_owned_file_for_backtrace(ex.backtrace)).to eq [CodeTeams.find('Bar'), 'app/my_error.rb']
end
end
end
context 'excluded_teams is not passed in as an input parameter' do
it 'finds the right team' do
expect { MyFile.raise_error }.to raise_error do |ex|
team_to_exclude = CodeTeams.find('Bar')
expect(CodeOwnership.first_owned_file_for_backtrace(ex.backtrace, excluded_teams: [team_to_exclude])).to eq [CodeTeams.find('Foo'), 'app/my_file.rb']
end
end
end
context 'when nothing is owned' do
it 'returns nil' do
expect { raise 'opsy' }.to raise_error do |ex|
expect(CodeOwnership.first_owned_file_for_backtrace(ex.backtrace)).to be_nil
end
end
end
end
describe '.for_class' do
before do
create_files_with_defined_classes
write_configuration
end
it 'can find the right owner for a class' do
expect(CodeOwnership.for_class(MyFile)).to eq CodeTeams.find('Foo')
end
it 'memoizes the values' do
expect(CodeOwnership.for_class(MyFile)).to eq CodeTeams.find('Foo')
allow(CodeOwnership).to receive(:for_file)
allow(Object).to receive(:const_source_location)
expect(CodeOwnership.for_class(MyFile)).to eq CodeTeams.find('Foo')
# Memoization should avoid these calls
expect(CodeOwnership).to_not have_received(:for_file)
expect(Object).to_not have_received(:const_source_location)
end
it 'returns nil if the class constant cannot be found' do
allow(CodeOwnership).to receive(:for_file)
allow(Object).to receive(:const_source_location).and_raise(NameError)
expect(CodeOwnership.for_class(MyFile)).to eq nil
end
end
describe '.for_team' do
before { create_non_empty_application }
it 'prints out ownership information for the given team' do
expect(CodeOwnership.for_team('Bar')).to eq <<~OWNERSHIP
# Code Ownership Report for `Bar` Team
## Annotations at the top of file
- frontend/javascripts/packages/my_package/owned_file.jsx
- packs/my_pack/owned_file.rb
## Team-specific owned globs
- app/services/bar_stuff/**
- frontend/javascripts/bar_stuff/**
## Owner in .codeowner
- directory/owner/**/**
## Owner metadata key in package.yml
- packs/my_other_package/**/**
## Owner metadata key in package.json
- frontend/javascripts/packages/my_other_package/**/**
## Team YML ownership
- config/teams/bar.yml
OWNERSHIP
end
end
describe 'pack level ownership' do
# These errors show up from `bin/packwerk validate`, so using the `ApplicationValidator` to test
let(:validation_result) do
configuration = Packwerk::Configuration.from_path
package_set = Packwerk::PackageSet.load_all_from(
configuration.root_path,
package_pathspec: configuration.package_paths
)
Packwerk.const_get(:ApplicationValidator).new.call(package_set, configuration)
end
before do
# We stub this to avoid needing to set up a Rails app
allow(Packwerk::RailsLoadPaths).to receive(:for).and_return({ 'packs/my_pack/app/services' => Object })
write_pack('.')
write_pack('packs/my_pack', { 'owner' => 'Foo' })
write_file('packs/my_pack/app/services/my_pack.rb')
end
it 'does not create a validation error' do
expect(validation_result.error_value).to be_nil
expect(validation_result.ok?).to eq true
end
end
end