diff options
author | David Workman <workmad3@gmail.com> | 2014-09-03 23:49:36 +0100 |
---|---|---|
committer | David Workman <workmad3@gmail.com> | 2014-09-03 23:49:36 +0100 |
commit | 10621b23c26cd813cc8559d61105bf8be20e0c37 (patch) | |
tree | d98bd7f87d1e00ee4702ca01fba2342f1b038682 | |
parent | f1de2a538ba8b89bdcb24de168db0b99cb0cc8fb (diff) | |
download | chef-10621b23c26cd813cc8559d61105bf8be20e0c37.tar.gz |
Fixing case of a nil recipe file with chef apply
Raising the error 'No recipe file was provided' with a fatal!
call when the recipe_name is nil. This case occurs when
chef-apply is called without a recipe name argument.
Also renamed the apply spec file to apply_spec so it is picked
up by an rspec test run. Fixed a typo in the file where it was
creating a 'Chef::Application::Recipe' for @app instead of
'Chef::Application::Apply'
-rw-r--r-- | lib/chef/application/apply.rb | 18 | ||||
-rw-r--r-- | spec/unit/application/apply_spec.rb (renamed from spec/unit/application/apply.rb) | 13 |
2 files changed, 21 insertions, 10 deletions
diff --git a/lib/chef/application/apply.rb b/lib/chef/application/apply.rb index f2dd2cb586..7e5d5406c0 100644 --- a/lib/chef/application/apply.rb +++ b/lib/chef/application/apply.rb @@ -92,14 +92,18 @@ class Chef::Application::Apply < Chef::Application end def read_recipe_file(file_name) - recipe_path = file_name.to_s - unless File.exist?(recipe_path) - Chef::Application.fatal!("No file exists at #{recipe_path}", 1) + if file_name.nil? + Chef::Application.fatal!("No recipe file was provided", 1) + else + recipe_path = file_name + unless File.exist?(recipe_path) + Chef::Application.fatal!("No file exists at #{recipe_path}", 1) + end + recipe_path = File.expand_path(recipe_path) + recipe_fh = open(recipe_path) + recipe_text = recipe_fh.read + [recipe_text, recipe_fh] end - recipe_path = File.expand_path(recipe_path) - recipe_fh = open(recipe_path) - recipe_text = recipe_fh.read - [recipe_text, recipe_fh] end def get_recipe_and_run_context diff --git a/spec/unit/application/apply.rb b/spec/unit/application/apply_spec.rb index 32c98c6ed6..3f7fb851c2 100644 --- a/spec/unit/application/apply.rb +++ b/spec/unit/application/apply_spec.rb @@ -20,7 +20,7 @@ require 'spec_helper' describe Chef::Application::Apply do before do - @app = Chef::Application::Recipe.new + @app = Chef::Application::Apply.new @app.stub(:configure_logging).and_return(true) @recipe_text = "package 'nyancat'" Chef::Config[:solo] = true @@ -41,18 +41,25 @@ describe Chef::Application::Apply do File.stub(:exist?).with("foo.rb").and_return(true) Chef::Application.stub(:fatal!).and_return(true) end + it "should read text properly" do @app.read_recipe_file(@recipe_file_name)[0].should == @recipe_text end it "should return a file_handle" do @app.read_recipe_file(@recipe_file_name)[1].should be_instance_of(RSpec::Mocks::Mock) end + describe "when recipe is nil" do + it "should raise a fatal with the missing filename message" do + Chef::Application.should_receive(:fatal!).with("No recipe file was provided", 1) + @app.read_recipe_file(nil) + end + end describe "when recipe doesn't exist" do before do File.stub(:exist?).with(@recipe_file_name).and_return(false) end - it "should raise a fatal" do - Chef::Application.should_receive(:fatal!) + it "should raise a fatal with the file doesn't exist message" do + Chef::Application.should_receive(:fatal!).with(/^No file exists at/, 1) @app.read_recipe_file(@recipe_file_name) end end |