Rewrite CLI#run:

* Rename #run! to #run;
* Add #load_recipe method;
* Remove most builder/accessor/memoizable methods.
This commit is contained in:
Thibault Jouan 2014-01-07 22:19:52 +00:00
parent 4d34f66ec5
commit 7dfbc3905b
3 changed files with 38 additions and 53 deletions

View File

@ -15,11 +15,12 @@ module Producer
output.puts USAGE output.puts USAGE
exit EX_USAGE exit EX_USAGE
end end
cli.run! cli.run
end end
end end
attr_reader :arguments, :stdout attr_reader :arguments, :stdout
attr_accessor :recipe
def initialize(arguments, stdout: $stdout) def initialize(arguments, stdout: $stdout)
raise ArgumentError unless arguments.any? raise ArgumentError unless arguments.any?
@ -27,20 +28,13 @@ module Producer
@stdout = stdout @stdout = stdout
end end
def run! def run(worker: Interpreter.new)
interpreter.process recipe.tasks load_recipe
worker.process recipe.tasks
end end
def env def load_recipe
@env ||= Env.new @recipe = Recipe.evaluate_from_file(@arguments.first, Env.new)
end
def recipe
@recipe ||= Recipe.evaluate_from_file(@arguments.first, env)
end
def interpreter
@interpreter ||= Interpreter.new
end end
end end
end end

5
spec/fixtures/recipes/some_recipe.rb vendored Normal file
View File

@ -0,0 +1,5 @@
task :some_task do
end
task :another_task do
end

View File

@ -5,7 +5,7 @@ module Producer::Core
include ExitHelpers include ExitHelpers
include FixturesHelpers include FixturesHelpers
let(:recipe_file) { fixture_path_for 'recipes/empty.rb' } let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' }
let(:arguments) { [recipe_file] } let(:arguments) { [recipe_file] }
let(:stdout) { StringIO.new } let(:stdout) { StringIO.new }
@ -23,7 +23,7 @@ module Producer::Core
it 'runs the CLI' do it 'runs the CLI' do
cli = double 'cli' cli = double 'cli'
allow(CLI).to receive(:new) { cli } allow(CLI).to receive(:new) { cli }
expect(cli).to receive :run! expect(cli).to receive :run
run run
end end
@ -69,51 +69,37 @@ module Producer::Core
end end
end end
describe '#run!' do
it 'processes the tasks with the interpreter' do
allow(cli.recipe).to receive(:tasks) { [:some_task] }
expect(cli.interpreter).to receive(:process).with [:some_task]
cli.run!
end
end
describe '#env' do
it 'builds an environment with the current recipe' do
expect(Env).to receive :new
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 describe '#recipe' do
it 'builds a recipe' do it 'returns the assigned recipe' do
expect(Recipe) recipe = double 'recipe'
.to receive(:evaluate_from_file).with(recipe_file, cli.env) cli.recipe = recipe
cli.recipe
end
it 'returns the recipe' do
recipe = double('recipe').as_null_object
allow(Recipe).to receive(:new) { recipe }
expect(cli.recipe).to be recipe expect(cli.recipe).to be recipe
end end
end end
describe '#interpreter' do describe '#run' do
it 'builds a interpreter' do it 'loads the recipe' do
expect(Interpreter).to receive :new cli.run
cli.interpreter expect(cli.recipe).to be_a Recipe
end end
it 'returns the interpreter' do it 'process recipe tasks with given worker' do
interpreter = double 'interpreter' worker = double 'worker'
allow(Interpreter).to receive(:new) { interpreter } expect(worker).to receive(:process).with [kind_of(Task), kind_of(Task)]
expect(cli.interpreter).to be interpreter cli.run worker: worker
end
end
describe '#load_recipe' do
it 'evaluates the recipe file with an env' do
expect(Recipe)
.to receive(:evaluate_from_file).with(recipe_file, kind_of(Env))
cli.load_recipe
end
it 'assigns the evaluated recipe' do
cli.load_recipe
expect(cli.recipe.tasks.count).to be 2
end end
end end
end end