Implement tasks condition feature
This commit is contained in:
		
							
								
								
									
										15
									
								
								features/condition.feature
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								features/condition.feature
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| Feature: condition | ||||
|  | ||||
|   Scenario: prevents task evaluation when condition is not met | ||||
|     Given a recipe with: | ||||
|       """ | ||||
|       task :hello do | ||||
|         condition { false } | ||||
|  | ||||
|         puts 'evaluated' | ||||
|         exit 70 | ||||
|       end | ||||
|       """ | ||||
|     When I execute the recipe | ||||
|     Then the exit status must be 0 | ||||
|     And the output must not contain "evaluated" | ||||
| @@ -9,7 +9,21 @@ module Producer | ||||
|       end | ||||
|  | ||||
|       def evaluate | ||||
|         instance_eval &@block | ||||
|         DSL.new &@block | ||||
|       end | ||||
|  | ||||
|  | ||||
|       class DSL | ||||
|         ConditionNotMetError = Class.new(RuntimeError) | ||||
|  | ||||
|         def initialize(&block) | ||||
|           instance_eval &block | ||||
|         rescue ConditionNotMetError | ||||
|         end | ||||
|  | ||||
|         def condition(&block) | ||||
|           raise ConditionNotMetError unless block.call | ||||
|         end | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|   | ||||
| @@ -3,7 +3,7 @@ require 'spec_helper' | ||||
| module Producer::Core | ||||
|   describe Task do | ||||
|     let(:name)      { :some_task } | ||||
|     let(:block)     { } | ||||
|     let(:block)     { Proc.new { } } | ||||
|     subject(:task)  { Task.new(name, &block) } | ||||
|  | ||||
|     describe '#name' do | ||||
| @@ -12,11 +12,46 @@ module Producer::Core | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     describe 'evaluate' do | ||||
|     describe '#evaluate' do | ||||
|       it 'builds a task DSL sandbox' do | ||||
|         expect(Task::DSL).to receive(:new).with(&block) | ||||
|         task.evaluate | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     describe Task::DSL do | ||||
|       let(:dsl) { Task::DSL.new &block } | ||||
|  | ||||
|       describe '#initialize' do | ||||
|         let(:block) { Proc.new { raise 'error from task' } } | ||||
|  | ||||
|         it 'evaluates its block' do | ||||
|         expect { task.evaluate }.to raise_error(RuntimeError, 'error from task') | ||||
|           expect { dsl }.to raise_error(RuntimeError, 'error from task') | ||||
|         end | ||||
|       end | ||||
|  | ||||
|       describe '#condition' do | ||||
|         context 'condition is met (block evals to true)' do | ||||
|           let(:block) { Proc.new { | ||||
|             condition { true } | ||||
|             raise 'error after condition' | ||||
|           } } | ||||
|  | ||||
|           it 'evaluates all the block' do | ||||
|             expect { dsl }.to raise_error(RuntimeError, 'error after condition') | ||||
|           end | ||||
|         end | ||||
|  | ||||
|         context 'condition is not met (block evals to false)' do | ||||
|           let(:block) { Proc.new { | ||||
|             condition { false } | ||||
|             raise | ||||
|           } } | ||||
|  | ||||
|           it 'evaluates all the block' do | ||||
|             expect { dsl }.not_to raise_error | ||||
|           end | ||||
|         end | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user