Skip to content

Commit 3fcce96

Browse files
schoblaskaprofessortstannardperryqhashleywillard
authored
restrict mappers to files that are actually in param (#92)
* restrict mappers to files that are actually in param * remove need for T.unsafe * store file count for using expanded cache in constant --------- Co-authored-by: Todd Sedano <[email protected]> Co-authored-by: Teal Stannard <[email protected]> Co-authored-by: Perry Hertler <[email protected]> Co-authored-by: Ashley Willard <[email protected]>
1 parent ab91bce commit 3fcce96

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/code_ownership/private/glob_cache.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module Private
66
class GlobCache
77
extend T::Sig
88

9+
EXPANDED_CACHE_FILE_COUNT_THRESHOLD = 100
10+
911
MapperDescription = T.type_alias { String }
1012

1113
CacheShape = T.type_alias do
@@ -34,10 +36,15 @@ def raw_cache_contents
3436

3537
sig { params(files: T::Array[String]).returns(FilesByMapper) }
3638
def mapper_descriptions_that_map_files(files)
37-
if files.count > 100
39+
if files.count > EXPANDED_CACHE_FILE_COUNT_THRESHOLD
3840
# When looking at many files, expanding the cache out using Dir.glob and checking for intersections is faster
3941
files_by_mappers = files.map{ |f| [f, Set.new([]) ]}.to_h
40-
files_by_mappers.merge(files_by_mappers_via_expanded_cache)
42+
43+
files_by_mappers_via_expanded_cache.each do |file, mapper|
44+
T.must(files_by_mappers[file]) << mapper if files_by_mappers[file]
45+
end
46+
47+
files_by_mappers
4148
else
4249
# When looking at few files, using File.fnmatch is faster
4350
files_by_mappers_via_file_fnmatch(files)

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,27 @@ module CodeOwnership
6666
end
6767
end
6868

69-
context 'the input files do not include the file owned in multiple ways' do
70-
it 'ignores the file with multiple ownership' do
71-
expect { CodeOwnership.validate!(files: ['app/services/some_other_file.rb']) }.to_not raise_error
69+
it "ignores the file with multiple ownership if it's not in the files param" do
70+
expect { CodeOwnership.validate!(files: ['app/services/some_other_file.rb']) }.to_not raise_error
71+
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
7290
end
7391
end
7492
end

0 commit comments

Comments
 (0)