Improve exceptions usage in specs:

* Throw symbols instead of raising exceptions to test code evaluation,
  this will reduce "bad" usages of exceptions, symbols seems a little
  better for now;
* Add a specific exception to be used in specs for testing behaviour
  when an exception is raised from a recipe.
This commit is contained in:
Thibault Jouan 2013-08-01 23:13:24 +00:00
parent c3737e738e
commit d677c3aa97
5 changed files with 21 additions and 13 deletions

View File

@ -1 +1 @@
raise 'error from recipe'
raise SomeErrorInRecipe, 'some recipe error'

1
spec/fixtures/recipes/throw.rb vendored Normal file
View File

@ -0,0 +1 @@
throw :recipe_code

View File

@ -28,8 +28,8 @@ module Producer::Core
describe '#evaluate' do
it 'evaluates its code' do
dsl = Recipe::DSL.new { raise 'error from recipe' }
expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
dsl = Recipe::DSL.new { throw :recipe_code }
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
end
it 'returns itself' do
@ -43,7 +43,7 @@ module Producer::Core
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 { dsl.evaluate(env) }.to raise_error(SomeErrorInRecipe) { |e|
expect(e.backtrace.first).to match /\A#{filepath}/
}
end
@ -59,17 +59,19 @@ module Producer::Core
subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
describe '#source' do
let(:filepath) { fixture_path_for 'recipes/error' }
let(:filepath) { fixture_path_for 'recipes/throw' }
let(:code) { "source '#{filepath}'" }
subject(:dsl) { Recipe::DSL.new code }
it 'sources the recipe given as argument' do
expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
end
context 'when sourced recipe is invalid' do
let(:filepath) { fixture_path_for 'recipes/error' }
it 'reports its file path in the error' do
expect { dsl.evaluate(env) }.to raise_error(RuntimeError) { |e|
expect { dsl.evaluate(env) }.to raise_error(SomeErrorInRecipe) { |e|
expect(e.backtrace.first).to match /\A#{filepath}/
}
end

View File

@ -6,11 +6,11 @@ module Producer::Core
subject(:dsl) { Task::DSL.new &block }
describe '#evaluate' do
let(:block) { proc { raise 'error from task' } }
let(:block) { proc { throw :task_code } }
it 'evaluates its code' do
expect { dsl.evaluate(env) }
.to raise_error(RuntimeError, 'error from task')
.to throw_symbol :task_code
end
context 'when given block is invalid' do
@ -26,23 +26,23 @@ module Producer::Core
context 'when met (block evals to true)' do
let(:block) { proc {
condition { true }
raise 'error after condition'
throw :after_condition
} }
it 'evaluates all the block' do
expect { dsl.evaluate(env) }
.to raise_error(RuntimeError, 'error after condition')
.to throw_symbol :after_condition
end
end
context 'when not met (block evals to false)' do
let(:block) { proc {
condition { false }
raise
throw :after_condition
} }
it 'stops block evaluation' do
expect { dsl.evaluate(env) }.not_to raise_error
expect { dsl.evaluate(env) }.not_to throw_symbol :after_condition
end
end
end

View File

@ -2,3 +2,8 @@ require 'producer/core'
require 'support/exit_helpers'
require 'support/fixtures_helpers'
# Specific error thrown in the error fixture recipe, we can't define it in the
# recipe, rspec wouldn't know about it.
SomeErrorInRecipe = Class.new(RuntimeError)