diff --git a/ext/code_ownership/src/lib.rs b/ext/code_ownership/src/lib.rs index 95e160b..da617ad 100644 --- a/ext/code_ownership/src/lib.rs +++ b/ext/code_ownership/src/lib.rs @@ -85,10 +85,10 @@ fn validate(ruby: &Ruby, files: Option>) -> Result { validate_result(ruby, &run_result) } -fn generate_and_validate(ruby: &Ruby, files: Option>, skip_stage: bool) -> Result { +fn generate_and_validate(ruby: &Ruby, files: Option>, git_stage: bool) -> Result { let run_config = build_run_config(); let files_vec = files.unwrap_or_default(); - let run_result = runner::generate_and_validate(&run_config, files_vec, skip_stage); + let run_result = runner::generate_and_validate(&run_config, files_vec, git_stage); validate_result(ruby, &run_result) } diff --git a/lib/code_ownership.rb b/lib/code_ownership.rb index 80bc414..31b4614 100644 --- a/lib/code_ownership.rb +++ b/lib/code_ownership.rb @@ -232,7 +232,7 @@ def self.validate!( files: nil ) if autocorrect - ::RustCodeOwners.generate_and_validate(files, !stage_changes) + ::RustCodeOwners.generate_and_validate(files, stage_changes) else ::RustCodeOwners.validate(files) end diff --git a/lib/code_ownership/version.rb b/lib/code_ownership/version.rb index 667e740..4deded7 100644 --- a/lib/code_ownership/version.rb +++ b/lib/code_ownership/version.rb @@ -2,5 +2,5 @@ # frozen_string_literal: true module CodeOwnership - VERSION = '2.2.0' + VERSION = '2.2.1' end diff --git a/sorbet/rbi/manual.rbi b/sorbet/rbi/manual.rbi index feba3a1..6fe4e54 100644 --- a/sorbet/rbi/manual.rbi +++ b/sorbet/rbi/manual.rbi @@ -9,7 +9,7 @@ module RustCodeOwners def for_file(file) end - def generate_and_validate(files, skip_stage) + def generate_and_validate(files, git_stage) end def validate(files) diff --git a/spec/lib/code_ownership_spec.rb b/spec/lib/code_ownership_spec.rb index f1b46d7..ed2b5e4 100644 --- a/spec/lib/code_ownership_spec.rb +++ b/spec/lib/code_ownership_spec.rb @@ -620,6 +620,53 @@ expect { CodeOwnership.validate!(autocorrect: false) }.not_to raise_error end end + + context 'when the project is a git repository' do + let(:staged_files) { `git diff --cached --name-only`.lines(chomp: true) } + + before do + create_non_empty_application + system('git', 'init', '--quiet', exception: true) + end + + it 'stages the CODEOWNERS file by default' do + CodeOwnership.validate! + expect(staged_files).to eq ['.github/CODEOWNERS'] + end + + it 'does not stage the CODEOWNERS file when stage_changes is false' do + CodeOwnership.validate!(stage_changes: false) + expect(codeowners_path).to exist + expect(staged_files).to be_empty + end + end + + ['', "# no keys yet\n", "---\n"].each do |contents| + context "when a package.yml is #{contents.inspect}" do + before do + write_configuration(owned_globs: ['packs/**/*.rb']) + write_file('config/teams/bar.yml', <<~CONTENTS) + name: Bar + github: + team: '@MyOrg/bar-team' + CONTENTS + write_file('packs/empty_pack/package.yml', contents) + write_file('packs/empty_pack/owned_file.rb', "# @team Bar\nclass OwnedFile; end\n") + end + + it 'passes validation' do + expect { CodeOwnership.validate!(stage_changes: false) }.not_to raise_error + end + + it 'leaves ownership to the annotation without printing a parse error' do + owner = nil + # for_file falls back to the annotation when package.yml fails to parse, and only prints the error. + expect { owner = CodeOwnership.for_file('packs/empty_pack/owned_file.rb', from_codeowners: false) } + .not_to output.to_stderr_from_any_process + expect(owner).to eq CodeTeams.find('Bar') + end + end + end end describe '.bust_caches!' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 56e2b39..52ae89b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -21,6 +21,12 @@ config.include_context 'application fixtures' + # Hooks and linked worktrees export variables such as GIT_DIR and GIT_INDEX_FILE, which would aim git, including + # the extension's git add in validate!, at the repository running the suite instead of each example's temp project. + config.before(:suite) do + `git rev-parse --local-env-vars`.split.each { |key| ENV.delete(key) } + end + config.before do |c| unless c.metadata[:do_not_bust_cache] CodeOwnership.bust_caches!