Attach our env to the worker from the CLI

This commit is contained in:
Thibault Jouan 2014-05-19 23:02:23 +00:00
parent 024ab7eba0
commit 7cffa34442
4 changed files with 52 additions and 12 deletions

View File

@ -19,21 +19,26 @@ module Producer
end
end
attr_reader :arguments, :stdout, :recipe
attr_reader :arguments, :stdout, :env, :recipe
def initialize(arguments, stdout: $stdout)
raise ArgumentError unless arguments.any?
@arguments = arguments
def initialize(args, stdout: $stdout)
@arguments = args
@stdout = stdout
@env = Env.new(output: stdout)
raise ArgumentError unless arguments.any?
end
def run(worker: Worker.new)
def run(worker: build_worker)
load_recipe
worker.process recipe.tasks
end
def load_recipe
@recipe = Recipe.evaluate_from_file(@arguments.first, Env.new)
@recipe = Recipe.evaluate_from_file(@arguments.first, env)
end
def build_worker
Worker.new(env)
end
end
end

View File

@ -1,6 +1,12 @@
module Producer
module Core
class Worker
attr_accessor :env
def initialize(env)
@env = env
end
def process(tasks)
tasks.each { |t| process_task t }
end

View File

@ -44,10 +44,16 @@ module Producer::Core
end
describe '#initialize' do
subject(:cli) { CLI.new(arguments) }
it 'assigns the env with CLI output' do
expect(cli.env.output).to be stdout
end
it 'assigns $stdout as the default standard output' do
expect(cli.stdout).to be $stdout
context 'without options' do
subject(:cli) { CLI.new(arguments) }
it 'assigns $stdout as the default standard output' do
expect(cli.stdout).to be $stdout
end
end
context 'without arguments' do
@ -84,10 +90,16 @@ module Producer::Core
end
end
describe '#env' do
it 'returns an env' do
expect(cli.env).to be_an Env
end
end
describe '#load_recipe' do
it 'evaluates the recipe file with an env' do
it 'evaluates the recipe file with the CLI env' do
expect(Recipe)
.to receive(:evaluate_from_file).with(recipe_file, kind_of(Env))
.to receive(:evaluate_from_file).with(recipe_file, cli.env)
cli.load_recipe
end
@ -96,5 +108,15 @@ module Producer::Core
expect(cli.recipe.tasks.count).to be 2
end
end
describe '#build_worker' do
it 'returns a worker' do
expect(cli.build_worker).to be_a Worker
end
it 'assigns the CLI env' do
expect(cli.build_worker.env).to eq cli.env
end
end
end
end

View File

@ -2,7 +2,8 @@ require 'spec_helper'
module Producer::Core
describe Worker do
subject(:worker) { described_class.new }
let(:env) { Env.new }
subject(:worker) { described_class.new(env) }
describe '#process' do
it 'processes each task' do
@ -31,5 +32,11 @@ module Producer::Core
end
end
end
describe '#env' do
it 'returns the assigned env' do
expect(worker.env).to be env
end
end
end
end