mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
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:
parent
266241f173
commit
a8ffe449ff
2 changed files with 36 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue