Implement recipe error reporting feature
This commit is contained in:
4
spec/fixtures/recipes/invalid.rb
vendored
Normal file
4
spec/fixtures/recipes/invalid.rb
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# this recipe will raise a NameError on line 4 when evaluated by the recipe
|
||||
# DSL.
|
||||
|
||||
invalid_keyword
|
@@ -20,7 +20,7 @@ module Producer::Core
|
||||
end
|
||||
|
||||
it 'evaluates the recipe' do
|
||||
expect(cli).to receive(:evaluate_recipe_file).with(arguments[1])
|
||||
expect(cli).to receive(:evaluate_recipe_file)
|
||||
cli.run!
|
||||
end
|
||||
end
|
||||
@@ -50,14 +50,14 @@ module Producer::Core
|
||||
describe '#evaluate_recipe_file' do
|
||||
it 'builds a recipe' do
|
||||
expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original
|
||||
cli.evaluate_recipe_file(arguments[1])
|
||||
cli.evaluate_recipe_file
|
||||
end
|
||||
|
||||
it 'builds an environment with the current recipe' do
|
||||
recipe = double('recipe').as_null_object
|
||||
allow(Recipe).to receive(:from_file).and_return(recipe)
|
||||
expect(Env).to receive(:new).with(recipe).and_call_original
|
||||
cli.evaluate_recipe_file(arguments[1])
|
||||
cli.evaluate_recipe_file
|
||||
end
|
||||
|
||||
it 'evaluates the recipe with the environment' do
|
||||
@@ -66,7 +66,33 @@ module Producer::Core
|
||||
env = double('env')
|
||||
allow(Env).to receive(:new).and_return(env)
|
||||
expect(recipe).to receive(:evaluate).with(env)
|
||||
cli.evaluate_recipe_file(arguments[1])
|
||||
cli.evaluate_recipe_file
|
||||
end
|
||||
|
||||
context 'error during recipe evaluation' do
|
||||
let(:arguments) { ['host', fixture_path_for('recipes/invalid.rb')] }
|
||||
let(:stdout) { StringIO.new }
|
||||
subject(:cli) { CLI.new(arguments, stdout) }
|
||||
|
||||
it 'exits with a return status of 70' do
|
||||
expect { cli.evaluate_recipe_file }
|
||||
.to raise_error(SystemExit) { |e|
|
||||
expect(e.status).to eq 70
|
||||
}
|
||||
end
|
||||
|
||||
it 'prints the error' do
|
||||
begin
|
||||
cli.evaluate_recipe_file
|
||||
rescue SystemExit
|
||||
end
|
||||
expect(stdout.string).to match(/
|
||||
\A
|
||||
#{arguments[1]}:4:
|
||||
.+
|
||||
invalid\srecipe\skeyword\s`invalid_keyword'
|
||||
/x)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@@ -5,7 +5,7 @@ module Producer::Core
|
||||
include FixturesHelpers
|
||||
|
||||
let(:code) { 'nil' }
|
||||
let(:env) { double('env') }
|
||||
let(:env) { double('env').as_null_object }
|
||||
subject(:recipe) { Recipe.new(code) }
|
||||
|
||||
describe '.from_file' do
|
||||
@@ -88,6 +88,24 @@ module Producer::Core
|
||||
it 'returns itself' do
|
||||
expect(dsl.evaluate(env)).to eq dsl
|
||||
end
|
||||
|
||||
context 'invalid recipe' do
|
||||
let(:filepath) { fixture_path_for 'recipes/error.rb' }
|
||||
let(:recipe) { Recipe.from_file(filepath) }
|
||||
subject(:dsl) { Recipe::DSL.new File.read(filepath) }
|
||||
|
||||
it 'reports the recipe file path in the error' do
|
||||
allow(env).to receive(:current_recipe) { recipe }
|
||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError) { |e|
|
||||
expect(e.backtrace.first).to match /\A#{filepath}/
|
||||
}
|
||||
end
|
||||
|
||||
it 'raises a RecipeEvaluationError on NameError' do
|
||||
dsl = Recipe::DSL.new { incorrect_keyword }
|
||||
expect { dsl.evaluate(env) }.to raise_error(Recipe::RecipeEvaluationError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'DSL specific methods' do
|
||||
|
Reference in New Issue
Block a user