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:
parent
c3737e738e
commit
d677c3aa97
2
spec/fixtures/recipes/error.rb
vendored
2
spec/fixtures/recipes/error.rb
vendored
@ -1 +1 @@
|
|||||||
raise 'error from recipe'
|
raise SomeErrorInRecipe, 'some recipe error'
|
||||||
|
1
spec/fixtures/recipes/throw.rb
vendored
Normal file
1
spec/fixtures/recipes/throw.rb
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
throw :recipe_code
|
@ -28,8 +28,8 @@ module Producer::Core
|
|||||||
|
|
||||||
describe '#evaluate' do
|
describe '#evaluate' do
|
||||||
it 'evaluates its code' do
|
it 'evaluates its code' do
|
||||||
dsl = Recipe::DSL.new { raise 'error from recipe' }
|
dsl = Recipe::DSL.new { throw :recipe_code }
|
||||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
|
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns itself' do
|
it 'returns itself' do
|
||||||
@ -43,7 +43,7 @@ module Producer::Core
|
|||||||
|
|
||||||
it 'reports the recipe file path in the error' do
|
it 'reports the recipe file path in the error' do
|
||||||
allow(env).to receive(:current_recipe) { recipe }
|
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}/
|
expect(e.backtrace.first).to match /\A#{filepath}/
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -59,17 +59,19 @@ module Producer::Core
|
|||||||
subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
|
subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
|
||||||
|
|
||||||
describe '#source' do
|
describe '#source' do
|
||||||
let(:filepath) { fixture_path_for 'recipes/error' }
|
let(:filepath) { fixture_path_for 'recipes/throw' }
|
||||||
let(:code) { "source '#{filepath}'" }
|
let(:code) { "source '#{filepath}'" }
|
||||||
subject(:dsl) { Recipe::DSL.new code }
|
subject(:dsl) { Recipe::DSL.new code }
|
||||||
|
|
||||||
it 'sources the recipe given as argument' do
|
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
|
end
|
||||||
|
|
||||||
context 'when sourced recipe is invalid' do
|
context 'when sourced recipe is invalid' do
|
||||||
|
let(:filepath) { fixture_path_for 'recipes/error' }
|
||||||
|
|
||||||
it 'reports its file path in the error' do
|
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}/
|
expect(e.backtrace.first).to match /\A#{filepath}/
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -6,11 +6,11 @@ module Producer::Core
|
|||||||
subject(:dsl) { Task::DSL.new &block }
|
subject(:dsl) { Task::DSL.new &block }
|
||||||
|
|
||||||
describe '#evaluate' do
|
describe '#evaluate' do
|
||||||
let(:block) { proc { raise 'error from task' } }
|
let(:block) { proc { throw :task_code } }
|
||||||
|
|
||||||
it 'evaluates its code' do
|
it 'evaluates its code' do
|
||||||
expect { dsl.evaluate(env) }
|
expect { dsl.evaluate(env) }
|
||||||
.to raise_error(RuntimeError, 'error from task')
|
.to throw_symbol :task_code
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when given block is invalid' do
|
context 'when given block is invalid' do
|
||||||
@ -26,23 +26,23 @@ module Producer::Core
|
|||||||
context 'when met (block evals to true)' do
|
context 'when met (block evals to true)' do
|
||||||
let(:block) { proc {
|
let(:block) { proc {
|
||||||
condition { true }
|
condition { true }
|
||||||
raise 'error after condition'
|
throw :after_condition
|
||||||
} }
|
} }
|
||||||
|
|
||||||
it 'evaluates all the block' do
|
it 'evaluates all the block' do
|
||||||
expect { dsl.evaluate(env) }
|
expect { dsl.evaluate(env) }
|
||||||
.to raise_error(RuntimeError, 'error after condition')
|
.to throw_symbol :after_condition
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when not met (block evals to false)' do
|
context 'when not met (block evals to false)' do
|
||||||
let(:block) { proc {
|
let(:block) { proc {
|
||||||
condition { false }
|
condition { false }
|
||||||
raise
|
throw :after_condition
|
||||||
} }
|
} }
|
||||||
|
|
||||||
it 'stops block evaluation' do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -2,3 +2,8 @@ require 'producer/core'
|
|||||||
|
|
||||||
require 'support/exit_helpers'
|
require 'support/exit_helpers'
|
||||||
require 'support/fixtures_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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user