Add helper methods, tests for registry key path building

Broke out some of the conditional logic in the `#initialize`
method into helper methods and added tests.

Signed-off-by: Adam Leff <adam@leff.co>
This commit is contained in:
Adam Leff 2017-04-07 10:09:51 -04:00
parent 266241f173
commit a8ffe449ff
No known key found for this signature in database
GPG key ID: 7A5136DE1C1112F8
2 changed files with 36 additions and 6 deletions

View file

@ -63,18 +63,15 @@ module Inspec::Resources
@options = {}
if reg_key && reg_key.is_a?(Hash)
@options = @options.merge!(reg_key)
# generate registry_key if we do not have a regular expression
@options[:path] = @options[:hive]
# add optional key path
if @options[:key]
@options[:path] += '\\' if !@options[:key].start_with?('\\')
@options[:path] += @options[:key]
end
@options[:path] = generate_registry_key_path_from_options
@options[:name] ||= @options[:path]
else
@options[:name] = name
@options[:path] = reg_key
end
return skip_resource 'The `registry_key` resource is not supported on your OS yet.' if !inspec.os.windows?
end
@ -258,6 +255,20 @@ module Inspec::Resources
options[symbol]
end
def generate_registry_key_path_from_options
path = @options[:hive]
path += format_key_from_options
path
end
def format_key_from_options
key = @options[:key]
return '' unless key
key.start_with?('\\') ? key : "\\#{key}"
end
end
# for compatability with serverspec

View file

@ -15,4 +15,23 @@ describe 'Inspec::Resources::RegistryKey' do
resource_without_name = MockLoader.new(:windows).load_resource('registry_key', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule')
_(resource_without_name.Start).must_equal 2
end
it 'generates a proper path from options' do
resource = MockLoader.new(:windows).load_resource(
'registry_key',
'Test 1',
{ hive: 'my_hive', key: '\\my_prefixed_key'},
)
_(resource.send(:generate_registry_key_path_from_options)).must_equal 'my_hive\\my_prefixed_key'
end
it 'generates a proper path from options when the key has no leading slash' do
resource = MockLoader.new(:windows).load_resource(
'registry_key',
'Test 1',
{ hive: 'my_hive', key: 'key_with_no_slash'},
)
_(resource.send(:generate_registry_key_path_from_options)).must_equal 'my_hive\\key_with_no_slash'
end
end