summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-07-06 13:04:46 -0600
committerJohn Keiser <john@johnkeiser.com>2015-07-06 13:04:46 -0600
commit2408528f13ce5b47c18f0db4c6b6d3cf58d25c0a (patch)
tree9243002f6b6d294e4f1bb21a4bc714c9d577c1a1
parentcb163f4b69e0378b9dc4c60b935b891e05706ccc (diff)
downloadchef-2408528f13ce5b47c18f0db4c6b6d3cf58d25c0a.tar.gz
Deprecate passing more than 1 argument to create a resourcejk/3634
-rw-r--r--lib/chef/dsl/resources.rb10
-rw-r--r--spec/integration/recipes/recipe_dsl_spec.rb16
2 files changed, 18 insertions, 8 deletions
diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb
index c634c6520d..f15beaeab0 100644
--- a/lib/chef/dsl/resources.rb
+++ b/lib/chef/dsl/resources.rb
@@ -10,14 +10,16 @@ class Chef
def self.add_resource_dsl(dsl_name)
begin
module_eval(<<-EOM, __FILE__, __LINE__+1)
- def #{dsl_name}(name=nil, &block)
- declare_resource(#{dsl_name.inspect}, name, caller[0], &block)
+ def #{dsl_name}(*args, &block)
+ Chef::Log.deprecation("Cannot create resource #{dsl_name} with more than one argument. All arguments except the name (\#{args[0].inspect}) will be ignored. This will cause an error in Chef 13. Arguments: \#{args}") if args.size > 1
+ declare_resource(#{dsl_name.inspect}, args[0], caller[0], &block)
end
EOM
rescue SyntaxError
# Handle the case where dsl_name has spaces, etc.
- define_method(dsl_name.to_sym) do |name=nil, &block|
- declare_resource(dsl_name, name, caller[0], &block)
+ define_method(dsl_name.to_sym) do |*args, &block|
+ Chef::Log.deprecation("Cannot create resource #{dsl_name} with more than one argument. All arguments except the name (#{args[0].inspect}) will be ignored. This will cause an error in Chef 13. Arguments: #{args}") if args.size > 1
+ declare_resource(dsl_name, args[0], caller[0], &block)
end
end
end
diff --git a/spec/integration/recipes/recipe_dsl_spec.rb b/spec/integration/recipes/recipe_dsl_spec.rb
index 1ab01fb20e..a4de6ae542 100644
--- a/spec/integration/recipes/recipe_dsl_spec.rb
+++ b/spec/integration/recipes/recipe_dsl_spec.rb
@@ -19,6 +19,7 @@ describe "Recipe DSL methods" do
default_action :create
class<<self
+ attr_accessor :created_name
attr_accessor :created_resource
attr_accessor :created_provider
end
@@ -30,6 +31,7 @@ describe "Recipe DSL methods" do
def load_current_resource
end
def action_create
+ BaseThingy.created_name = new_resource.name
BaseThingy.created_resource = new_resource.class
BaseThingy.created_provider = self.class
end
@@ -52,6 +54,7 @@ describe "Recipe DSL methods" do
base_thingy 'blah' do; end
}
expect(recipe.logged_warnings).to eq ''
+ expect(BaseThingy.created_name).to eq 'blah'
expect(BaseThingy.created_resource).to eq BaseThingy
end
@@ -61,10 +64,15 @@ describe "Recipe DSL methods" do
}.to raise_error(ArgumentError, 'You must supply a name when declaring a base_thingy resource')
end
- it "errors out when you call base_thingy 'foo', 'bar' do ... end in a recipe" do
- expect_converge {
- base_thingy 'foo', 'bar' do; end
- }.to raise_error(ArgumentError, 'wrong number of arguments (2 for 0..1)')
+ it "emits a warning when you call base_thingy 'foo', 'bar' do ... end in a recipe" do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
+ recipe = converge {
+ base_thingy 'foo', 'bar' do
+ end
+ }
+ expect(recipe.logged_warnings).to match(/Cannot create resource base_thingy with more than one argument. All arguments except the name \("foo"\) will be ignored. This will cause an error in Chef 13. Arguments: \["foo", "bar"\]/)
+ expect(BaseThingy.created_name).to eq 'foo'
+ expect(BaseThingy.created_resource).to eq BaseThingy
end
context "Deprecated automatic resource DSL" do