From 02f7b3ef2d455fff46cb6d5a0877a5193bc769c9 Mon Sep 17 00:00:00 2001 From: Julianna Date: Wed, 13 Dec 2023 01:05:04 -0600 Subject: [PATCH] fix: local overcommit file merges with existing .overcommit.yml file (#815) --- lib/overcommit/configuration_loader.rb | 6 +- spec/overcommit/configuration_loader_spec.rb | 62 ++++++++++++++------ 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/lib/overcommit/configuration_loader.rb b/lib/overcommit/configuration_loader.rb index e4063d7e..8fd68a01 100644 --- a/lib/overcommit/configuration_loader.rb +++ b/lib/overcommit/configuration_loader.rb @@ -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 + l_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(l_config) if l_config if @options.fetch(:verify) { config.verify_signatures? } verify_signatures(config) diff --git a/spec/overcommit/configuration_loader_spec.rb b/spec/overcommit/configuration_loader_spec.rb index 2ddbe4e6..90497201 100644 --- a/spec/overcommit/configuration_loader_spec.rb +++ b/spec/overcommit/configuration_loader_spec.rb @@ -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 @@ -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