Refactor CLI
This commit is contained in:
parent
9cefb62f15
commit
ea6875e9ee
@ -52,17 +52,13 @@ module Producer
|
|||||||
fail ArgumentError unless arguments.any?
|
fail ArgumentError unless arguments.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run(worker: Worker.new(@env))
|
||||||
worker.process load_recipe.tasks
|
worker.process recipe.tasks
|
||||||
env.cleanup
|
@env.cleanup
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_recipe
|
def recipe
|
||||||
Recipe.evaluate_from_file(@arguments.first, env)
|
@recipe ||= Recipe.evaluate_from_file(@arguments.first, env)
|
||||||
end
|
|
||||||
|
|
||||||
def worker
|
|
||||||
Worker.new(env)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,12 +8,11 @@ module Producer::Core
|
|||||||
let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' }
|
let(:recipe_file) { fixture_path_for 'recipes/some_recipe.rb' }
|
||||||
let(:options) { [] }
|
let(:options) { [] }
|
||||||
let(:arguments) { [*options, recipe_file] }
|
let(:arguments) { [*options, recipe_file] }
|
||||||
let(:stdin) { StringIO.new}
|
let(:stdin) { StringIO.new }
|
||||||
let(:stdout) { StringIO.new }
|
let(:stdout) { StringIO.new }
|
||||||
let(:stderr) { StringIO.new }
|
let(:stderr) { StringIO.new }
|
||||||
|
|
||||||
subject(:cli) { described_class.new(
|
subject(:cli) { described_class.new(arguments,
|
||||||
arguments,
|
|
||||||
stdin: stdin, stdout: stdout, stderr: stderr) }
|
stdin: stdin, stdout: stdout, stderr: stderr) }
|
||||||
|
|
||||||
describe '.run!' do
|
describe '.run!' do
|
||||||
@ -84,32 +83,8 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
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
|
describe '#env' do
|
||||||
it 'returns the assigned env' do
|
it 'returns an env' do
|
||||||
expect(cli.env).to be_an Env
|
expect(cli.env).to be_an Env
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -171,12 +146,10 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#run' do
|
describe '#run' do
|
||||||
it 'process recipe tasks with given worker' do
|
it 'processes recipe tasks with a worker' do
|
||||||
worker = double 'worker'
|
worker = instance_spy Worker
|
||||||
allow(cli).to receive(:worker) { worker }
|
cli.run worker: worker
|
||||||
expect(worker).to receive(:process)
|
expect(worker).to have_received(:process).with cli.recipe.tasks
|
||||||
.with([an_instance_of(Task), an_instance_of(Task)])
|
|
||||||
cli.run
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'cleans up the env' do
|
it 'cleans up the env' do
|
||||||
@ -185,25 +158,12 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#load_recipe' do
|
describe '#recipe' do
|
||||||
it 'evaluates the recipe file with the CLI env' do
|
it 'returns the evaluated recipe' do
|
||||||
expect(Recipe)
|
expect(cli.recipe.tasks).to match [
|
||||||
.to receive(:evaluate_from_file).with(recipe_file, cli.env)
|
an_object_having_attributes(name: :some_task),
|
||||||
cli.load_recipe
|
an_object_having_attributes(name: :another_task)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user