From 5d3aef162c43e6239b6d8f5ef8a2045bd10632bc Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 3 Feb 2015 12:58:19 -0800 Subject: fix LWRP constant lookups * providers had the same bug as CHEF-4117 on resources * removed the strict_const_defined method on Chef::Resource since ruby 1.8.7 deprecation made that method entirely trivial * added tests, verified the failure cases really work * todo added since i think we're leaking state in-between tests --- lib/chef/provider/lwrp_base.rb | 2 +- lib/chef/resource.rb | 4 ---- lib/chef/resource/lwrp_base.rb | 2 +- spec/unit/lwrp_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/chef/provider/lwrp_base.rb b/lib/chef/provider/lwrp_base.rb index 121abf5fdb..492ddda6da 100644 --- a/lib/chef/provider/lwrp_base.rb +++ b/lib/chef/provider/lwrp_base.rb @@ -86,7 +86,7 @@ class Chef class_name = convert_to_class_name(provider_name) - if Chef::Provider.const_defined?(class_name) + if Chef::Provider.const_defined?(class_name, false) Chef::Log.info("#{class_name} light-weight provider is already initialized -- Skipping loading #{filename}!") Chef::Log.debug("Overriding already defined LWRPs is not supported anymore starting with Chef 12.") provider_class = Chef::Provider.const_get(class_name) diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 3e9d119cee..5a6f3ec037 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -931,10 +931,6 @@ class Chef run_context.notifies_delayed(Notification.new(resource_spec, action, self)) end - def self.strict_const_defined?(const) - const_defined?(const, false) - end - class << self # back-compat # NOTE: that we do not support unregistering classes as descendents like diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index 0a1253780c..ce72e98028 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -39,7 +39,7 @@ class Chef rname = filename_to_qualified_string(cookbook_name, filename) class_name = convert_to_class_name(rname) - if Resource.strict_const_defined?(class_name) + if Resource.const_defined?(class_name, false) Chef::Log.info("#{class_name} light-weight resource is already initialized -- Skipping loading #{filename}!") Chef::Log.debug("Overriding already defined LWRPs is not supported anymore starting with Chef 12.") resource_class = Resource.const_get(class_name) diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index 35963dec64..64a2176e76 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -36,6 +36,30 @@ describe "LWRP" do allow($stderr).to receive(:write) end + it "should not skip loading a resource when there's a top level symbol of the same name" do + Object.const_set('LwrpFoo', Class.new) + file = File.expand_path( "lwrp/resources/foo.rb", CHEF_SPEC_DATA) + expect(Chef::Log).not_to receive(:info).with(/Skipping/) + expect(Chef::Log).not_to receive(:debug).with(/anymore/) + Chef::Resource::LWRPBase.build_from_file("lwrp", file, nil) + Object.send(:remove_const, 'LwrpFoo') + Chef::Resource.send(:remove_const, 'LwrpFoo') + end + + it "should not skip loading a provider when there's a top level symbol of the same name" do + Object.const_set('LwrpBuckPasser', Class.new) + file = File.expand_path( "lwrp/providers/buck_passer.rb", CHEF_SPEC_DATA) + expect(Chef::Log).not_to receive(:info).with(/Skipping/) + expect(Chef::Log).not_to receive(:debug).with(/anymore/) + Chef::Provider::LWRPBase.build_from_file("lwrp", file, nil) + Object.send(:remove_const, 'LwrpBuckPasser') + Chef::Provider.send(:remove_const, 'LwrpBuckPasser') + end + + # @todo: we need a before block to manually remove_const all of the LWRPs that we + # load in these tests. we're threading state through these tests in LWRPs that + # have already been loaded in prior tests, which probably renders some of them bogus + it "should log if attempting to load resource of same name" do Dir[File.expand_path( "lwrp/resources/*", CHEF_SPEC_DATA)].each do |file| Chef::Resource::LWRPBase.build_from_file("lwrp", file, nil) -- cgit v1.2.1