Skip to content

Commit d680f8f

Browse files
sdsShane da Silva
authored and
Shane da Silva
committed
Add partial specs for Configuration
Still have to add specs for `merge` and `apply_environment!`. Change-Id: I4be442c7eae7db4cb3298277cb7049565a1ed1f6 Reviewed-on: http://gerrit.causes.com/35574 Reviewed-by: Shane da Silva <shane@causes.com> Tested-by: Shane da Silva <shane@causes.com>
1 parent 7a7e47a commit d680f8f

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

lib/overcommit/configuration.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def enabled_builtin_hooks(hook_type)
2121
end
2222

2323
# Returns a non-modifiable configuration for a hook.
24-
def hook_config(hook, hook_type = nil)
24+
def for_hook(hook, hook_type = nil)
2525
unless hook_type
2626
components = hook.class.name.split('::')
2727
hook = components.last
@@ -72,7 +72,7 @@ def hook_exists?(hook_type, hook_name)
7272
# possible.
7373
def validate
7474
@hash = convert_nils_to_empty_hashes(@hash)
75-
ensure_special_all_section_exists(@hash)
75+
ensure_hook_type_sections_exist(@hash)
7676
end
7777

7878
def smart_merge(parent, child)
@@ -88,10 +88,9 @@ def smart_merge(parent, child)
8888
end
8989
end
9090

91-
def ensure_special_all_section_exists(hash)
92-
hook_types = Dir[File.join(OVERCOMMIT_HOME, 'lib/overcommit/hook/*')].
93-
select { |path| File.directory?(path) }.
94-
map { |path| File.basename(path) }
91+
def ensure_hook_type_sections_exist(hash)
92+
hook_types = Overcommit::Utils.supported_hook_types.
93+
map { |type| type.gsub('-', '_') }
9594

9695
hook_types.each do |hook_type|
9796
hash[hook_type] ||= {}

lib/overcommit/hook/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Base
88
def_delegators :@context, :modified_files
99

1010
def initialize(config, context)
11-
@config = config.hook_config(self)
11+
@config = config.for_hook(self)
1212
@context = context
1313
end
1414

lib/overcommit/utils.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def underscorize(str)
3030
# Returns a list of supported hook types (pre-commit, commit-msg, etc.)
3131
def supported_hook_types
3232
Dir[File.join(OVERCOMMIT_HOME, 'lib', 'overcommit', 'hook', '*')].
33-
map { |file| File.basename(file, '.rb').gsub('_', '-') }.
34-
reject { |file| file == 'base' }
33+
select { |file| File.directory?(file) }.
34+
map { |file| File.basename(file, '.rb').gsub('_', '-') }
3535
end
3636

3737
# Returns whether a command can be found given the current environment path.

spec/overcommit/configuration_spec.rb

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Configuration do
4+
let(:hash) { {} }
5+
let(:config) { described_class.new(hash) }
6+
7+
before do
8+
Overcommit::Utils.instance_variable_set(:@repo_root, nil)
9+
end
10+
11+
describe '#new' do
12+
let(:internal_hash) { config.instance_variable_get(:@hash) }
13+
subject { config }
14+
15+
context 'when no configuration exists for a hook type' do
16+
it 'creates sections for those hook types' do
17+
internal_hash.should have_key 'pre_commit'
18+
end
19+
20+
it 'creates the special ALL section for the hook type' do
21+
internal_hash['pre_commit'].should have_key 'ALL'
22+
end
23+
end
24+
25+
context 'when keys with empty values exist' do
26+
let(:hash) do
27+
{
28+
'pre_commit' => {
29+
'SomeHook' => nil
30+
},
31+
}
32+
end
33+
34+
it 'converts the values to empty hashes' do
35+
internal_hash['pre_commit']['SomeHook'].should == {}
36+
end
37+
end
38+
end
39+
40+
describe '#plugin_directory' do
41+
let(:hash) { { 'plugin_directory' => 'some-directory' } }
42+
subject { config.plugin_directory }
43+
44+
around do |example|
45+
repo do
46+
example.run
47+
end
48+
end
49+
50+
it { should == File.expand_path('some-directory') }
51+
end
52+
53+
describe '#enabled_builtin_hooks' do
54+
let(:hash) do
55+
{
56+
'pre_commit' => {
57+
'SomeHook' => nil,
58+
'SomeOtherHook' => {
59+
'enabled' => false
60+
},
61+
}
62+
}
63+
end
64+
65+
subject { config.enabled_builtin_hooks('pre_commit') }
66+
67+
it 'includes hooks that are not disabled' do
68+
subject.should == ['SomeHook']
69+
end
70+
end
71+
72+
describe '#for_hook' do
73+
let(:hash) do
74+
{
75+
'pre_commit' => {
76+
'ALL' => {
77+
'required' => false,
78+
},
79+
'SomeHook' => {
80+
'enabled' => true,
81+
'quiet' => false,
82+
}
83+
}
84+
}
85+
end
86+
87+
subject { config.for_hook('SomeHook', 'pre_commit') }
88+
89+
it 'returns the subset of the config for the specified hook' do
90+
subject['enabled'].should be_true
91+
subject['quiet'].should be_false
92+
end
93+
94+
it 'merges the the hook config with the ALL section' do
95+
subject['required'].should be_false
96+
end
97+
end
98+
end

0 commit comments

Comments
 (0)