summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2020-03-17 23:10:44 -0400
committerBryan McLellan <btm@loftninjas.org>2020-03-23 12:57:09 -0400
commitf8545babece542e9741d810bbbbebfcb3491bf64 (patch)
tree201109c454e7dd4967f7e9eafc25b735e5e64f19
parentf59c0443f43f6d8f0e7d95a030b34346c6cce482 (diff)
downloadchef-f8545babece542e9741d810bbbbebfcb3491bf64.tar.gz
Add YAML support to 'chef-apply'
Allows `chef-apply foo.yml` to be parsed as a YAML recipe. Also adds a configuration flag to enable `chef-apply foo.whatever --yaml`. Signed-off-by: Bryan McLellan <btm@loftninjas.org>
-rw-r--r--lib/chef/application/apply.rb16
-rw-r--r--spec/unit/application/apply_spec.rb17
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/chef/application/apply.rb b/lib/chef/application/apply.rb
index 4718e8750b..e242f91d29 100644
--- a/lib/chef/application/apply.rb
+++ b/lib/chef/application/apply.rb
@@ -105,6 +105,12 @@ class Chef::Application::Apply < Chef::Application
description: "Enable whyrun mode.",
boolean: true
+ option :yaml,
+ long: "--yaml",
+ description: "Parse recipe as YAML",
+ boolean: true,
+ default: false
+
option :profile_ruby,
long: "--[no-]profile-ruby",
description: "Dump complete Ruby call graph stack of entire #{Chef::Dist::PRODUCT} run (expert only).",
@@ -148,6 +154,10 @@ class Chef::Application::Apply < Chef::Application
if file_name.nil?
Chef::Application.fatal!("No recipe file was provided", Chef::Exceptions::RecipeNotFound.new)
else
+ if file_name =~ /\.yml$/
+ logger.info "Recipe file name ends with .yml, parsing as YAML"
+ config[:yaml] = true
+ end
recipe_path = File.expand_path(file_name)
unless File.exist?(recipe_path)
Chef::Application.fatal!("No file exists at #{recipe_path}", Chef::Exceptions::RecipeNotFound.new)
@@ -198,7 +208,11 @@ class Chef::Application::Apply < Chef::Application
@recipe_text, @recipe_fh = read_recipe_file @recipe_filename
end
recipe, run_context = get_recipe_and_run_context
- recipe.instance_eval(@recipe_text, @recipe_filename, 1)
+ if config[:yaml]
+ recipe.from_yaml(@recipe_text)
+ else
+ recipe.instance_eval(@recipe_text, @recipe_filename, 1)
+ end
runner = Chef::Runner.new(run_context)
catch(:end_client_run_early) do
begin
diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb
index b659f13fe8..d7927c84e1 100644
--- a/spec/unit/application/apply_spec.rb
+++ b/spec/unit/application/apply_spec.rb
@@ -46,6 +46,7 @@ describe Chef::Application::Apply do
it "should read text properly" do
expect(@app.read_recipe_file(@recipe_file_name)[0]).to eq(@recipe_text)
end
+
it "should return a file_handle" do
expect(@app.read_recipe_file(@recipe_file_name)[1]).to be_instance_of(RSpec::Mocks::Double)
end
@@ -57,6 +58,7 @@ describe Chef::Application::Apply do
@app.read_recipe_file(nil)
end
end
+
describe "when recipe doesn't exist" do
before do
allow(File).to receive(:exist?).with(@recipe_path).and_return(false)
@@ -67,7 +69,22 @@ describe Chef::Application::Apply do
@app.read_recipe_file(@recipe_file_name)
end
end
+
+ describe "when the recipe filename ends in .yml" do
+ it "configures for parsing the recipe as YAML" do
+ @recipe_file_name = "foo.yml"
+ @recipe_path = File.expand_path(@recipe_file_name)
+ @recipe_file = double("Tempfile (mock)", read: @recipe_text)
+ allow(@app).to receive(:open).with(@recipe_path).and_return(@recipe_file)
+ allow(File).to receive(:exist?).with(@recipe_path).and_return(true)
+ allow(Chef::Application).to receive(:fatal!).and_return(true)
+
+ @app.read_recipe_file(@recipe_file_name)
+ expect(@app.config[:yaml]).to eq(true)
+ end
+ end
end
+
describe "temp_recipe_file" do
before do
@app.instance_variable_set(:@recipe_text, @recipe_text)