Implement Worker class:

* Move recipe processing code in the worker;
* Refactor CLI and use the the worker;
* Implement Recipe#tasks and remove tasks application during evaluation,
  tasks are now applied by the worker after all evaluations are done.
This commit is contained in:
Thibault Jouan 2013-08-08 01:55:55 +00:00
parent ec44d01c36
commit 0904fa1fc9
9 changed files with 158 additions and 76 deletions

View File

@ -9,3 +9,4 @@ require 'producer/core/remote'
require 'producer/core/task' require 'producer/core/task'
require 'producer/core/task/dsl' require 'producer/core/task/dsl'
require 'producer/core/version' require 'producer/core/version'
require 'producer/core/worker'

View File

@ -12,16 +12,6 @@ module Producer
def run! def run!
check_arguments! check_arguments!
evaluate_recipe_file
end
def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 1
end
def evaluate_recipe_file
recipe = Recipe.from_file(@arguments.first)
env = Env.new(recipe)
begin begin
recipe.evaluate env recipe.evaluate env
rescue RecipeEvaluationError => e rescue RecipeEvaluationError => e
@ -31,6 +21,23 @@ module Producer
exit 70 exit 70
end end
worker.process recipe.tasks
end
def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 1
end
def env
@env ||= Env.new(recipe)
end
def recipe
@recipe ||= Recipe.from_file(@arguments.first)
end
def worker
@worker ||= Worker.new
end end
private private

View File

@ -1,7 +1,7 @@
module Producer module Producer
module Core module Core
class Recipe class Recipe
attr_reader :code, :filepath attr_reader :code, :filepath, :tasks
def self.from_file(filepath) def self.from_file(filepath)
new(File.read(filepath), filepath) new(File.read(filepath), filepath)
@ -10,11 +10,13 @@ module Producer
def initialize(code, filepath = nil) def initialize(code, filepath = nil)
@code = code @code = code
@filepath = filepath @filepath = filepath
@tasks = []
end end
def evaluate(env) def evaluate(env)
dsl = DSL.new(@code).evaluate(env) dsl = DSL.new(@code).evaluate(env)
dsl.tasks.map { |e| e.evaluate env } dsl.tasks.map { |e| e.evaluate env }
@tasks = dsl.tasks
end end
end end
end end

View File

@ -1,17 +1,18 @@
module Producer module Producer
module Core module Core
class Task class Task
attr_reader :name attr_reader :name, :actions
def initialize(name, &block) def initialize(name, &block)
@name = name @name = name
@block = block @block = block
@actions = []
end end
def evaluate(env) def evaluate(env)
dsl = DSL.new(&@block) dsl = DSL.new(&@block)
dsl.evaluate(env) dsl.evaluate(env)
dsl.actions.map(&:apply) @actions = dsl.actions
end end
end end
end end

View File

@ -0,0 +1,13 @@
module Producer
module Core
class Worker
def process(tasks)
tasks.each { |t| process_task t }
end
def process_task(task)
task.actions.each(&:apply)
end
end
end
end

View File

