Implement recipe error reporting feature

This commit is contained in:
Thibault Jouan 2013-07-29 22:12:03 +00:00
parent f6d9dd2c90
commit e12dd5c0e7
6 changed files with 85 additions and 9 deletions

View File

@ -9,6 +9,21 @@ Feature: recipe evaluation
Then the exit status must be 0 Then the exit status must be 0
And the output must contain "hello from recipe" And the output must contain "hello from recipe"
Scenario: reports errors when evaluating an invalid recipe
Given a recipe with:
"""
puts 'OK'
invalid_keyword
"""
When I execute the recipe
Then the exit status must be 70
And the output must match:
"""
\AOK
recipe.rb:3:.+invalid recipe keyword `invalid_keyword'
"""
Scenario: source keyword, requires a recipe file Scenario: source keyword, requires a recipe file
Given a recipe with: Given a recipe with:
""" """

View File

@ -12,17 +12,24 @@ module Producer
def run! def run!
check_arguments! check_arguments!
evaluate_recipe_file(@arguments[1]) evaluate_recipe_file
end end
def check_arguments! def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 2 print_usage_and_exit(64) unless @arguments.length == 2
end end
def evaluate_recipe_file(filepath) def evaluate_recipe_file
recipe = Recipe.from_file(@arguments[1]) recipe = Recipe.from_file(@arguments[1])
env = Env.new(recipe) env = Env.new(recipe)
recipe.evaluate env begin
recipe.evaluate env
rescue Recipe::RecipeEvaluationError => e
@stdout.puts [e.backtrace.shift, e.message].join ': '
@stdout.puts e.backtrace
exit 70
end
end end

View File

@ -1,6 +1,8 @@
module Producer module Producer
module Core module Core
class Recipe class Recipe
RecipeEvaluationError = Class.new(StandardError)
attr_reader :code, :filepath attr_reader :code, :filepath
def self.from_file(filepath) def self.from_file(filepath)
@ -29,11 +31,15 @@ module Producer
def evaluate(env) def evaluate(env)
if @code if @code
instance_eval @code instance_eval @code, env.current_recipe.filepath
else else
instance_eval &@block instance_eval &@block
end end
self self
rescue NameError => e
err = RecipeEvaluationError.new("invalid recipe keyword `#{e.name}'")
err.set_backtrace e.backtrace.reject { |l| l =~ /\/producer-core\// }
raise err
end end

4
spec/fixtures/recipes/invalid.rb vendored Normal file
View File

@ -0,0 +1,4 @@
# this recipe will raise a NameError on line 4 when evaluated by the recipe
# DSL.
invalid_keyword

View File

@ -20,7 +20,7 @@ module Producer::Core
end end
it 'evaluates the recipe' do it 'evaluates the recipe' do
expect(cli).to receive(:evaluate_recipe_file).with(arguments[1]) expect(cli).to receive(:evaluate_recipe_file)
cli.run! cli.run!
end end
end end
@ -50,14 +50,14 @@ module Producer::Core
describe '#evaluate_recipe_file' do describe '#evaluate_recipe_file' do
it 'builds a recipe' do it 'builds a recipe' do
expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original expect(Recipe).to receive(:from_file).with(arguments[1]).and_call_original
cli.evaluate_recipe_file(arguments[1]) cli.evaluate_recipe_file
end end
it 'builds an environment with the current recipe' do it 'builds an environment with the current recipe' do
recipe = double('recipe').as_null_object recipe = double('recipe').as_null_object
allow(Recipe).to receive(:from_file).and_return(recipe) allow(Recipe).to receive(:from_file).and_return(recipe)
expect(Env).to receive(:new).with(recipe).and_call_original expect(Env).to receive(:new).with(recipe).and_call_original
cli.evaluate_recipe_file(arguments[1]) cli.evaluate_recipe_file
end end
it 'evaluates the recipe with the environment' do it 'evaluates the recipe with the environment' do
@ -66,7 +66,33 @@ module Producer::Core
env = double('env') env = double('env')
allow(Env).to receive(:new).and_return(env) allow(Env).to receive(:new).and_return(env)
expect(recipe).to receive(:evaluate).with(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 end
end end

View File

@ -5,7 +5,7 @@ module Producer::Core
include FixturesHelpers include FixturesHelpers
let(:code) { 'nil' } let(:code) { 'nil' }
let(:env) { double('env') } let(:env) { double('env').as_null_object }
subject(:recipe) { Recipe.new(code) } subject(:recipe) { Recipe.new(code) }
describe '.from_file' do describe '.from_file' do
@ -88,6 +88,24 @@ module Producer::Core
it 'returns itself' do it 'returns itself' do
expect(dsl.evaluate(env)).to eq dsl expect(dsl.evaluate(env)).to eq dsl
end 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 end
context 'DSL specific methods' do context 'DSL specific methods' do