diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-30 12:24:56 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-30 12:24:56 -0700 |
commit | de209e6ee12939f641ec0ed002c2d3506abaafe7 (patch) | |
tree | 0a6b37f0519a71444250c5ffd84d3df89649228d | |
parent | 5584126a725ab72c24ea610328f56a905986295b (diff) | |
download | chef-de209e6ee12939f641ec0ed002c2d3506abaafe7.tar.gz |
make nameless properties opt-in
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/dsl/resources.rb | 8 | ||||
-rw-r--r-- | lib/chef/resource.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource/apt_update.rb | 4 | ||||
-rw-r--r-- | lib/chef/resource_builder.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource_collection/resource_set.rb | 4 | ||||
-rw-r--r-- | spec/integration/recipes/recipe_dsl_spec.rb | 27 | ||||
-rw-r--r-- | spec/unit/dsl/resources_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 5 |
8 files changed, 33 insertions, 21 deletions
diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb index 4baec22f2e..638e626ae2 100644 --- a/lib/chef/dsl/resources.rb +++ b/lib/chef/dsl/resources.rb @@ -34,10 +34,10 @@ class Chef def self.add_resource_dsl(dsl_name) module_eval(<<-EOM, __FILE__, __LINE__ + 1) - def #{dsl_name}(arg = "", &block) - declare_resource(#{dsl_name.inspect}, arg, created_at: caller[0], &block) - end - EOM + def #{dsl_name}(args = nil, &block) + declare_resource(#{dsl_name.inspect}, args, created_at: caller[0], &block) + end + EOM end def self.remove_resource_dsl(dsl_name) diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 2aee8536a4..9dc01a8d3a 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -84,7 +84,7 @@ class Chef # @param name [Object] The name to set, typically a String or Array # @return [String] The name of this Resource. # - property :name, String, coerce: proc { |v| v.is_a?(Array) ? v.join(", ") : v.to_s }, desired_state: false + property :name, String, coerce: proc { |v| v.is_a?(Array) ? v.join(", ") : v.to_s }, desired_state: false, required: true # # The node the current Chef run is using. diff --git a/lib/chef/resource/apt_update.rb b/lib/chef/resource/apt_update.rb index 1a25ec2ef5..67ca7fbfea 100644 --- a/lib/chef/resource/apt_update.rb +++ b/lib/chef/resource/apt_update.rb @@ -1,6 +1,6 @@ # # Author:: Thom May (<thom@chef.io>) -# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# Copyright:: Copyright (c) 2016-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,8 @@ class Chef resource_name :apt_update provides :apt_update + # allow bare apt_update with no name + property :name, String, default: "" property :frequency, Integer, default: 86_400 default_action :periodic diff --git a/lib/chef/resource_builder.rb b/lib/chef/resource_builder.rb index 275f795362..43a2495cba 100644 --- a/lib/chef/resource_builder.rb +++ b/lib/chef/resource_builder.rb @@ -43,8 +43,6 @@ class Chef end def build(&block) - 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?" diff --git a/lib/chef/resource_collection/resource_set.rb b/lib/chef/resource_collection/resource_set.rb index 66ccc1ccb3..111d23dc09 100644 --- a/lib/chef/resource_collection/resource_set.rb +++ b/lib/chef/resource_collection/resource_set.rb @@ -46,7 +46,7 @@ class Chef def insert_as(resource, resource_type = nil, instance_name = nil) is_chef_resource!(resource) resource_type ||= resource.resource_name - instance_name ||= resource.name || "" + instance_name ||= resource.name key = create_key(resource_type, instance_name) @resources_by_key[key] = resource end @@ -145,7 +145,7 @@ class Chef private - def create_key(resource_type, instance_name = "") + def create_key(resource_type, instance_name) "#{resource_type}[#{instance_name}]" end diff --git a/spec/integration/recipes/recipe_dsl_spec.rb b/spec/integration/recipes/recipe_dsl_spec.rb index 14cee0e1fd..f7a0e8b6e8 100644 --- a/spec/integration/recipes/recipe_dsl_spec.rb +++ b/spec/integration/recipes/recipe_dsl_spec.rb @@ -59,13 +59,30 @@ describe "Recipe DSL methods" do expect(BaseThingy.created_resource).to eq BaseThingy end - it "does not errors when you call base_thingy do ... end in a recipe" do - recipe = converge do + it "errors when you call base_thingy do ... end in a recipe" do + expect_converge do base_thingy { ; } + end.to raise_error(Chef::Exceptions::ValidationFailed) + end + + context "nameless resources" do + before(:context) do + class NamelessThingy < BaseThingy + resource_name :nameless_thingy + provides :nameless_thingy + + property :name, String, default: "" + end + end + + it "does not error when not given a name" do + recipe = converge do + nameless_thingy {} + end + expect(recipe.logged_warnings).to eq "" + expect(BaseThingy.created_name).to eq "" + expect(BaseThingy.created_resource).to eq NamelessThingy end - expect(recipe.logged_warnings).to eq "" - expect(BaseThingy.created_name).to eq "" - expect(BaseThingy.created_resource).to eq BaseThingy end context "Deprecated automatic resource DSL" do diff --git a/spec/unit/dsl/resources_spec.rb b/spec/unit/dsl/resources_spec.rb index 5354505df8..dc05f8bcf4 100644 --- a/spec/unit/dsl/resources_spec.rb +++ b/spec/unit/dsl/resources_spec.rb @@ -80,6 +80,6 @@ describe Chef::DSL::Resources do test_resource {} end end - it { is_expected.to eq [[:test_resource, ""]] } + it { is_expected.to eq [[:test_resource, nil]] } end end diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index c60f470a77..7a538b721b 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -74,11 +74,6 @@ describe Chef::Recipe do expect { recipe.not_home("not_home_resource") }.to raise_error(NameError) end - it "does not require a name argument and looks up with empty brackets" do - recipe.zen_master - expect(run_context.resource_collection.lookup("zen_master[]").name).to eql("") - end - it "should allow regular errors (not NameErrors) to pass unchanged" do expect do recipe.cat("felix") { raise ArgumentError, "You Suck" } |