Skip to content

Commit ce31f6a

Browse files
author
Alex Evanczuk
authored
Tidy up references to code_ownership configuration in test (#51)
* Tidy up test references to config/code_ownership.yml * Ignore unowned globs earlier * bump version * small nit
1 parent 5b5fb1c commit ce31f6a

16 files changed

+41
-78
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_ownership (1.32.5)
4+
code_ownership (1.32.6)
55
code_teams (~> 1.0)
66
packs
77
sorbet-runtime

code_ownership.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "code_ownership"
3-
spec.version = '1.32.5'
3+
spec.version = '1.32.6'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'A gem to help engineering teams declare ownership of code'

lib/code_ownership/private.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def self.path_from_klass(klass)
7676
sig { returns(T::Array[String]) }
7777
def self.tracked_files
7878
@tracked_files ||= T.let(@tracked_files, T.nilable(T::Array[String]))
79-
@tracked_files ||= Dir.glob(configuration.owned_globs)
79+
@tracked_files ||= Dir.glob(configuration.owned_globs) - Dir.glob(configuration.unowned_globs)
8080
end
8181

8282
sig { params(team_name: String, location_of_reference: String).returns(CodeTeams::Team) }

lib/code_ownership/private/validations/files_have_owners.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ class FilesHaveOwners
1010

1111
sig { override.params(files: T::Array[String], autocorrect: T::Boolean, stage_changes: T::Boolean).returns(T::Array[String]) }
1212
def validation_errors(files:, autocorrect: true, stage_changes: true)
13-
allow_list = Dir.glob(Private.configuration.unowned_globs)
1413
files_by_mapper = Private.glob_cache.files_by_mapper
1514

1615
files_not_mapped_at_all = files.select do |file|
1716
files_by_mapper.fetch(file, []).count == 0
1817
end
1918

20-
files_without_owners = files_not_mapped_at_all - allow_list
21-
2219
errors = T.let([], T::Array[String])
2320

24-
if files_without_owners.any?
21+
if files_not_mapped_at_all.any?
2522
errors << <<~MSG
2623
Some files are missing ownership:
2724
28-
#{files_without_owners.map { |file| "- #{file}" }.join("\n")}
25+
#{files_not_mapped_at_all.map { |file| "- #{file}" }.join("\n")}
2926
MSG
3027
end
3128

spec/lib/code_ownership/cli_spec.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
let(:argv) { ['validate'] }
66

77
before do
8-
write_file('config/code_ownership.yml', <<~YML)
9-
owned_globs:
10-
- 'app/**/*.rb'
11-
YML
12-
8+
write_configuration
139
write_file('app/services/my_file.rb')
1410
write_file('frontend/javascripts/my_file.jsx')
1511
end
@@ -19,7 +15,7 @@
1915
expect(CodeOwnership).to receive(:validate!) do |args| # rubocop:disable RSpec/MessageSpies
2016
expect(args[:autocorrect]).to eq true
2117
expect(args[:stage_changes]).to eq true
22-
expect(args[:files]).to match_array(['app/services/my_file.rb'])
18+
expect(args[:files]).to match_array(["app/services/my_file.rb", "frontend/javascripts/my_file.jsx"])
2319
end
2420
subject
2521
end
@@ -29,10 +25,7 @@
2925

3026
describe 'for_file' do
3127
before do
32-
write_file('config/code_ownership.yml', <<~YML)
33-
owned_globs:
34-
- 'app/**/*.rb'
35-
YML
28+
write_configuration
3629

3730
write_file('app/services/my_file.rb')
3831
write_file('config/teams/my_team.yml', <<~YML)

spec/lib/code_ownership/private/extension_loader_spec.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ module CodeOwnership
44
let(:codeowners_validation) { Private::Validations::GithubCodeownersUpToDate }
55

66
before do
7-
write_file('config/code_ownership.yml', <<~YML)
8-
owned_globs:
9-
- app/**/*.rb
10-
require:
11-
- ./lib/my_extension.rb
12-
YML
7+
write_configuration('require' => ['./lib/my_extension.rb'])
138

149
write_file('config/teams/bar.yml', <<~CONTENTS)
1510
name: Bar
@@ -31,7 +26,7 @@ class MyExtension
3126
returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
3227
end
3328
def map_files_to_owners(files) # rubocop:disable Lint/UnusedMethodArgument
34-
files.map{|f| [f, CodeTeams.all.last]}.to_h
29+
files.select{|f| Pathname.new(f).extname == '.rb'}.map{|f| [f, CodeTeams.all.last]}.to_h
3530
end
3631
3732
sig do
@@ -46,7 +41,7 @@ def map_file_to_owner(file)
4641
override.returns(T::Hash[String, T.nilable(::CodeTeams::Team)])
4742
end
4843
def codeowners_lines_to_owners
49-
Dir.glob('**/*.*').map{|f| [f, CodeTeams.all.last]}.to_h
44+
Dir.glob('**/*.rb').map{|f| [f, CodeTeams.all.last]}.to_h
5045
end
5146
5247
sig { override.returns(String) }
@@ -105,8 +100,6 @@ def bust_caches!
105100
106101
# My special extension
107102
/app/services/my_ownable_file.rb @org/my-team
108-
/config/code_ownership.yml @org/my-team
109-
/config/teams/bar.yml @org/my-team
110103
/lib/my_extension.rb @org/my-team
111104
EXPECTED
112105
end

spec/lib/code_ownership/private/ownership_mappers/file_annotations_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module CodeOwnership
22
RSpec.describe Private::OwnershipMappers::FileAnnotations do
33
describe '.for_team' do
44
before do
5-
create_configuration
5+
write_configuration
66
write_file('config/teams/bar.yml', <<~CONTENTS)
77
name: Bar
88
CONTENTS
@@ -36,7 +36,7 @@ module CodeOwnership
3636
describe '.for_file' do
3737
context 'ruby owned file' do
3838
before do
39-
create_configuration
39+
write_configuration
4040
write_file('config/teams/bar.yml', <<~CONTENTS)
4141
name: Bar
4242
CONTENTS
@@ -53,7 +53,7 @@ module CodeOwnership
5353

5454
context 'javascript owned file' do
5555
before do
56-
create_configuration
56+
write_configuration
5757
write_file('config/teams/bar.yml', <<~CONTENTS)
5858
name: Bar
5959
CONTENTS
@@ -80,7 +80,7 @@ module CodeOwnership
8080
write_file('config/teams/foo.yml', <<~CONTENTS)
8181
name: Foo
8282
CONTENTS
83-
create_minimal_configuration
83+
write_configuration
8484
end
8585

8686
context 'ruby file has no annotation' do

spec/lib/code_ownership/private/ownership_mappers/js_package_ownership_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module CodeOwnership
33
describe 'CodeOwnership.validate!' do
44
context 'application has invalid JSON in package' do
55
before do
6-
write_file('config/code_ownership.yml', {}.to_yaml)
6+
write_configuration
77

88
write_file('frontend/javascripts/my_package/package.json', <<~CONTENTS)
99
{ syntax error!!!
@@ -27,7 +27,7 @@ module CodeOwnership
2727

2828
describe 'CodeOwnershp.for_file' do
2929
before do
30-
create_configuration
30+
write_configuration
3131

3232
write_file('frontend/javascripts/packages/my_other_package/package.json', <<~CONTENTS)
3333
{

spec/lib/code_ownership/private/ownership_mappers/package_ownership_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module CodeOwnership
22
RSpec.describe Private::OwnershipMappers::PackageOwnership do
33
before do
4-
create_configuration
4+
write_configuration
55
write_file('config/teams/bar.yml', <<~CONTENTS)
66
name: Bar
77
CONTENTS

spec/lib/code_ownership/private/ownership_mappers/team_globs_spec.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module CodeOwnership
22
RSpec.describe Private::OwnershipMappers::TeamGlobs do
3-
before { create_configuration }
3+
before { write_configuration }
44

55
describe 'CodeOwnership.for_file' do
66
before do
@@ -27,10 +27,7 @@ module CodeOwnership
2727
describe 'CodeOwnership.validate!' do
2828
context 'two teams own the same exact glob' do
2929
before do
30-
write_file('config/code_ownership.yml', <<~YML)
31-
owned_globs:
32-
- '{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx}'
33-
YML
30+
write_configuration
3431

3532
write_file('packs/my_pack/owned_file.rb')
3633
write_file('frontend/javascripts/blah/my_file.rb')

spec/lib/code_ownership/private/ownership_mappers/team_yml_ownership_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module CodeOwnership
22
RSpec.describe Private::OwnershipMappers::TeamYmlOwnership do
33
before do
4-
create_configuration
4+
write_configuration
55
write_file('config/teams/bar.yml', <<~CONTENTS)
66
name: Bar
77
CONTENTS

spec/lib/code_ownership/private/validations/files_have_owners_spec.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module CodeOwnership
88
write_file('Gemfile', <<~CONTENTS)
99
CONTENTS
1010

11-
create_minimal_configuration
11+
write_configuration
1212
end
1313

1414
it 'does not raise an error' do
@@ -24,7 +24,7 @@ module CodeOwnership
2424

2525
context 'the file is not in unowned_globs' do
2626
before do
27-
create_minimal_configuration
27+
write_configuration
2828
end
2929

3030
it 'lets the user know the file must have ownership' do
@@ -50,12 +50,7 @@ module CodeOwnership
5050

5151
context 'that file is in unowned_globs' do
5252
before do
53-
write_file('config/code_ownership.yml', <<~YML)
54-
owned_globs:
55-
- 'app/**/*.rb'
56-
unowned_globs:
57-
- app/missing_ownership.rb
58-
YML
53+
write_configuration('unowned_globs' => ['app/missing_ownership.rb', 'config/code_ownership.yml'])
5954
end
6055

6156
it 'does not raise an error' do

spec/lib/code_ownership/private/validations/files_have_unique_owners_spec.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ module CodeOwnership
33
describe 'CodeOwnership.validate!' do
44
context 'a file in owned_globs has ownership defined in multiple ways' do
55
before do
6-
write_file('config/code_ownership.yml', <<~YML)
7-
owned_globs:
8-
- '{app,components,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}'
9-
YML
6+
write_configuration
107

118
write_file('app/services/some_other_file.rb', <<~YML)
129
# @team Bar

spec/lib/code_ownership/private/validations/github_codeowners_up_to_date_spec.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module CodeOwnership
55

66
context 'run with autocorrect' do
77
before do
8-
create_minimal_configuration
8+
write_configuration
99
end
1010

1111
context 'in an empty application' do
@@ -157,7 +157,7 @@ module CodeOwnership
157157

158158
context 'run without staging changes' do
159159
before do
160-
create_minimal_configuration
160+
write_configuration
161161
end
162162

163163
it 'does not stage the changes to the codeowners file' do
@@ -180,7 +180,7 @@ module CodeOwnership
180180

181181
context 'run without autocorrect' do
182182
before do
183-
create_minimal_configuration
183+
write_configuration
184184
end
185185

186186
context 'in an empty application' do
@@ -469,11 +469,7 @@ module CodeOwnership
469469

470470
context 'code_ownership.yml has skip_codeowners_validation set' do
471471
before do
472-
write_file('config/code_ownership.yml', <<~YML)
473-
owned_globs:
474-
- app/**/*.rb
475-
skip_codeowners_validation: true
476-
YML
472+
write_configuration('skip_codeowners_validation' => true)
477473
end
478474

479475
it 'skips validating the codeowners file' do

spec/lib/code_ownership_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
name: Bar
88
CONTENTS
99

10-
create_minimal_configuration
10+
write_configuration
1111
end
1212

1313
context 'invalid team in a file annotation' do
@@ -88,7 +88,7 @@
8888
describe '.for_backtrace' do
8989
before do
9090
create_files_with_defined_classes
91-
create_minimal_configuration
91+
write_configuration
9292
end
9393

9494
context 'excluded_teams is not passed in as an input parameter' do
@@ -111,7 +111,7 @@
111111

112112
describe '.first_owned_file_for_backtrace' do
113113
before do
114-
create_minimal_configuration
114+
write_configuration
115115
create_files_with_defined_classes
116116
end
117117

@@ -145,7 +145,7 @@
145145
describe '.for_class' do
146146
before do
147147
create_files_with_defined_classes
148-
create_minimal_configuration
148+
write_configuration
149149
end
150150

151151
it 'can find the right owner for a class' do

spec/support/application_fixtures.rb

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
RSpec.shared_context 'application fixtures' do
22
let(:codeowners_path) { Pathname.pwd.join('.github/CODEOWNERS') }
33

4-
let(:create_configuration) do
5-
write_file('config/code_ownership.yml', <<~YML)
6-
owned_globs:
7-
- '{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx}'
8-
YML
9-
end
104

11-
let(:create_minimal_configuration) do
12-
write_file('config/code_ownership.yml', <<~YML)
13-
owned_globs:
14-
- app/**/*.{rb,jsx}
15-
YML
5+
def write_configuration(owned_globs: nil, **kwargs)
6+
owned_globs ||= ['{app,components,config,frontend,lib,packs,spec}/**/*.{rb,rake,js,jsx,ts,tsx,json,yml}']
7+
config = {
8+
'owned_globs' => owned_globs,
9+
'unowned_globs' => ['config/code_ownership.yml']
10+
}.merge(kwargs)
11+
write_file('config/code_ownership.yml', config.to_yaml)
1612
end
1713

1814
let(:create_non_empty_application) do
19-
create_configuration
15+
write_configuration
2016

2117
write_file('frontend/javascripts/packages/my_package/owned_file.jsx', <<~CONTENTS)
2218
// @team Bar
@@ -26,7 +22,6 @@
2622
# @team Bar
2723
CONTENTS
2824

29-
3025
write_file('frontend/javascripts/packages/my_other_package/package.json', <<~CONTENTS)
3126
{
3227
"name": "@gusto/my_package",

0 commit comments

Comments
 (0)