Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/code_ownership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ fn validate(ruby: &Ruby, files: Option<Vec<String>>) -> Result<Value, Error> {
validate_result(ruby, &run_result)
}

fn generate_and_validate(ruby: &Ruby, files: Option<Vec<String>>, skip_stage: bool) -> Result<Value, Error> {
fn generate_and_validate(ruby: &Ruby, files: Option<Vec<String>>, git_stage: bool) -> Result<Value, Error> {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/code_ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/code_ownership/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# frozen_string_literal: true

module CodeOwnership
VERSION = '2.2.0'
VERSION = '2.2.1'
end
2 changes: 1 addition & 1 deletion sorbet/rbi/manual.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/code_ownership_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
Loading