diff options
author | Claire McQuin <claire@getchef.com> | 2015-09-21 09:13:11 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2015-09-21 10:19:14 -0700 |
commit | 12461128b1dc82b56568fe8ac6d5d9a8ffb0f3f2 (patch) | |
tree | eb036ed0c4d39643a4808edf8b2270417c965fc0 | |
parent | 3da8700c1c88a44488732bae47bcc5b1cb545ca2 (diff) | |
download | chef-12461128b1dc82b56568fe8ac6d5d9a8ffb0f3f2.tar.gz |
Safely clean up Win32 namespace after specs
-rw-r--r-- | spec/support/shared/context/win32.rb | 34 | ||||
-rw-r--r-- | spec/unit/platform/query_helpers_spec.rb | 15 | ||||
-rw-r--r-- | spec/unit/provider/service/windows_spec.rb | 17 | ||||
-rw-r--r-- | spec/unit/win32/registry_spec.rb | 18 |
4 files changed, 63 insertions, 21 deletions
diff --git a/spec/support/shared/context/win32.rb b/spec/support/shared/context/win32.rb new file mode 100644 index 0000000000..3dbe876114 --- /dev/null +++ b/spec/support/shared/context/win32.rb @@ -0,0 +1,34 @@ +# +# Copyright:: Copyright (c) 2015 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +RSpec.shared_context "Win32" do + before(:all) do + @original_win32 = if defined?(Win32) + win32 = Object.send(:const_get, 'Win32') + Object.send(:remove_const, 'Win32') + win32 + else + nil + end + Win32 = Module.new + end + + after(:all) do + Object.send(:remove_const, 'Win32') if defined?(Win32) + Object.send(:const_set, 'Win32', @original_win32) if @original_win32 + end +end diff --git a/spec/unit/platform/query_helpers_spec.rb b/spec/unit/platform/query_helpers_spec.rb index 7f1b3984d3..c220018d09 100644 --- a/spec/unit/platform/query_helpers_spec.rb +++ b/spec/unit/platform/query_helpers_spec.rb @@ -32,6 +32,8 @@ describe "Chef::Platform#windows_server_2003?" do end describe "Chef::Platform#windows_nano_server?" do + include_context "Win32" + let(:key) { "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Server\\ServerLevels" } let(:key_query_value) { 0x0001 } let(:access) { key_query_value | 0x0100 } @@ -39,14 +41,6 @@ describe "Chef::Platform#windows_nano_server?" do let(:registry) { double("Win32::Registry") } before(:all) do - @original_win32 = if defined?(Win32) - win32 = Object.send(:const_get, 'Win32') - Object.send(:remove_const, 'Win32') - win32 - else - nil - end - Win32 = Module.new Win32::Registry = Class.new Win32::Registry::Error = Class.new(RuntimeError) @@ -62,11 +56,6 @@ describe "Chef::Platform#windows_nano_server?" do Win32::Registry.send(:remove_const, 'KEY_QUERY_VALUE') if defined?(Win32::Registry::KEY_QUERY_VALUE) end - after(:all) do - Object.send(:remove_const, 'Win32') if defined?(Win32) - Object.send(:const_set, 'Win32', @original_win32) if @original_win32 - end - it "returns false early when not on windows" do allow(ChefConfig).to receive(:windows?).and_return(false) expect(Chef::Platform).to_not receive(:require) diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb index 784a2232b2..d27cc54ec1 100644 --- a/spec/unit/provider/service/windows_spec.rb +++ b/spec/unit/provider/service/windows_spec.rb @@ -21,6 +21,12 @@ require 'spec_helper' require 'mixlib/shellout' describe Chef::Provider::Service::Windows, "load_current_resource" do + include_context "Win32" + + before(:all) do + Win32::Service = Class.new + end + before(:each) do @node = Chef::Node.new @events = Chef::EventDispatch::Dispatcher.new @@ -28,12 +34,11 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do @new_resource = Chef::Resource::WindowsService.new("chef") @provider = Chef::Provider::Service::Windows.new(@new_resource, @run_context) @provider.current_resource = Chef::Resource::WindowsService.new("current-chef") - Object.send(:remove_const, 'Win32') if defined?(Win32) - Win32 = Module.new - Win32::Service = Class.new + Win32::Service::AUTO_START = 0x00000002 Win32::Service::DEMAND_START = 0x00000003 Win32::Service::DISABLED = 0x00000004 + allow(Win32::Service).to receive(:status).with(@new_resource.service_name).and_return( double("StatusStruct", :current_state => "running")) allow(Win32::Service).to receive(:config_info).with(@new_resource.service_name).and_return( @@ -42,6 +47,12 @@ describe Chef::Provider::Service::Windows, "load_current_resource" do allow(Win32::Service).to receive(:configure).and_return(Win32::Service) end + after(:each) do + Win32::Service.send(:remove_const, 'AUTO_START') if defined?(Win32::Service::AUTO_START) + Win32::Service.send(:remove_const, 'DEMAND_START') if defined?(Win32::Service::DEMAND_START) + Win32::Service.send(:remove_const, 'DISABLED') if defined?(Win32::Service::DISABLED) + end + it "should set the current resources service name to the new resources service name" do @provider.load_current_resource expect(@provider.current_resource.service_name).to eq('chef') diff --git a/spec/unit/win32/registry_spec.rb b/spec/unit/win32/registry_spec.rb index fdd3e85a8c..3ad26d967e 100644 --- a/spec/unit/win32/registry_spec.rb +++ b/spec/unit/win32/registry_spec.rb @@ -19,6 +19,7 @@ require 'spec_helper' describe Chef::Win32::Registry do + include_context "Win32" let(:value1) { { :name => "one", :type => :string, :data => "1" } } let(:value1_upcase_name) { {:name => "ONE", :type => :string, :data => "1"} } @@ -29,25 +30,32 @@ describe Chef::Win32::Registry do let(:sub_key) {'OpscodePrimes'} let(:missing_key_path) {'HKCU\Software'} + before(:all) do + Win32::Registry = Class.new + Win32::Registry::Error = Class.new(RuntimeError) + end + before(:each) do allow_any_instance_of(Chef::Win32::Registry).to receive(:machine_architecture).and_return(:x86_64) @registry = Chef::Win32::Registry.new() #Making the values for registry constants available on unix - Object.send(:remove_const, 'Win32') if defined?(Win32) - Win32 = Module.new - Win32::Registry = Class.new Win32::Registry::KEY_SET_VALUE = 0x0002 Win32::Registry::KEY_QUERY_VALUE = 0x0001 Win32::Registry::KEY_WRITE = 0x00020000 | 0x0002 | 0x0004 Win32::Registry::KEY_READ = 0x00020000 | 0x0001 | 0x0008 | 0x0010 - Win32::Registry::Error = Class.new(RuntimeError) - @hive_mock = double("::Win32::Registry::HKEY_CURRENT_USER") @reg_mock = double("reg") end + after(:each) do + Win32::Registry.send(:remove_const, 'KEY_SET_VALUE') if defined?(Win32::Registry::KEY_SET_VALUE) + Win32::Registry.send(:remove_const, 'KEY_QUERY_VALUE') if defined?(Win32::Registry::KEY_QUERY_VALUE) + Win32::Registry.send(:remove_const, 'KEY_READ') if defined?(Win32::Registry::KEY_READ) + Win32::Registry.send(:remove_const, 'KEY_WRITE') if defined?(Win32::Registry::KEY_WRITE) + end + describe "get_values" do it "gets all values for a key if the key exists" do expect(@registry).to receive(:get_hive_and_key).with(key_path).and_return([@hive_mock, key]) |