Skip to content

Commit c45ae64

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

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed

lib/overcommit/configuration.rb

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def apply_environment!(hook_type, env)
5555
@hash[hook_type]['ALL']['skip'] = true
5656
else
5757
skipped_hooks.select { |hook_name| hook_exists?(hook_type, hook_name) }.
58+
map { |hook_name| Overcommit::Utils.camel_case(hook_name) }.
5859
each do |hook_name|
5960
@hash[hook_type][hook_name] ||= {}
6061
@hash[hook_type][hook_name]['skip'] = true
@@ -69,6 +70,8 @@ def apply_environment!(hook_type, env)
6970
private
7071

7172
def hook_exists?(hook_type, hook_name)
73+
hook_name = Overcommit::Utils.underscorize(hook_name)
74+
7275
File.exist?(File.join(OVERCOMMIT_HOME, 'lib', 'overcommit', 'hook',
7376
hook_type, "#{hook_name}.rb"))
7477
end

lib/overcommit/utils.rb

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def underscorize(str)
2727
downcase
2828
end
2929

30+
# Converts a string containing underscores/hyphens/spaces into CamelCase.
31+
def camel_case(str)
32+
str.split(/_|-| /).map { |part| part.sub(/^\w/) { |c| c.upcase } }.join
33+
end
34+
3035
# Returns a list of supported hook types (pre-commit, commit-msg, etc.)
3136
def supported_hook_types
3237
Dir[File.join(OVERCOMMIT_HOME, 'lib', 'overcommit', 'hook', '*')].

spec/configuration_spec.rb

-11
This file was deleted.

spec/overcommit/configuration_spec.rb

+67
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,71 @@
173173
end
174174
end
175175
end
176+
177+
describe '#apply_environment!' do
178+
let(:hash) { {} }
179+
let(:config) { described_class.new(hash) }
180+
let!(:old_config) { described_class.new(hash.dup) }
181+
subject { config }
182+
183+
before do
184+
config.apply_environment!('pre-commit', env)
185+
end
186+
187+
context 'when no hooks are requested to be skipped' do
188+
let(:env) { {} }
189+
190+
it 'does nothing to the configuration' do
191+
subject.should == old_config
192+
end
193+
end
194+
195+
context 'when a non-existent hook is requested to be skipped' do
196+
let(:env) { { 'SKIP' => 'SomeMadeUpHook' } }
197+
198+
it 'does nothing to the configuration' do
199+
subject.should == old_config
200+
end
201+
end
202+
203+
context 'when an existing hook is requested to be skipped' do
204+
let(:env) { { 'SKIP' => 'AuthorName' } }
205+
206+
it 'sets the skip option of the hook to true' do
207+
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
208+
end
209+
210+
context 'and the hook is spelt with underscores' do
211+
let(:env) { { 'SKIP' => 'author_name' } }
212+
213+
it 'sets the skip option of the hook to true' do
214+
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
215+
end
216+
end
217+
218+
context 'and the hook is spelt with hyphens' do
219+
let(:env) { { 'SKIP' => 'author-name' } }
220+
221+
it 'sets the skip option of the hook to true' do
222+
subject.for_hook('AuthorName', 'pre_commit')['skip'].should be_true
223+
end
224+
end
225+
end
226+
227+
context 'when the word "all" is included in the skip list' do
228+
let(:env) { { 'SKIP' => 'all' } }
229+
230+
it 'sets the skip option of the ALL section to true' do
231+
subject.for_hook('ALL', 'pre_commit')['skip'].should be_true
232+
end
233+
234+
context 'and "all" is capitalized' do
235+
let(:env) { { 'SKIP' => 'ALL' } }
236+
237+
it 'sets the skip option of the special ALL config to true' do
238+
subject.for_hook('ALL', 'pre_commit')['skip'].should be_true
239+
end
240+
end
241+
end
242+
end
176243
end

0 commit comments

Comments
 (0)