From 3e4e7d6956f037e7cf7217c24d5482991a98a126 Mon Sep 17 00:00:00 2001 From: Perry Hertler Date: Fri, 16 Feb 2024 08:12:03 -0600 Subject: [PATCH] extracting OwnerAssigner for testing --- lib/code_ownership/private.rb | 2 +- lib/code_ownership/private/glob_cache.rb | 9 +--- lib/code_ownership/private/owner_assigner.rb | 20 ++++++++ .../private/owner_assigner_spec.rb | 51 +++++++++++++++++++ 4 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 lib/code_ownership/private/owner_assigner.rb create mode 100644 spec/lib/code_ownership/private/owner_assigner_spec.rb diff --git a/lib/code_ownership/private.rb b/lib/code_ownership/private.rb index 0963216..63c933a 100644 --- a/lib/code_ownership/private.rb +++ b/lib/code_ownership/private.rb @@ -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' @@ -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 diff --git a/lib/code_ownership/private/glob_cache.rb b/lib/code_ownership/private/glob_cache.rb index b29aa60..53e29c2 100644 --- a/lib/code_ownership/private/glob_cache.rb +++ b/lib/code_ownership/private/glob_cache.rb @@ -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 diff --git a/lib/code_ownership/private/owner_assigner.rb b/lib/code_ownership/private/owner_assigner.rb new file mode 100644 index 0000000..d6d9807 --- /dev/null +++ b/lib/code_ownership/private/owner_assigner.rb @@ -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 diff --git a/spec/lib/code_ownership/private/owner_assigner_spec.rb b/spec/lib/code_ownership/private/owner_assigner_spec.rb new file mode 100644 index 0000000..52a47f2 --- /dev/null +++ b/spec/lib/code_ownership/private/owner_assigner_spec.rb @@ -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