summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-02-03 12:58:19 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-02-03 12:58:19 -0800
commit5d3aef162c43e6239b6d8f5ef8a2045bd10632bc (patch)
treea2ccc6700b42d8bb222d489fd075e51d73ab4759
parent703b75790decd2d7f1fc271f9311091b03023241 (diff)
downloadchef-lcg/lwrp-const.tar.gz
fix LWRP constant lookupslcg/lwrp-const
* 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
-rw-r--r--lib/chef/provider/lwrp_base.rb2
-rw-r--r--lib/chef/resource.rb4
-rw-r--r--lib/chef/resource/lwrp_base.rb2
-rw-r--r--spec/unit/lwrp_spec.rb24
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)