Skip to content

Commit 38a9702

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Add specs for Configuration#merge
Change-Id: I8354549cede40792f8c1af3323be28197387f184 Reviewed-on: http://gerrit.causes.com/35575 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent d680f8f commit 38a9702

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

lib/overcommit/configuration.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def initialize(hash)
77
validate
88
end
99

10+
def ==(other)
11+
super || @hash == other.hash
12+
end
13+
alias :eql? :==
14+
1015
# Returns absolute path to the directory that external hook plugins should
1116
# be loaded from.
1217
def plugin_directory
@@ -79,7 +84,7 @@ def smart_merge(parent, child)
7984
parent.merge(child) do |key, old, new|
8085
case old
8186
when Array
82-
old + new
87+
old + Array(new)
8388
when Hash
8489
smart_merge(old, new)
8590
else

spec/overcommit/configuration_spec.rb

+78
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,82 @@
9595
subject['required'].should be_false
9696
end
9797
end
98+
99+
describe '#merge' do
100+
let(:parent_config) { described_class.new(parent) }
101+
let(:child_config) { described_class.new(child) }
102+
subject { parent_config.merge(child_config) }
103+
104+
context 'when parent and child are empty' do
105+
let(:parent) { {} }
106+
let(:child) { {} }
107+
108+
it 'returns a config equivalent to both' do
109+
subject.should == parent_config
110+
subject.should == child_config
111+
end
112+
end
113+
114+
context 'when parent and child are the same' do
115+
let(:parent) { child }
116+
117+
let(:child) do
118+
{
119+
'plugin_directory' => 'some-directory',
120+
'pre-commit' => {
121+
'SomeHook' => {
122+
'enabled' => false,
123+
}
124+
},
125+
}
126+
end
127+
128+
it 'returns a config equivalent to both' do
129+
subject.should == parent_config
130+
subject.should == child_config
131+
end
132+
end
133+
134+
context 'when parent item contains a hash' do
135+
let(:parent) { { 'pre_commit' => { 'SomeHook' => { 'some-value' => 1 } } } }
136+
137+
context 'and child item contains a different hash under the same key' do
138+
let(:child) { { 'pre_commit' => { 'SomeOtherHook' => { 'something' => 2 } } } }
139+
140+
it 'merges the hashes together' do
141+
subject.for_hook('SomeHook', 'pre_commit').should == { 'some-value' => 1 }
142+
subject.for_hook('SomeOtherHook', 'pre_commit').should == { 'something' => 2 }
143+
end
144+
end
145+
146+
context 'and child item contains a hash under a different key' do
147+
let(:child) { { 'commit_msg' => { 'SomeHook' => { 'some-value' => 2 } } } }
148+
149+
it 'appends the item to the parent array' do
150+
subject.for_hook('SomeHook', 'pre_commit').should == { 'some-value' => 1 }
151+
subject.for_hook('SomeHook', 'commit_msg').should == { 'some-value' => 2 }
152+
end
153+
end
154+
end
155+
156+
context 'when parent item contains an array' do
157+
let(:parent) { { 'pre_commit' => { 'SomeHook' => { 'list' => [1, 2, 3] } } } }
158+
159+
context 'and child item contains an array' do
160+
let(:child) { { 'pre_commit' => { 'SomeHook' => { 'list' => [4, 5] } } } }
161+
162+
it 'concatenates the arrays together' do
163+
subject.for_hook('SomeHook', 'pre_commit')['list'] == [1, 2, 3, 4, 5]
164+
end
165+
end
166+
167+
context 'and child item contains a single item' do
168+
let(:child) { { 'pre_commit' => { 'SomeHook' => { 'list' => 4 } } } }
169+
170+
it 'appends the item to the parent array' do
171+
subject.for_hook('SomeHook', 'pre_commit')['list'] == [1, 2, 3, 4]
172+
end
173+
end
174+
end
175+
end
98176
end

0 commit comments

Comments
 (0)