Skip to content

Commit bf62bd1

Browse files
authored
always use expanded cache when getting mapper descriptions for files (#104)
* always use expanded cache when getting mapper descriptions for files * add missing loop for mappers * bump version (1.36.2 => 1.36.3)
1 parent d4eccb5 commit bf62bd1

File tree

4 files changed

+8
-65
lines changed

4 files changed

+8
-65
lines changed

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.36.2'
3+
spec.version = '1.36.3'
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/glob_cache.rb

+5-36
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module Private
66
class GlobCache
77
extend T::Sig
88

9-
EXPANDED_CACHE_FILE_COUNT_THRESHOLD = 100
10-
119
MapperDescription = T.type_alias { String }
1210

1311
CacheShape = T.type_alias do
@@ -36,19 +34,15 @@ def raw_cache_contents
3634

3735
sig { params(files: T::Array[String]).returns(FilesByMapper) }
3836
def mapper_descriptions_that_map_files(files)
39-
if files.count > EXPANDED_CACHE_FILE_COUNT_THRESHOLD
40-
# When looking at many files, expanding the cache out using Dir.glob and checking for intersections is faster
41-
files_by_mappers = files.to_h { |f| [f, Set.new([])] }
37+
files_by_mappers = files.to_h { |f| [f, Set.new([])] }
4238

43-
files_by_mappers_via_expanded_cache.each do |file, mapper|
39+
files_by_mappers_via_expanded_cache.each do |file, mappers|
40+
mappers.each do |mapper|
4441
T.must(files_by_mappers[file]) << mapper if files_by_mappers[file]
4542
end
46-
47-
files_by_mappers
48-
else
49-
# When looking at few files, using File.fnmatch is faster
50-
files_by_mappers_via_file_fnmatch(files)
5143
end
44+
45+
files_by_mappers
5246
end
5347

5448
private
@@ -81,31 +75,6 @@ def files_by_mappers_via_expanded_cache
8175
files_by_mappers
8276
end
8377
end
84-
85-
sig { params(files: T::Array[String]).returns(FilesByMapper) }
86-
def files_by_mappers_via_file_fnmatch(files)
87-
files_by_mappers = T.let({}, FilesByMapper)
88-
89-
files.each do |file|
90-
files_by_mappers[file] ||= Set.new([])
91-
@raw_cache_contents.each do |mapper_description, globs_by_owner|
92-
# As much as I'd like to *not* special case the file annotations mapper, using File.fnmatch? on the thousands of files mapped by the
93-
# file annotations mapper is a lot of unnecessary extra work.
94-
# Therefore we can just check if the file is in the globs directly for file annotations, otherwise use File.fnmatch
95-
if mapper_description == OwnershipMappers::FileAnnotations::DESCRIPTION
96-
files_by_mappers.fetch(file) << mapper_description if globs_by_owner[file]
97-
else
98-
globs_by_owner.each_key do |glob|
99-
if File.fnmatch?(glob, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
100-
files_by_mappers.fetch(file) << mapper_description
101-
end
102-
end
103-
end
104-
end
105-
end
106-
107-
files_by_mappers
108-
end
10978
end
11079
end
11180
end

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

-20
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,6 @@ module CodeOwnership
6969
it "ignores the file with multiple ownership if it's not in the files param" do
7070
expect { CodeOwnership.validate!(files: ['app/services/some_other_file.rb']) }.to_not raise_error
7171
end
72-
73-
context 'there are > 100 files in validation check' do
74-
let(:files) do
75-
files = []
76-
77-
101.times do |i|
78-
filename = "app/services/some_other_file#{i}.rb"
79-
files << filename
80-
write_file(filename, <<~YML)
81-
# @team Bar
82-
YML
83-
end
84-
85-
files
86-
end
87-
88-
it "ignores the file with multiple ownership if it's not in the files param" do
89-
expect { CodeOwnership.validate!(files: files) }.to_not raise_error
90-
end
91-
end
9272
end
9373

9474
context 'with mutliple directory ownership files' do

spec/lib/code_ownership_spec.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@
2727

2828
context 'file ownership with [] characters' do
2929
before do
30-
write_file('app/services/[test]/some_other_file.ts', <<~YML)
30+
write_file('app/services/[test]/some_file.ts', <<~TYPESCRIPT)
3131
// @team Bar
3232
// Countries
33-
YML
34-
35-
100.times do |i|
36-
write_file("app/services/withoutbracket/some_other_file#{i}.ts", <<~YML)
37-
// @team Bar
38-
YML
39-
end
33+
TYPESCRIPT
4034
end
4135

4236
it 'has no validation errors' do

0 commit comments

Comments
 (0)