diff --git a/spec/fixtures/recipes/error.rb b/spec/fixtures/recipes/error.rb index 1c3b23b..6ba9843 100644 --- a/spec/fixtures/recipes/error.rb +++ b/spec/fixtures/recipes/error.rb @@ -1 +1 @@ -raise 'error from recipe' +raise SomeErrorInRecipe, 'some recipe error' diff --git a/spec/fixtures/recipes/throw.rb b/spec/fixtures/recipes/throw.rb new file mode 100644 index 0000000..11fa6e9 --- /dev/null +++ b/spec/fixtures/recipes/throw.rb @@ -0,0 +1 @@ +throw :recipe_code diff --git a/spec/producer/core/recipe/dsl_spec.rb b/spec/producer/core/recipe/dsl_spec.rb index aacc551..5cd49b6 100644 --- a/spec/producer/core/recipe/dsl_spec.rb +++ b/spec/producer/core/recipe/dsl_spec.rb @@ -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 diff --git a/spec/producer/core/task/dsl_spec.rb b/spec/producer/core/task/dsl_spec.rb index cfc2383..bf7cc37 100644 --- a/spec/producer/core/task/dsl_spec.rb +++ b/spec/producer/core/task/dsl_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 95f6163..b96e18e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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)