diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-07 16:34:30 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-05-13 13:43:15 -0700 |
commit | 95e77aace5fa40ecbdee6fe9d341184b3cf7b8a3 (patch) | |
tree | f1b8c728799ae1806ed96dada17a329ae7f8c9c9 | |
parent | 146f0809c1912988751688fff2ef6945c90a4513 (diff) | |
download | chef-95e77aace5fa40ecbdee6fe9d341184b3cf7b8a3.tar.gz |
Replace lookup_provider_constant and protect against deprecation in tests
-rw-r--r-- | lib/chef/resource.rb | 17 | ||||
-rw-r--r-- | spec/data/lwrp/providers/buck_passer.rb | 20 | ||||
-rw-r--r-- | spec/data/lwrp/providers/buck_passer_2.rb | 20 | ||||
-rw-r--r-- | spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb | 16 | ||||
-rw-r--r-- | spec/unit/lwrp_spec.rb | 32 |
5 files changed, 81 insertions, 24 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index e62e63a44a..2f5c2b7798 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -686,8 +686,6 @@ class Chef # def provider(arg=nil) klass = if arg.kind_of?(String) || arg.kind_of?(Symbol) - # TODO deprecate. This does class munging. If you know - # the class name, just pass the class. lookup_provider_constant(arg) else arg @@ -1173,14 +1171,17 @@ class Chef end extend DeprecatedLWRPClass + # @api private def lookup_provider_constant(name, action=:nothing) - resource = Chef::Resource.new(self.name, self.run_context) - resource.instance_eval { @resource_name = name } - provider = Chef::ProviderResolver.new(self.node, resource, action).resolve - if !provider - raise ArgumentError, "No provider found to match '#{name}'" + begin + self.class.provider_base.const_get(convert_to_class_name(name.to_s)) + rescue NameError => e + if e.to_s =~ /#{Regexp.escape(self.class.provider_base.to_s)}/ + raise ArgumentError, "No provider found to match '#{name}'" + else + raise e + end end - provider end end end diff --git a/spec/data/lwrp/providers/buck_passer.rb b/spec/data/lwrp/providers/buck_passer.rb index 9792e2c824..2bbca07bf7 100644 --- a/spec/data/lwrp/providers/buck_passer.rb +++ b/spec/data/lwrp/providers/buck_passer.rb @@ -1,12 +1,28 @@ provides :buck_passer +def without_deprecation_warnings(&block) + old_treat_deprecation_warnings_as_errors = Chef::Config[:treat_deprecation_warnings_as_errors] + Chef::Config[:treat_deprecation_warnings_as_errors] = false + begin + block.call + ensure + Chef::Config[:treat_deprecation_warnings_as_errors] = old_treat_deprecation_warnings_as_errors + end +end + action :pass_buck do lwrp_foo :prepared_thumbs do action :prepare_thumbs - provider :lwrp_thumb_twiddler + # We know there will be a deprecation error here; head it off + without_deprecation_warnings do + provider :lwrp_thumb_twiddler + end end lwrp_foo :twiddled_thumbs do action :twiddle_thumbs - provider :lwrp_thumb_twiddler + # We know there will be a deprecation error here; head it off + without_deprecation_warnings do + provider :lwrp_thumb_twiddler + end end end diff --git a/spec/data/lwrp/providers/buck_passer_2.rb b/spec/data/lwrp/providers/buck_passer_2.rb index d34da3c378..c3bab7266f 100644 --- a/spec/data/lwrp/providers/buck_passer_2.rb +++ b/spec/data/lwrp/providers/buck_passer_2.rb @@ -1,10 +1,26 @@ +def without_deprecation_warnings(&block) + old_treat_deprecation_warnings_as_errors = Chef::Config[:treat_deprecation_warnings_as_errors] + Chef::Config[:treat_deprecation_warnings_as_errors] = false + begin + block.call + ensure + Chef::Config[:treat_deprecation_warnings_as_errors] = old_treat_deprecation_warnings_as_errors + end +end + action :pass_buck do lwrp_bar :prepared_eyes do action :prepare_eyes - provider :lwrp_paint_drying_watcher + # We know there will be a deprecation error here; head it off + without_deprecation_warnings do + provider :lwrp_paint_drying_watcher + end end lwrp_bar :dried_paint_watched do action :watch_paint_dry - provider :lwrp_paint_drying_watcher + # We know there will be a deprecation error here; head it off + without_deprecation_warnings do + provider :lwrp_paint_drying_watcher + end end end diff --git a/spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb b/spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb index f5841fb01c..77c1111ff5 100644 --- a/spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb +++ b/spec/data/lwrp/providers/embedded_resource_accesses_providers_scope.rb @@ -3,11 +3,23 @@ # are passed properly (as demonstrated by the call to generate_new_name). attr_reader :enclosed_resource +def without_deprecation_warnings(&block) + old_treat_deprecation_warnings_as_errors = Chef::Config[:treat_deprecation_warnings_as_errors] + Chef::Config[:treat_deprecation_warnings_as_errors] = false + begin + block.call + ensure + Chef::Config[:treat_deprecation_warnings_as_errors] = old_treat_deprecation_warnings_as_errors + end +end + action :twiddle_thumbs do @enclosed_resource = lwrp_foo :foo do monkey generate_new_name(new_resource.monkey){ 'the monkey' } - action :twiddle_thumbs - provider :lwrp_monkey_name_printer + # We know there will be a deprecation error here; head it off + without_deprecation_warnings do + provider :lwrp_monkey_name_printer + end end end diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index 36b470295f..a1cccb3071 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -19,11 +19,14 @@ require 'spec_helper' require 'tmpdir' require 'fileutils' +require 'chef/mixin/convert_to_class_name' module LwrpConstScopingConflict end describe "LWRP" do + include Chef::Mixin::ConvertToClassName + before do @original_VERBOSE = $VERBOSE $VERBOSE = nil @@ -38,6 +41,16 @@ describe "LWRP" do Chef::Resource.resource_for_node(name, Chef::Node.new) end + def get_lwrp_provider(name) + old_treat_deprecation_warnings_as_errors = Chef::Config[:treat_deprecation_warnings_as_errors] + Chef::Config[:treat_deprecation_warnings_as_errors] = false + begin + Chef::Provider.const_get(convert_to_class_name(name.to_s)) + ensure + Chef::Config[:treat_deprecation_warnings_as_errors] = old_treat_deprecation_warnings_as_errors + end + end + describe "when overriding an existing class" do before :each do allow($stderr).to receive(:write) @@ -327,13 +340,12 @@ describe "LWRP" do Dir[File.expand_path(File.expand_path("../../data/lwrp/providers/*", __FILE__))].each do |file| Chef::Provider::LWRPBase.build_from_file("lwrp", file, @run_context) end - end it "should properly handle a new_resource reference" do resource = get_lwrp(:lwrp_foo).new("morpheus", @run_context) resource.monkey("bob") - resource.provider(:lwrp_monkey_name_printer) + resource.provider(get_lwrp_provider(:lwrp_monkey_name_printer)) provider = Chef::Platform.provider_for_resource(resource, :twiddle_thumbs) provider.action_twiddle_thumbs @@ -354,15 +366,15 @@ describe "LWRP" do end it "should create a method for each action" do - expect(Chef::Provider::LwrpBuckPasser.instance_methods).to include(:action_pass_buck) - expect(Chef::Provider::LwrpThumbTwiddler.instance_methods).to include(:action_twiddle_thumbs) + expect(get_lwrp_provider(:lwrp_buck_passer).instance_methods).to include(:action_pass_buck) + expect(get_lwrp_provider(:lwrp_thumb_twiddler).instance_methods).to include(:action_twiddle_thumbs) end end it "should insert resources embedded in the provider into the middle of the resource collection" do injector = get_lwrp(:lwrp_foo).new("morpheus", @run_context) injector.action(:pass_buck) - injector.provider(:lwrp_buck_passer) + injector.provider(get_lwrp_provider(:lwrp_buck_passer)) dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) dummy.provider(Chef::Provider::Easy) @run_context.resource_collection.insert(injector) @@ -379,11 +391,11 @@ describe "LWRP" do it "should insert embedded resources from multiple providers, including from the last position, properly into the resource collection" do injector = get_lwrp(:lwrp_foo).new("morpheus", @run_context) injector.action(:pass_buck) - injector.provider(:lwrp_buck_passer) + injector.provider(get_lwrp_provider(:lwrp_buck_passer)) injector2 = get_lwrp(:lwrp_bar).new("tank", @run_context) injector2.action(:pass_buck) - injector2.provider(:lwrp_buck_passer_2) + injector2.provider(get_lwrp_provider(:lwrp_buck_passer_2)) dummy = Chef::Resource::ZenMaster.new("keanu reeves", @run_context) dummy.provider(Chef::Provider::Easy) @@ -406,7 +418,7 @@ describe "LWRP" do it "should properly handle a new_resource reference" do resource = get_lwrp(:lwrp_foo).new("morpheus", @run_context) resource.monkey("bob") - resource.provider(:lwrp_monkey_name_printer) + resource.provider(get_lwrp_provider(:lwrp_monkey_name_printer)) provider = Chef::Platform.provider_for_resource(resource, :twiddle_thumbs) provider.action_twiddle_thumbs @@ -417,7 +429,7 @@ describe "LWRP" do it "should properly handle an embedded Resource accessing the enclosing Provider's scope" do resource = get_lwrp(:lwrp_foo).new("morpheus", @run_context) resource.monkey("bob") - resource.provider(:lwrp_embedded_resource_accesses_providers_scope) + resource.provider(get_lwrp_provider(:lwrp_embedded_resource_accesses_providers_scope)) provider = Chef::Platform.provider_for_resource(resource, :twiddle_thumbs) #provider = @runner.build_provider(resource) @@ -437,7 +449,7 @@ describe "LWRP" do @resource = get_lwrp(:lwrp_foo).new("morpheus", @run_context) @resource.allowed_actions << :test @resource.action(:test) - @resource.provider(:lwrp_inline_compiler) + @resource.provider(get_lwrp_provider(:lwrp_inline_compiler)) end it "does not add interior resources to the exterior resource collection" do |