Skip to content

Commit

Permalink
fix: local overcommit file merges with existing .overcommit.yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
julesros committed Sep 24, 2023
1 parent 90e3679 commit c97ec56
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
6 changes: 2 additions & 4 deletions lib/overcommit/configuration_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ def load_repo_config
# Loads a configuration, ensuring it extends the default configuration.
def load_file(file, local_file = nil)
overcommit_config = self.class.load_from_file(file, default: false, logger: @log)
if local_file
local_config = self.class.load_from_file(local_file, default: false, logger: @log)
end
local_config = self.class.load_from_file(local_file, default: false, logger: @log) if local_file
config = self.class.default_configuration.merge(overcommit_config)
config = self.class.default_configuration.merge(local_config) if local_config
config = config.merge(local_config) if local_config

if @options.fetch(:verify) { config.verify_signatures? }
verify_signatures(config)
Expand Down
62 changes: 43 additions & 19 deletions spec/overcommit/configuration_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,32 @@
end
end

context 'when repo contains a local configuration file' do
context 'when repo only contains a repo level configuration file' do
let(:config_contents) { <<-CFG }
plugin_directory: 'some-directory'
PreCommit:
Rubocop:
enabled: true
CFG

around do |example|
repo do
File.open('.overcommit.yml', 'w') { |f| f.write(config_contents) }
example.run
end
end

it 'includes default settings' do
subject
subject.for_hook('CapitalizedSubject', 'CommitMsg').should include('enabled' => true)
end

it 'includes .overwrite.yml configs' do
subject
subject.for_hook('Rubocop', 'PreCommit').should include('enabled' => true)
end
end

context 'when repo also contains a local configuration file' do
let(:local_config_contents) { <<-CFG }
plugin_directory: 'some-different-directory'
CFG
Expand All @@ -75,28 +96,31 @@
end
end

it 'loads the file' do
Overcommit::ConfigurationLoader.any_instance.
should_receive(:load_file).
with(File.expand_path('.overcommit.yml'), File.expand_path('.local-overcommit.yml'))
let(:config_contents) { <<-CFG }
PreCommit:
ScssLint:
enabled: true
CFG

let(:local_config_contents) { <<-CFG }
PreCommit:
Rubocop:
enabled: true
CFG

it 'includes default settings' do
subject
subject.for_hook('CapitalizedSubject', 'CommitMsg').should include('enabled' => true)
end

it 'merges each loaded file with the default configuration' do
subject.plugin_directory.should == File.expand_path('some-different-directory')
it 'includes .overwrite.yml configs' do
subject
subject.for_hook('ScssLint', 'PreCommit').should include('enabled' => true)
end

context 'and the configuration file contains a hook with no `enabled` option' do
let(:config_contents) { <<-CFG }
PreCommit:
ScssLint:
command: ['bundle', 'exec', 'scss-lint']
CFG

it 'displays a warning' do
subject
output.string.should =~ /PreCommit::ScssLint.*not.*enabled/i
end
it 'includes .local-overwrite.yml configs' do
subject
subject.for_hook('Rubocop', 'PreCommit').should include('enabled' => true)
end
end
end
Expand Down

0 comments on commit c97ec56

Please sign in to comment.