summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-07-06 13:17:04 -0600
committerJay Mundrawala <jdmundrawala@gmail.com>2015-07-06 18:12:50 -0700
commitfdd5d0912a2565301bab784a7c7d3b1ee0bc8d20 (patch)
tree362e49825f868dcf6610c6f42b7d05cea8b45ca0
parent348b4557d02e184bcc45d1bb7842989863429bcf (diff)
downloadchef-fdd5d0912a2565301bab784a7c7d3b1ee0bc8d20.tar.gz
Merge branch 'jk/3634'
-rw-r--r--lib/chef/dsl/resources.rb10
-rw-r--r--spec/integration/recipes/recipe_dsl_spec.rb28
2 files changed, 34 insertions, 4 deletions
diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb
index 1ce12ed0a0..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, created_at=nil, &block)
- declare_resource(#{dsl_name.inspect}, name, created_at || 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, created_at=nil, &block|
- declare_resource(dsl_name, name, created_at || 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 4e80c5a738..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
@@ -47,6 +49,32 @@ describe "Recipe DSL methods" do
BaseThingy.created_provider = nil
end
+ it "creates base_thingy when you call base_thingy in a recipe" do
+ recipe = converge {
+ 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
+
+ it "errors out when you call base_thingy do ... end in a recipe" do
+ expect_converge {
+ base_thingy do; end
+ }.to raise_error(ArgumentError, 'You must supply a name when declaring a base_thingy resource')
+ end
+
+ 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
before do
Chef::Config[:treat_deprecation_warnings_as_errors] = false