Skip to content

Commit 02f7b3e

Browse files
authored
fix: local overcommit file merges with existing .overcommit.yml file (#815)
1 parent 90e3679 commit 02f7b3e

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

lib/overcommit/configuration_loader.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ def load_repo_config
7070
# Loads a configuration, ensuring it extends the default configuration.
7171
def load_file(file, local_file = nil)
7272
overcommit_config = self.class.load_from_file(file, default: false, logger: @log)
73-
if local_file
74-
local_config = self.class.load_from_file(local_file, default: false, logger: @log)
75-
end
73+
l_config = self.class.load_from_file(local_file, default: false, logger: @log) if local_file
7674
config = self.class.default_configuration.merge(overcommit_config)
77-
config = self.class.default_configuration.merge(local_config) if local_config
75+
config = config.merge(l_config) if l_config
7876

7977
if @options.fetch(:verify) { config.verify_signatures? }
8078
verify_signatures(config)

spec/overcommit/configuration_loader_spec.rb

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,32 @@
5858
end
5959
end
6060

61-
context 'when repo contains a local configuration file' do
61+
context 'when repo only contains a repo level configuration file' do
6262
let(:config_contents) { <<-CFG }
63-
plugin_directory: 'some-directory'
63+
PreCommit:
64+
Rubocop:
65+
enabled: true
6466
CFG
6567

68+
around do |example|
69+
repo do
70+
File.open('.overcommit.yml', 'w') { |f| f.write(config_contents) }
71+
example.run
72+
end
73+
end
74+
75+
it 'includes default settings' do
76+
subject
77+
subject.for_hook('CapitalizedSubject', 'CommitMsg').should include('enabled' => true)
78+
end
79+
80+
it 'includes .overwrite.yml configs' do
81+
subject
82+
subject.for_hook('Rubocop', 'PreCommit').should include('enabled' => true)
83+
end
84+
end
85+
86+
context 'when repo also contains a local configuration file' do
6687
let(:local_config_contents) { <<-CFG }
6788
plugin_directory: 'some-different-directory'
6889
CFG
@@ -75,28 +96,31 @@
7596
end
7697
end
7798

78-
it 'loads the file' do
79-
Overcommit::ConfigurationLoader.any_instance.
80-
should_receive(:load_file).
81-
with(File.expand_path('.overcommit.yml'), File.expand_path('.local-overcommit.yml'))
99+
let(:config_contents) { <<-CFG }
100+
PreCommit:
101+
ScssLint:
102+
enabled: true
103+
CFG
104+
105+
let(:local_config_contents) { <<-CFG }
106+
PreCommit:
107+
Rubocop:
108+
enabled: true
109+
CFG
110+
111+
it 'includes default settings' do
82112
subject
113+
subject.for_hook('CapitalizedSubject', 'CommitMsg').should include('enabled' => true)
83114
end
84115

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

89-
context 'and the configuration file contains a hook with no `enabled` option' do
90-
let(:config_contents) { <<-CFG }
91-
PreCommit:
92-
ScssLint:
93-
command: ['bundle', 'exec', 'scss-lint']
94-
CFG
95-
96-
it 'displays a warning' do
97-
subject
98-
output.string.should =~ /PreCommit::ScssLint.*not.*enabled/i
99-
end
121+
it 'includes .local-overwrite.yml configs' do
122+
subject
123+
subject.for_hook('Rubocop', 'PreCommit').should include('enabled' => true)
100124
end
101125
end
102126
end

0 commit comments

Comments
 (0)