-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcode_ownership_spec.rb
More file actions
575 lines (458 loc) · 17.1 KB
/
code_ownership_spec.rb
File metadata and controls
575 lines (458 loc) · 17.1 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# frozen_string_literal: true
RSpec.describe CodeOwnership do
it 'has a version number' do
expect(CodeOwnership::VERSION).not_to be nil
end
context 'teams_for_files_from_codeowners' do
subject { CodeOwnership.teams_for_files_from_codeowners(files) }
let(:files) { ['app/services/my_file.rb'] }
context 'when config is not found' do
let(:files) { ['app/javascript/[test]/test.js'] }
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError, /Can't open config file:/)
end
end
context 'with non-empty application' do
before do
create_non_empty_application
# codeowners-rs is matching files against the codeowners file
RustCodeOwners.generate_and_validate(nil, false)
end
context 'when no ownership is found' do
let(:files) { ['app/madeup/file.rb'] }
it 'properly assigns ownership' do
expect(subject).to eq({ 'app/madeup/file.rb' => nil })
end
end
context 'when file path starts with ./' do
let(:files) { ['./app/javascript/[test]/test.js'] }
it 'properly assigns ownership' do
expect(subject).to eq({ './app/javascript/[test]/test.js' => nil })
end
end
context 'when ownership is found' do
let(:files) { ['packs/my_pack/owned_file.rb'] }
it 'returns the correct team' do
expect(subject).to eq({ 'packs/my_pack/owned_file.rb' => CodeTeams.find('Bar') })
end
context 'subsequent for_file utilizes cached team' do
let(:files) { ['packs/my_pack/owned_file.rb', 'packs/my_pack/owned_file2.rb'] }
it 'returns the correct team' do
subject # caches paths -> teams
allow(RustCodeOwners).to receive(:for_file)
expect(described_class.for_file('packs/my_pack/owned_file.rb')).to eq(CodeTeams.find('Bar'))
expect(RustCodeOwners).to_not have_received(:for_file)
end
end
end
context 'when ownership is found but team is not found' do
let(:file_path) { ['packs/my_pack/owned_file.rb'] }
before do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path.first => { team_name: 'Made Up Team' } })
end
it 'returns nil' do
expect(subject).to eq({ 'packs/my_pack/owned_file.rb' => nil })
end
end
context 'when ownership is found but team is not found and allow_raise is true' do
let(:files) { ['packs/my_pack/owned_file.rb'] }
before do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ files.first => { team_name: 'Made Up Team' } })
end
it 'raises an error' do
expect { CodeOwnership.teams_for_files_from_codeowners(files, allow_raise: true) }.to raise_error(StandardError, /Could not find team with name:/)
end
end
end
end
describe '.for_file_from_codeowners' do
subject { CodeOwnership.for_file(file_path, from_codeowners: true) }
context 'when config is not found' do
let(:file_path) { 'app/javascript/[test]/test.js' }
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError, /Can't open config file:/)
end
end
context 'with non-empty application' do
before do
create_non_empty_application
# codeowners-rs is matching files against the codeowners file
RustCodeOwners.generate_and_validate(nil, false)
end
context 'when no ownership is found' do
let(:file_path) { 'app/madeup/file.rb' }
it 'properly assigns ownership' do
expect(subject).to be_nil
end
end
context 'when file path starts with ./' do
let(:file_path) { './app/javascript/[test]/test.js' }
it 'properly assigns ownership' do
expect(subject).to be_nil
end
end
context 'when ownership is found' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
end
context 'subsequent for_file utilizes cached team' do
it 'returns the correct team' do
subject # caches path -> team
allow(RustCodeOwners).to receive(:for_file)
expect(described_class.for_file(file_path)).to eq(CodeTeams.find('Bar'))
expect(RustCodeOwners).to_not have_received(:for_file)
end
end
end
context 'when ownership is found but team is not found' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
before do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path => { team_name: 'Made Up Team' } })
end
it 'returns nil' do
expect(subject).to be_nil
end
end
context 'when ownership is found but team is not found and allow_raise is true' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
before do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path => { team_name: 'Made Up Team' } })
end
it 'raises an error' do
expect { CodeOwnership.for_file(file_path, from_codeowners: true, allow_raise: true) }.to raise_error(StandardError, /Could not find team with name:/)
end
end
end
end
describe '.for_file' do
subject { CodeOwnership.for_file(file_path) }
context 'when config is not found' do
let(:file_path) { 'app/javascript/[test]/test.js' }
it 'raises an error' do
expect { subject }.to raise_error(RuntimeError, /Can't open config file:/)
end
end
context 'with non-empty application' do
before do
create_non_empty_application
# codeowners-rs is matching files against the codeowners file for default path
RustCodeOwners.generate_and_validate(nil, false)
end
context 'when no ownership is found' do
let(:file_path) { 'app/madeup/file.rb' }
it 'properly assigns ownership' do
expect(subject).to be_nil
end
end
context 'when file path starts with ./' do
let(:file_path) { './app/javascript/[test]/test.js' }
it 'properly assigns ownership' do
expect(subject).to be_nil
end
end
context 'when ownership is found' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
end
context 'when ownership is cached' do
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
allow(RustCodeOwners).to receive(:teams_for_files)
expect(CodeOwnership.for_file(file_path)).to eq CodeTeams.find('Bar')
expect(RustCodeOwners).not_to have_received(:teams_for_files)
end
end
end
context 'when ownership is found via metadata.owner in package.yml' do
let(:file_path) { 'packs/metadata_owner_pack/some_file.rb' }
before do
write_file('packs/metadata_owner_pack/package.yml', <<~CONTENTS)
metadata:
owner: Foo
CONTENTS
write_file(file_path, '# some content')
RustCodeOwners.generate_and_validate(nil, false)
end
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Foo')
end
end
context 'when ownership is found but team is not found' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
before do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path => { team_name: 'Made Up Team' } })
end
it 'returns nil by default' do
expect(subject).to be_nil
end
end
context 'when ownership is found but team is not found and allow_raise is true' do
let(:file_path) { 'packs/my_pack/owned_file.rb' }
it 'raises an error when using from_codeowners path' do
allow(RustCodeOwners).to receive(:teams_for_files).and_return({ file_path => { team_name: 'Made Up Team' } })
expect { CodeOwnership.for_file(file_path, allow_raise: true) }.to raise_error(StandardError, /Could not find team with name:/)
end
it 'raises an error when using single-file path' do
allow(RustCodeOwners).to receive(:for_file).and_return({ team_name: 'Made Up Team' })
expect { CodeOwnership.for_file(file_path, from_codeowners: false, allow_raise: true) }.to raise_error(StandardError, /Could not find team with name:/)
end
end
end
end
describe '.for_class' do
subject { described_class.for_class(klass) }
let(:klass) do
described_class
end
let(:file_path) { 'packs/my_pack/owned_file.rb' }
before do
allow(CodeOwnership::Private::FilePathFinder).to receive(:path_from_klass).and_return(file_path)
end
context 'when the klass path is found' do
before do
create_non_empty_application
end
it 'calls for_file with the correct file path' do
subject
expect(CodeOwnership::Private::FilePathFinder).to have_received(:path_from_klass).with(klass)
end
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
end
end
context 'when the klass path is not found' do
let(:file_path) { nil }
it 'returns nil' do
expect(subject).to be_nil
end
end
end
describe '.for_package' do
subject { described_class.for_package(package) }
let(:package_yml_path) { 'packs/my_pack/package.yml' }
before do
create_non_empty_application
write_file(package_yml_path, raw_hash.to_yaml)
end
let(:package) do
Packs::Pack.from(Pathname.new(package_yml_path).realpath)
end
context 'with owner set' do
let(:raw_hash) { { 'owner' => 'Bar' } }
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
end
end
context 'with metadata owner set' do
let(:raw_hash) { { 'metadata' => { 'owner' => 'Bar' } } }
it 'returns the correct team' do
expect(subject).to eq CodeTeams.find('Bar')
end
end
context 'with no owner set' do
let(:raw_hash) { {} }
it 'returns nil' do
expect(subject).to be_nil
end
end
context 'with unknown owner' do
let(:raw_hash) { { 'owner' => 'Does Not Exist' } }
it 'raises helpful error' do
expect { subject }.to raise_error(StandardError, /Could not find team with name:/)
end
end
context 'with empty owner string' do
let(:raw_hash) { { 'owner' => '' } }
it 'raises helpful error' do
expect { subject }.to raise_error(StandardError, /Could not find team with name:/)
end
end
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(described_class.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(described_class.for_backtrace(ex.backtrace, excluded_teams: [team_to_exclude])).to eq CodeTeams.find('Foo')
end
end
end
context 'with nil backtrace' do
it 'returns nil' do
expect(described_class.for_backtrace(nil)).to be_nil
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(described_class.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(described_class.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 'bang!' }.to raise_error do |ex|
expect(described_class.first_owned_file_for_backtrace(ex.backtrace)).to be_nil
end
end
end
context 'with nil backtrace' do
it 'returns nil' do
expect(described_class.first_owned_file_for_backtrace(nil)).to be_nil
end
end
end
describe '.version' do
it 'returns the version' do
expect(described_class.version).to eq ["code_ownership version: #{CodeOwnership::VERSION}", "codeowners-rs version: #{RustCodeOwners.version}"]
end
end
describe '.remove_file_annotation!' do
subject(:remove_file_annotation) do
CodeOwnership.remove_file_annotation!(filename)
# Getting the owner gets stored in the cache, so after we remove the file annotation we want to bust the cache
CodeOwnership.bust_caches!
end
before do
write_file('config/teams/foo.yml', <<~CONTENTS)
name: Foo
github:
team: '@MyOrg/foo-team'
CONTENTS
write_configuration
end
context 'ruby file has no annotation' do
let(:filename) { 'app/my_file.rb' }
before do
write_file(filename, <<~CONTENTS)
# Empty file
CONTENTS
end
it 'has no effect' do
expect(File.read(filename)).to eq "# Empty file\n"
remove_file_annotation
expect(File.read(filename)).to eq "# Empty file\n"
end
end
context 'ruby file has annotation' do
let(:filename) { 'app/my_file.rb' }
before do
write_file(filename, <<~CONTENTS)
# @team Foo
# Some content
CONTENTS
RustCodeOwners.generate_and_validate(nil, false)
end
it 'removes the annotation' do
current_ownership = CodeOwnership.for_file(filename, from_codeowners: false)
expect(current_ownership&.name).to eq 'Foo'
expect(File.read(filename)).to eq <<~RUBY
# @team Foo
# Some content
RUBY
remove_file_annotation
new_ownership = CodeOwnership.for_file(filename, from_codeowners: false)
expect(new_ownership).to eq nil
expected_output = <<~RUBY
# Some content
RUBY
expect(File.read(filename)).to eq expected_output
end
end
context 'javascript file has annotation' do
let(:filename) { 'app/my_file.jsx' }
before do
write_file(filename, <<~CONTENTS)
// @team Foo
// Some content
CONTENTS
RustCodeOwners.generate_and_validate(nil, false)
end
it 'removes the annotation' do
current_ownership = CodeOwnership.for_file(filename, from_codeowners: false)
expect(current_ownership&.name).to eq 'Foo'
expect(File.read(filename)).to eq <<~JAVASCRIPT
// @team Foo
// Some content
JAVASCRIPT
remove_file_annotation
new_ownership = CodeOwnership.for_file(filename, from_codeowners: false)
expect(new_ownership).to eq nil
expected_output = <<~JAVASCRIPT
// Some content
JAVASCRIPT
expect(File.read(filename)).to eq expected_output
end
end
context "haml has annotation (only verifies file is changed, the curren implementation doesn't verify haml files)" do
let(:filename) { 'app/views/my_file.html.haml' }
before do
write_file(filename, <<~CONTENTS)
-# @team Foo
-# Some content
CONTENTS
end
it 'removes the annotation' do
expect(File.read(filename)).to eq <<~HAML
-# @team Foo
-# Some content
HAML
remove_file_annotation
expected_output = <<~HAML
-# Some content
HAML
expect(File.read(filename)).to eq expected_output
end
end
context 'file has new lines after the annotation' do
let(:filename) { 'app/my_file.rb' }
before do
write_file(filename, <<~CONTENTS)
# @team Foo
# Some content
# Some other content
CONTENTS
end
it 'removes the annotation and the leading new lines' do
expect(File.read(filename)).to eq <<~RUBY
# @team Foo
# Some content
# Some other content
RUBY
remove_file_annotation
expected_output = <<~RUBY
# Some content
# Some other content
RUBY
expect(File.read(filename)).to eq expected_output
end
end
end
end