From ea6875e9ee6e054af6ed82856687581e1abe6f2e Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 22 Sep 2014 12:30:50 +0000 Subject: [PATCH] Refactor CLI --- lib/producer/core/cli.rb | 14 +++---- spec/producer/core/cli_spec.rb | 68 +++++++--------------------------- 2 files changed, 19 insertions(+), 63 deletions(-) diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index 121c846..1417203 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -52,17 +52,13 @@ module Producer fail ArgumentError unless arguments.any? end - def run - worker.process load_recipe.tasks - env.cleanup + def run(worker: Worker.new(@env)) + worker.process recipe.tasks + @env.cleanup end - def load_recipe - Recipe.evaluate_from_file(@arguments.first, env) - end - - def worker - Worker.new(env) + def recipe + @recipe ||= Recipe.evaluate_from_file(@arguments.first, env) end end end diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index 7281d95..14680d0 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -8,13 +8,12 @@ module Producer::Core let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' } let(:options) { [] } let(:arguments) { [*options, recipe_file] } - let(:stdin) { StringIO.new} + let(:stdin) { StringIO.new } let(:stdout) { StringIO.new } let(:stderr) { StringIO.new } - subject(:cli) { described_class.new( - arguments, - stdin: stdin, stdout: stdout, stderr: stderr) } + subject(:cli) { described_class.new(arguments, + stdin: stdin, stdout: stdout, stderr: stderr) } describe '.run!' do let(:cli) { double('cli').as_null_object } @@ -84,32 +83,8 @@ module Producer::Core end end - describe '#initialize' do - subject(:cli) { described_class.new(arguments) } - - it 'assigns $stdin as the default standard input' do - expect(cli.stdin).to be $stdin - end - - it 'assigns $stdout as the default standard output' do - expect(cli.stdout).to be $stdout - end - end - - describe '#arguments' do - it 'returns the assigned arguments' do - expect(cli.arguments).to eq arguments - end - end - - describe '#stdout' do - it 'returns the assigned standard output' do - expect(cli.stdout).to be stdout - end - end - describe '#env' do - it 'returns the assigned env' do + it 'returns an env' do expect(cli.env).to be_an Env end @@ -171,12 +146,10 @@ module Producer::Core end describe '#run' do - it 'process recipe tasks with given worker' do - worker = double 'worker' - allow(cli).to receive(:worker) { worker } - expect(worker).to receive(:process) - .with([an_instance_of(Task), an_instance_of(Task)]) - cli.run + it 'processes recipe tasks with a worker' do + worker = instance_spy Worker + cli.run worker: worker + expect(worker).to have_received(:process).with cli.recipe.tasks end it 'cleans up the env' do @@ -185,25 +158,12 @@ module Producer::Core end end - describe '#load_recipe' do - it 'evaluates the recipe file with the CLI env' do - expect(Recipe) - .to receive(:evaluate_from_file).with(recipe_file, cli.env) - cli.load_recipe - end - - it 'returns the recipe' do - expect(cli.load_recipe).to be_a Recipe - end - end - - describe '#worker' do - it 'returns a worker' do - expect(cli.worker).to be_a Worker - end - - it 'assigns the CLI env' do - expect(cli.worker.env).to eq cli.env + describe '#recipe' do + it 'returns the evaluated recipe' do + expect(cli.recipe.tasks).to match [ + an_object_having_attributes(name: :some_task), + an_object_having_attributes(name: :another_task) + ] end end end