Skip to content

Commit

Permalink
extracting OwnerAssigner for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
perryqh committed Feb 16, 2024
1 parent 9da5daa commit 3e4e7d6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/code_ownership/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'code_ownership/private/codeowners_file'
require 'code_ownership/private/parse_js_packages'
require 'code_ownership/private/glob_cache'
require 'code_ownership/private/owner_assigner'
require 'code_ownership/private/validations/files_have_owners'
require 'code_ownership/private/validations/github_codeowners_up_to_date'
require 'code_ownership/private/validations/files_have_unique_owners'
Expand Down Expand Up @@ -98,7 +99,6 @@ def self.file_tracked?(file)
in_unowned_globs = configuration.unowned_globs.any? do |unowned_glob|
File.fnmatch?(unowned_glob, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
end

in_owned_globs && !in_unowned_globs && File.exist?(file)
end

Expand Down
9 changes: 1 addition & 8 deletions lib/code_ownership/private/glob_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ def expanded_cache
@expanded_cache ||= begin
expanded_cache = {}
@raw_cache_contents.each do |mapper_description, globs_by_owner|
expanded_cache[mapper_description] = {}
globs_by_owner.each do |glob, owner|
expanded_cache[mapper_description][glob] = owner if File.exist?(glob)

Dir.glob(glob).each do |file, owner|
expanded_cache[mapper_description][file] = owner
end
end
expanded_cache[mapper_description] = OwnerAssigner.assign_owners(globs_by_owner)
end
expanded_cache
end
Expand Down
20 changes: 20 additions & 0 deletions lib/code_ownership/private/owner_assigner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# typed: strict
# frozen_string_literal: true

module CodeOwnership
module Private
class OwnerAssigner
extend T::Sig

sig { params(globs_to_owning_team_map: GlobsToOwningTeamMap).returns(GlobsToOwningTeamMap) }
def self.assign_owners(globs_to_owning_team_map)
globs_to_owning_team_map.each_with_object({}) do |(glob, owner), mapping|
mapping[glob] = owner if File.exist?(glob)
Dir.glob(glob).each do |file|
mapping[file] ||= owner
end
end
end
end
end
end
51 changes: 51 additions & 0 deletions spec/lib/code_ownership/private/owner_assigner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module CodeOwnership
RSpec.describe Private::OwnerAssigner do
describe '.assign_owners' do
subject(:assign_owners) { described_class.assign_owners(globs_to_owning_team_map) }

let(:team_1) { instance_double(CodeTeams::Team) }
let(:team_2) { instance_double(CodeTeams::Team) }

let(:globs_to_owning_team_map) do
{
'app/services/[test]/some_other_file.ts' => team_1,
'app/services/withoutbracket/file.ts' => team_2,
'app/models/*.rb' => team_2
}
end

before do
write_file('app/services/[test]/some_other_file.ts', <<~YML)
// @team Bar
YML

write_file('app/services/withoutbracket/file.ts', <<~YML)
// @team Bar
YML
end

it 'returns a hash with the same keys and the values that are files' do
expect(assign_owners).to eq(
'app/services/[test]/some_other_file.ts' => team_1,
'app/services/withoutbracket/file.ts' => team_2
)
end

context 'when glob pattern also exists' do
before do
write_file('app/services/t/some_other_file.ts', <<~YML)
// @team Bar
YML
end

it 'also matches the glob pattern' do
expect(assign_owners).to eq(
'app/services/[test]/some_other_file.ts' => team_1,
'app/services/t/some_other_file.ts' => team_1,
'app/services/withoutbracket/file.ts' => team_2
)
end
end
end
end
end

0 comments on commit 3e4e7d6

Please sign in to comment.