diff options
author | Claire McQuin <claire@chef.io> | 2015-09-03 11:15:27 -0700 |
---|---|---|
committer | Claire McQuin <claire@chef.io> | 2015-09-03 11:18:27 -0700 |
commit | 25b77f4e175e8a56865d3c908f29147c6d41c93a (patch) | |
tree | 13042cbc3c87bda943edd5dda0939cf1d1183d07 | |
parent | c00a3bdc8e00e512f4c3cbb2ff7169ff20a66672 (diff) | |
download | chef-25b77f4e175e8a56865d3c908f29147c6d41c93a.tar.gz |
Implement delete_key with Win32::Registry#delete_key
-rw-r--r-- | lib/chef/win32/registry.rb | 39 | ||||
-rw-r--r-- | spec/unit/registry_helper_spec.rb | 15 |
2 files changed, 19 insertions, 35 deletions
diff --git a/lib/chef/win32/registry.rb b/lib/chef/win32/registry.rb index e699a9bc06..12ad08a965 100644 --- a/lib/chef/win32/registry.rb +++ b/lib/chef/win32/registry.rb @@ -127,36 +127,23 @@ class Chef Chef::Log.debug("Registry key #{key_path}, does not exist, not deleting") return true end - #key_path is in the form "HKLM\Software\Opscode" for example, extracting - #hive = HKLM, - #hive_namespace = ::Win32::Registry::HKEY_LOCAL_MACHINE - hive = key_path.split("\\").shift - hive_namespace, key_including_parent = get_hive_and_key(key_path) - if has_subkeys?(key_path) - if recursive == true - subkeys = get_subkeys(key_path) - subkeys.each do |key| - keypath_to_check = hive+"\\"+key_including_parent+"\\"+key - Chef::Log.debug("Deleting registry key #{key_path} recursively") - delete_key(keypath_to_check, true) - end - delete_key_ex(hive_namespace, key_including_parent) - else - raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified" - end - else - delete_key_ex(hive_namespace, key_including_parent) - return true + if has_subkeys?(key_path) && !recursive + raise Chef::Exceptions::Win32RegNoRecursive, "Registry key #{key_path} has subkeys, and recursive not specified" + end + hive, key_including_parent = get_hive_and_key(key_path) + # key_including_parent: Software\\Root\\Branch\\Fruit + # key => Fruit + # key_parent => Software\\Root\\Branch + key_parts = key_including_parent.split("\\") + key = key_parts.pop + key_parent = key_parts.join("\\") + hive.open(key_parent, ::Win32::Registry::KEY_WRITE | registry_system_architecture) do |reg| + reg.delete_key(key, recursive) end + Chef::Log.debug("Registry key #{key_path} deleted") true end - #Using the 'RegDeleteKeyEx' Windows API that correctly supports WOW64 systems (Win2003) - #instead of the 'RegDeleteKey' - def delete_key_ex(hive, key) - RegDeleteKeyExW(hive.hkey, wstring(key), 0, 0) == 0 - end - def key_exists?(key_path) hive, key = get_hive_and_key(key_path) begin diff --git a/spec/unit/registry_helper_spec.rb b/spec/unit/registry_helper_spec.rb index b2d0b7b125..a30608add7 100644 --- a/spec/unit/registry_helper_spec.rb +++ b/spec/unit/registry_helper_spec.rb @@ -176,28 +176,25 @@ describe Chef::Provider::RegistryKey do describe "delete_key", :windows_only do it "deletes key if it has subkeys and recursive is set to true" do expect(@registry).to receive(:key_exists?).with(key_path).and_return(true) - expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) expect(@registry).to receive(:has_subkeys?).with(key_path).and_return(true) - expect(@registry).to receive(:get_subkeys).with(key_path).and_return([sub_key]) - expect(@registry).to receive(:key_exists?).with(key_path+"\\"+sub_key).and_return(true) - expect(@registry).to receive(:get_hive_and_key).with(key_path+"\\"+sub_key).and_return([@hive_mock, key+"\\"+sub_key]) - expect(@registry).to receive(:has_subkeys?).with(key_path+"\\"+sub_key).and_return(false) - expect(@registry).to receive(:delete_key_ex).twice + expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + expect(@hive_mock).to receive(:open).with(key_parent, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture).and_yield(@reg_mock) + expect(@reg_mock).to receive(:delete_key).with(key_to_delete, true).and_return(true) @registry.delete_key(key_path, true) end it "raises an exception if it has subkeys but recursive is set to false" do expect(@registry).to receive(:key_exists?).with(key_path).and_return(true) - expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) expect(@registry).to receive(:has_subkeys?).with(key_path).and_return(true) expect{@registry.delete_key(key_path, false)}.to raise_error(Chef::Exceptions::Win32RegNoRecursive) end it "deletes key if the key exists and has no subkeys" do expect(@registry).to receive(:key_exists?).with(key_path).and_return(true) - expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) expect(@registry).to receive(:has_subkeys?).with(key_path).and_return(false) - expect(@registry).to receive(:delete_key_ex) + expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) + expect(@hive_mock).to receive(:open).with(key_parent, ::Win32::Registry::KEY_WRITE | @registry.registry_system_architecture).and_yield(@reg_mock) + expect(@reg_mock).to receive(:delete_key).with(key_to_delete, true).and_return(true) @registry.delete_key(key_path, true) end end |