summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-05-27 18:01:47 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-01 08:02:04 -0700
commit16dbca593b2fef74d952c5e119313efbe2288670 (patch)
treecdf5c0e038833788142299edd9a532aca57e551e
parentdfa9b6004e20b4d41bbc9b653e812c5136a336c6 (diff)
downloadchef-16dbca593b2fef74d952c5e119313efbe2288670.tar.gz
Toss an error if a resource without a resource_name is created
-rw-r--r--lib/chef/resource_builder.rb7
-rw-r--r--spec/integration/recipes/resource_definition_spec.rb21
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/chef/resource_builder.rb b/lib/chef/resource_builder.rb
index bb0962d128..9e9f7047a4 100644
--- a/lib/chef/resource_builder.rb
+++ b/lib/chef/resource_builder.rb
@@ -18,6 +18,10 @@
# NOTE: this was extracted from the Recipe DSL mixin, relevant specs are in spec/unit/recipe_spec.rb
+require 'chef/exceptions'
+require 'chef/resource'
+require 'chef/log'
+
class Chef
class ResourceBuilder
attr_reader :type
@@ -46,6 +50,9 @@ class Chef
raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil?
@resource = resource_class.new(name, run_context)
+ if resource.resource_name.nil?
+ raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`! Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?"
+ end
resource.source_line = created_at
resource.declared_type = type
diff --git a/spec/integration/recipes/resource_definition_spec.rb b/spec/integration/recipes/resource_definition_spec.rb
new file mode 100644
index 0000000000..da92bb18fa
--- /dev/null
+++ b/spec/integration/recipes/resource_definition_spec.rb
@@ -0,0 +1,21 @@
+require 'support/shared/integration/integration_helper'
+
+describe "Resource definition" do
+ include IntegrationSupport
+
+ context "With a resource with no resource_name or provides line" do
+ before do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
+ end
+
+ before(:context) {
+ class Chef::Resource::ResourceDefinitionNoNameTest < Chef::Resource
+ end
+ }
+ it "Creating said resource with the resource builder fails with an exception" do
+ expect_converge {
+ resource_definition_no_name_test 'blah'
+ }.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
+ end
+ end
+end