From 33f3d712420e766928abf9073084a91275629d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eva=20Kadlecova=CC=81?= Date: Fri, 22 Feb 2019 10:29:51 +0100 Subject: [PATCH] Add various paths for CODEOWNERS file --- lib/codeowners/checker.rb | 12 ++++-- spec/codeowners/checker/group/line_spec.rb | 5 +-- spec/codeowners/checker_spec.rb | 45 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/lib/codeowners/checker.rb b/lib/codeowners/checker.rb index 178a46b..d6c063f 100644 --- a/lib/codeowners/checker.rb +++ b/lib/codeowners/checker.rb @@ -107,13 +107,17 @@ def main_group codeowners.main_group end - def codeowners_filename - File.join(@repo_dir, '.github', 'CODEOWNERS') - end - def commit_changes! @git.add(codeowners_filename) @git.commit('Fix pattern :robot:') end + + private + + def codeowners_filename + directories = ['', '.github', 'docs', '.gitlab'] + paths = directories.map { |dir| File.join(@repo_dir, dir, 'CODEOWNERS') } + Dir.glob(paths).first || paths.first + end end end diff --git a/spec/codeowners/checker/group/line_spec.rb b/spec/codeowners/checker/group/line_spec.rb index 50b331a..8421a33 100644 --- a/spec/codeowners/checker/group/line_spec.rb +++ b/spec/codeowners/checker/group/line_spec.rb @@ -28,9 +28,8 @@ describe '.subclasses' do let(:line_subclasses) do - ObjectSpace.each_object(::Class).select { |klass| klass < described_class } - [ - Codeowners::Checker::Group::UnrecognizedLine - ] + ObjectSpace.each_object(::Class).select { |klass| klass < described_class } - + [Codeowners::Checker::Group::UnrecognizedLine] end it 'includes all subclasses except of unrecognized line' do diff --git a/spec/codeowners/checker_spec.rb b/spec/codeowners/checker_spec.rb index 581c0e8..20768aa 100644 --- a/spec/codeowners/checker_spec.rb +++ b/spec/codeowners/checker_spec.rb @@ -227,4 +227,49 @@ def setup_git_for_project end end end + + describe '#codeowners' do + subject { described_class.new(folder_name, from, to) } + + context 'when the file is in the .github folder' do + it 'returns the content of the codeowners file' do + expect(subject.codeowners).to be_a(Codeowners::Checker::CodeOwners) + expect(subject.codeowners.to_content).to eq( + ['# comment ignored', '.rubocop.yml @owner', 'Gemfile @owner1', 'lib/billing/* @owner2', + 'lib/shared/* @owner2 @owner1'] + ) + end + end + + context 'when the file is in the root folder' do + before { FileUtils.mv('project/.github/CODEOWNERS', 'project/CODEOWNERS') } + + it 'returns the content of the codeowners file' do + expect(subject.codeowners.to_content).to eq( + ['# comment ignored', '.rubocop.yml @owner', 'Gemfile @owner1', 'lib/billing/* @owner2', + 'lib/shared/* @owner2 @owner1'] + ) + end + end + + context 'when the file does not exist' do + before { FileUtils.rm_f('project/.github/CODEOWNERS') } + + it 'uses a root folder for the file and returns an empty array for the content' do + expect(subject.codeowners.to_content).to eq([]) + end + end + end + + describe '#main_group' do + subject { described_class.new(folder_name, from, to) } + + it 'returns an array containing the main group' do + expect(subject.main_group.to_content).to eq( + ['# comment ignored', '.rubocop.yml @owner', 'Gemfile @owner1', 'lib/billing/* @owner2', + 'lib/shared/* @owner2 @owner1'] + ) + expect(subject.main_group).to be_a(Codeowners::Checker::Group) + end + end end