diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2014-10-16 10:48:21 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2014-10-16 10:48:21 -0700 |
commit | fa3e81ecd2df09afa4b9de2e4e95d3b637ee9641 (patch) | |
tree | a74048651e7234a3239a2bfbd1126771db543c74 | |
parent | b80ababcfe48bb53bf745100d18a55381c6a9f45 (diff) | |
parent | b5343e318c96c9278cc354a89e7310ca978aab6d (diff) | |
download | chef-fa3e81ecd2df09afa4b9de2e4e95d3b637ee9641.tar.gz |
Merge pull request #2185 from opscode/lcg/definition-retval
return whatever the definition returns
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/chef/dsl/recipe.rb | 6 | ||||
-rw-r--r-- | spec/unit/recipe_spec.rb | 11 | ||||
-rw-r--r-- | spec/unit/resource_definition_spec.rb | 78 |
4 files changed, 51 insertions, 45 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f321120901..86ad521ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,6 +127,7 @@ ### Chef Contributions +* Recipe definition now returns the retval of the definition * Add support for Windows 10 to version helper. * `dsc_script` resource should honor configuration parameters when `configuration_data_script` is not set (Issue #2209) * Ruby has been updated to 2.1.3 along with rubygems update to 2.4.2 diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 476d9db666..0f46736823 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -53,9 +53,7 @@ class Chef end def has_resource_definition?(name) - yes_or_no = run_context.definitions.has_key?(name) - - yes_or_no + run_context.definitions.has_key?(name) end # Processes the arguments and block as a resource definition. @@ -69,12 +67,10 @@ class Chef # This sets up the parameter overrides new_def.instance_eval(&block) if block - new_recipe = Chef::Recipe.new(cookbook_name, recipe_name, run_context) new_recipe.params = new_def.params new_recipe.params[:name] = args[0] new_recipe.instance_eval(&new_def.recipe) - new_recipe end # Instantiates a resource (via #build_resource), then adds it to the diff --git a/spec/unit/recipe_spec.rb b/spec/unit/recipe_spec.rb index 5b6f3ea55b..e29ad47045 100644 --- a/spec/unit/recipe_spec.rb +++ b/spec/unit/recipe_spec.rb @@ -345,6 +345,17 @@ describe Chef::Recipe do end recipe.resources(:zen_master => "lao tzu").something.should eql(false) end + + it "should return the last statement in the definition as the retval" do + crow_define = Chef::ResourceDefinition.new + crow_define.define :crow, :peace => false, :something => true do + "the return val" + end + run_context.definitions[:crow] = crow_define + recipe.crow "mine" do + peace true + end.should eql("the return val") + end end end diff --git a/spec/unit/resource_definition_spec.rb b/spec/unit/resource_definition_spec.rb index f24254cfce..01e28bf091 100644 --- a/spec/unit/resource_definition_spec.rb +++ b/spec/unit/resource_definition_spec.rb @@ -19,24 +19,22 @@ require 'spec_helper' describe Chef::ResourceDefinition do - before(:each) do - @def = Chef::ResourceDefinition.new() - end + let(:defn) { Chef::ResourceDefinition.new() } describe "initialize" do it "should be a Chef::ResourceDefinition" do - @def.should be_a_kind_of(Chef::ResourceDefinition) + expect(defn).to be_a_kind_of(Chef::ResourceDefinition) end it "should not initialize a new node if one is not provided" do - @def.node.should eql(nil) + expect(defn.node).to eql(nil) end it "should accept a node as an argument" do node = Chef::Node.new node.name("bobo") - @def = Chef::ResourceDefinition.new(node) - @def.node.name.should == "bobo" + defn = Chef::ResourceDefinition.new(node) + expect(defn.node.name).to eq("bobo") end end @@ -44,76 +42,76 @@ describe Chef::ResourceDefinition do it "should set the node with node=" do node = Chef::Node.new node.name("bobo") - @def.node = node - @def.node.name.should == "bobo" + defn.node = node + expect(defn.node.name).to eq("bobo") end it "should return the node" do - @def.node = Chef::Node.new - @def.node.should be_a_kind_of(Chef::Node) + defn.node = Chef::Node.new + expect(defn.node).to be_a_kind_of(Chef::Node) end end it "should accept a new definition with a symbol for a name" do - lambda { - @def.define :smoke do + expect { + defn.define :smoke do end - }.should_not raise_error - lambda { - @def.define "george washington" do + }.not_to raise_error + expect { + defn.define "george washington" do end - }.should raise_error(ArgumentError) - @def.name.should eql(:smoke) + }.to raise_error(ArgumentError) + expect(defn.name).to eql(:smoke) end it "should accept a new definition with a hash" do - lambda { - @def.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do + expect { + defn.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do end - }.should_not raise_error + }.not_to raise_error end it "should expose the prototype hash params in the params hash" do - @def.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do; end - @def.params[:cigar].should eql("cuban") - @def.params[:cigarette].should eql("marlboro") + defn.define :smoke, :cigar => "cuban", :cigarette => "marlboro" do; end + expect(defn.params[:cigar]).to eql("cuban") + expect(defn.params[:cigarette]).to eql("marlboro") end it "should store the block passed to define as a proc under recipe" do - @def.define :smoke do + defn.define :smoke do "I am what I am" end - @def.recipe.should be_a_kind_of(Proc) - @def.recipe.call.should eql("I am what I am") + expect(defn.recipe).to be_a_kind_of(Proc) + expect(defn.recipe.call).to eql("I am what I am") end it "should set paramaters based on method_missing" do - @def.mind "to fly" - @def.params[:mind].should eql("to fly") + defn.mind "to fly" + expect(defn.params[:mind]).to eql("to fly") end it "should raise an exception if prototype_params is not a hash" do - lambda { - @def.define :monkey, Array.new do + expect { + defn.define :monkey, Array.new do end - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end it "should raise an exception if define is called without a block" do - lambda { - @def.define :monkey - }.should raise_error(ArgumentError) + expect { + defn.define :monkey + }.to raise_error(ArgumentError) end it "should load a description from a file" do - @def.from_file(File.join(CHEF_SPEC_DATA, "definitions", "test.rb")) - @def.name.should eql(:rico_suave) - @def.params[:rich].should eql("smooth") + defn.from_file(File.join(CHEF_SPEC_DATA, "definitions", "test.rb")) + expect(defn.name).to eql(:rico_suave) + expect(defn.params[:rich]).to eql("smooth") end it "should turn itself into a string based on the name with to_s" do - @def.name = :woot - @def.to_s.should eql("woot") + defn.name = :woot + expect(defn.to_s).to eql("woot") end end |