@ -27,10 +27,54 @@ module Producer::Core
cli.run! cli.run!
end end
it 'evaluates the recipe' do it 'evaluates the recipe with the environment' do
expect(cli).to receive(:evaluate_recipe_file) expect(cli.recipe).to receive(:evaluate).with(cli.env)
cli.run! cli.run!
end end
it 'processes the tasks with the worker' do
allow(cli.recipe).to receive(:tasks) { [:some_task] }
expect(cli.worker).to receive(:process).with([:some_task])
cli.run!
end
context 'when recipe evaluation fails' do
let(:recipe_file) { fixture_path_for('recipes/invalid.rb') }
let(:stdout) { StringIO.new }
subject(:cli) { CLI.new(arguments, stdout) }
context 'when error is known' do
it 'exits with a return status of 70' do
expect { cli.run! }
.to raise_error(SystemExit) { |e|
expect(e.status).to eq 70
}
end
it 'prints the specific error' do
trap_exit { cli.run! }
expect(stdout.string).to match(/
\A
#{recipe_file}:4:
.+
invalid\srecipe\skeyword\s`invalid_keyword'
/x)
end
it 'excludes producer own source code from the error backtrace' do
trap_exit { cli.run! }
expect(stdout.string).to_not match /\/producer-core\//
end
end
context 'when error is unknown (unexpected)' do
it 'lets the error be' do
UnexpectedError = Class.new(StandardError)
allow(cli.recipe).to receive(:evaluate).and_raise(UnexpectedError)
expect { cli.run! }.to raise_error(UnexpectedError)
end
end
end
end end
describe '#check_arguments!' do describe '#check_arguments!' do
@ -58,67 +102,43 @@ module Producer::Core
end end
end end
describe '#evaluate_recipe_file' do describe '#env' do
it 'builds an environment with the current recipe' do
expect(Env).to receive(:new).with(cli.recipe)
cli.env
end
it 'returns the env' do
env = double('env')
allow(Env).to receive(:new) { env }
expect(cli.env).to be env
end
end
describe '#recipe' do
it 'builds a recipe' do it 'builds a recipe' do
expect(Recipe) expect(Recipe)
.to receive(:from_file).with(recipe_file).and_call_original .to receive(:from_file).with(recipe_file)
cli.evaluate_recipe_file cli.recipe
end end
it 'builds an environment with the current recipe' do it 'returns the recipe' do
recipe = double('recipe').as_null_object
allow(Recipe).to receive(:from_file).and_return(recipe)
expect(Env).to receive(:new).with(recipe).and_call_original
cli.evaluate_recipe_file
end
it 'evaluates the recipe with the environment' do
recipe = double('recipe') recipe = double('recipe')
allow(Recipe).to receive(:from_file).and_return(recipe) allow(Recipe).to receive(:new) { recipe }
env = double('env') expect(cli.recipe).to be recipe
allow(Env).to receive(:new).and_return(env)
expect(recipe).to receive(:evaluate).with(env)
cli.evaluate_recipe_file
end
context 'when recipe evaluation fails' do
let(:recipe_file) { fixture_path_for('recipes/invalid.rb') }
let(:stdout) { StringIO.new }
subject(:cli) { CLI.new(arguments, stdout) }
context 'when error is known' do
it 'exits with a return status of 70' do
expect { cli.evaluate_recipe_file }
.to raise_error(SystemExit) { |e|
expect(e.status).to eq 70
}
end
it 'prints the specific error' do
trap_exit { cli.evaluate_recipe_file }
expect(stdout.string).to match(/
\A
#{recipe_file}:4:
.+
invalid\srecipe\skeyword\s`invalid_keyword'
/x)
end
it 'excludes producer own source code from the error backtrace' do
trap_exit { cli.evaluate_recipe_file }
expect(stdout.string).to_not match /\/producer-core\//
end end
end end
context 'when error is unknown (unexpected)' do describe '#worker' do
it 'lets the error be' do it 'builds a worker' do
UnexpectedError = Class.new(StandardError) expect(Worker).to receive(:new)
recipe = double('recipe') cli.worker
allow(Recipe).to receive(:from_file).and_return(recipe)
allow(recipe).to receive(:evaluate).and_raise(UnexpectedError)
expect { cli.evaluate_recipe_file }.to raise_error(UnexpectedError)
end
end end
it 'returns the worker' do
worker = double('worker')
allow(Worker).to receive(:new) { worker }
expect(cli.worker).to be worker
end end
end end
end end

View File

@ -24,6 +24,10 @@ module Producer::Core
it 'assigns nil as a default filepath' do it 'assigns nil as a default filepath' do
expect(recipe.filepath).to be nil expect(recipe.filepath).to be nil
end end
it 'has no task' do
expect(recipe.tasks).to be_empty
end
end end
describe '#code' do describe '#code' do
@ -64,6 +68,13 @@ module Producer::Core
expect(task).to receive(:evaluate).with(env) expect(task).to receive(:evaluate).with(env)
recipe.evaluate(env) recipe.evaluate(env)
end end
it 'assigns the evaluated tasks' do
dsl = Recipe::DSL.new { task(:some_task) { } }
allow(Recipe::DSL).to receive(:new) { dsl }
recipe.evaluate(env)
expect(recipe.tasks.first.name).to eq :some_task
end
end end
end end
end end

View File

@ -14,6 +14,10 @@ module Producer::Core
it 'assigns the block' do it 'assigns the block' do
expect(task.instance_eval { @block }).to be block expect(task.instance_eval { @block }).to be block
end end
it 'has no action' do
expect(task.actions).to be_empty
end
end end
describe '#name' do describe '#name' do
@ -37,13 +41,12 @@ module Producer::Core
task.evaluate(env) task.evaluate(env)
end end
it 'applies DSL sandbox actions' do it 'assigns the evaluated actions' do
dsl = double('task DSL').as_null_object dsl = double('dsl').as_null_object
allow(Task::DSL).to receive(:new) { dsl } allow(Task::DSL).to receive(:new) { dsl }
action = double('action') allow(dsl).to receive(:actions) { [:some_action] }
allow(dsl).to receive(:actions) { [action] }
expect(action).to receive(:apply)
task.evaluate(env) task.evaluate(env)
expect(task.actions).to eq [:some_action]
end end
end end
end end

View File

@ -0,0 +1,24 @@
require 'spec_helper'
module Producer::Core
describe Worker do
subject(:worker) { Worker.new }
describe '#process' do
it 'processes each task' do
expect(worker).to receive(:process_task).with(:some_task)
worker.process [:some_task]
end
end
describe '#process_task' do
it 'applies the task actions' do
action = double('action')
task = double('task')
allow(task).to receive(:actions) { [action] }
expect(action).to receive(:apply)
worker.process_task(task)
end
end
end
end