mirror of
https://github.com/inspec/inspec
synced 2025-02-17 06:28:40 +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 = {}
|
@options = {}
|
||||||
if reg_key && reg_key.is_a?(Hash)
|
if reg_key && reg_key.is_a?(Hash)
|
||||||
@options = @options.merge!(reg_key)
|
@options = @options.merge!(reg_key)
|
||||||
|
|
||||||
# generate registry_key if we do not have a regular expression
|
# generate registry_key if we do not have a regular expression
|
||||||
@options[:path] = @options[:hive]
|
@options[:path] = generate_registry_key_path_from_options
|
||||||
# add optional key path
|
|
||||||
if @options[:key]
|
|
||||||
@options[:path] += '\\' if !@options[:key].start_with?('\\')
|
|
||||||
@options[:path] += @options[:key]
|
|
||||||
end
|
|
||||||
@options[:name] ||= @options[:path]
|
@options[:name] ||= @options[:path]
|
||||||
else
|
else
|
||||||
@options[:name] = name
|
@options[:name] = name
|
||||||
@options[:path] = reg_key
|
@options[:path] = reg_key
|
||||||
end
|
end
|
||||||
|
|
||||||
return skip_resource 'The `registry_key` resource is not supported on your OS yet.' if !inspec.os.windows?
|
return skip_resource 'The `registry_key` resource is not supported on your OS yet.' if !inspec.os.windows?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -258,6 +255,20 @@ module Inspec::Resources
|
||||||
|
|
||||||
options[symbol]
|
options[symbol]
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# for compatability with serverspec
|
# 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 = MockLoader.new(:windows).load_resource('registry_key', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule')
|
||||||
_(resource_without_name.Start).must_equal 2
|
_(resource_without_name.Start).must_equal 2
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue