Refactor and simplify recipe DSL evaluation usages:

Remove most of recipe evaluation code in Recipe class, and rely on
Recipe::DSL to get evaluated recipes.

* Remove Recipe#evaluate call from CLI, rely on
  Recipe.evaluate_from_file to get the evaluated recipe;
* Implement Recipe.evaluate_from_file(filepath, env);
* Implement Recipe::DSL.evaluate(code, env);
* Remove code and filepath accessor on Recipe;
* Remove Recipe.from_file and Recipe#evaluate methods;
* Move task evaluations in Recipe::DSL#evaluate;
* Modify Recipe constructor so that it accepts tasks as argument.
This commit is contained in:
Thibault Jouan
2013-08-10 15:36:27 +00:00
parent 7c5d5b0417
commit f0e144cebd
6 changed files with 77 additions and 85 deletions

View File

@@ -8,6 +8,35 @@ module Producer::Core
let(:env) { double('env').as_null_object }
subject(:dsl) { Recipe::DSL.new &code }
describe '.evaluate' do
let(:code) { 'nil' }
it 'builds a new DSL sandbox with given code' do
expect(Recipe::DSL).to receive(:new).once.with(code).and_call_original
Recipe::DSL.evaluate(code, env)
end
it 'evaluates the DSL sandbox code with given environment' do
dsl = double('dsl').as_null_object
allow(Recipe::DSL).to receive(:new) { dsl }
expect(dsl).to receive(:evaluate).with(env)
Recipe::DSL.evaluate(code, env)
end
it 'builds a recipe with evaluated tasks' do
dsl = Recipe::DSL.new('task(:some_task) { }')
allow(Recipe::DSL).to receive(:new) { dsl }
expect(Recipe).to receive(:new).with(dsl.tasks)
Recipe::DSL.evaluate(code, env)
end
it 'returns the recipe' do
recipe = double('recipe').as_null_object
allow(Recipe).to receive(:new) { recipe }
expect(Recipe::DSL.evaluate(code, env)).to be recipe
end
end
describe '#initialize' do
it 'assigns no task' do
expect(dsl.instance_eval { @tasks }).to be_empty
@@ -30,7 +59,7 @@ module Producer::Core
end
describe '#tasks' do
let(:code) { proc { task(:some_task) } }
let(:code) { proc { task(:some_task) { } } }
it 'returns registered tasks' do
dsl.evaluate(env)
@@ -44,6 +73,14 @@ module Producer::Core
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
end
it 'evaluates the registered tasks' do
task = double('task')
allow(Task).to receive(:new) { task }
dsl = Recipe::DSL.new { task(:some_task) }
expect(task).to receive(:evaluate).with(env)
dsl.evaluate(env)
end
it 'returns itself' do
expect(dsl.evaluate(env)).to eq dsl
end
@@ -81,11 +118,11 @@ module Producer::Core
end
describe '#task' do
let(:code) { proc { task(:first) { throw :first_task }; task(:last) } }
let(:code) { proc { task(:first) { :some_value }; task(:last) { } } }
it 'register a task with its code' do
expect(dsl.tasks.first.name).to eq :first
expect { dsl.tasks.first.evaluate(env) }.to throw_symbol :first_task
expect(dsl.tasks.first.instance_eval { @block.call })
.to eq :some_value
end
it 'registers tasks in declaration order' do