Refactor CLI usage into CLI.run!:
* Add Producer::Core::CLI::ArgumentError class; * Raise ArgumentError in CLI.new when recipe is missing; * Remove #check_arguments! and #print_usage_and_exit, move code into .run! method.
This commit is contained in:
parent
dc9e7f1412
commit
f7efd9c42f
@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
require 'producer/core'
|
require 'producer/core'
|
||||||
|
|
||||||
Producer::Core::CLI.new(ARGV).run!
|
Producer::Core::CLI.run!(ARGV)
|
||||||
|
@ -1,24 +1,34 @@
|
|||||||
module Producer
|
module Producer
|
||||||
module Core
|
module Core
|
||||||
class CLI
|
class CLI
|
||||||
|
ArgumentError = Class.new(::ArgumentError)
|
||||||
|
|
||||||
USAGE = "Usage: #{File.basename $0} recipe_file"
|
USAGE = "Usage: #{File.basename $0} recipe_file"
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def run!(arguments, output: $stderr)
|
||||||
|
begin
|
||||||
|
cli = new(arguments)
|
||||||
|
rescue ArgumentError
|
||||||
|
output.puts USAGE
|
||||||
|
exit 64
|
||||||
|
end
|
||||||
|
cli.run!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
attr_reader :arguments, :stdout
|
attr_reader :arguments, :stdout
|
||||||
|
|
||||||
def initialize(arguments, stdout: $stdout)
|
def initialize(arguments, stdout: $stdout)
|
||||||
|
raise ArgumentError unless arguments.any?
|
||||||
@arguments = arguments
|
@arguments = arguments
|
||||||
@stdout = stdout
|
@stdout = stdout
|
||||||
end
|
end
|
||||||
|
|
||||||
def run!
|
def run!
|
||||||
check_arguments!
|
|
||||||
interpreter.process recipe.tasks
|
interpreter.process recipe.tasks
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_arguments!
|
|
||||||
print_usage_and_exit(64) unless @arguments.length == 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def env
|
def env
|
||||||
@env ||= Env.new
|
@env ||= Env.new
|
||||||
end
|
end
|
||||||
@ -30,14 +40,6 @@ module Producer
|
|||||||
def interpreter
|
def interpreter
|
||||||
@interpreter ||= Interpreter.new
|
@interpreter ||= Interpreter.new
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def print_usage_and_exit(status)
|
|
||||||
@stdout.puts USAGE
|
|
||||||
|
|
||||||
exit status
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,12 +11,50 @@ module Producer::Core
|
|||||||
|
|
||||||
subject(:cli) { CLI.new(arguments, stdout: stdout) }
|
subject(:cli) { CLI.new(arguments, stdout: stdout) }
|
||||||
|
|
||||||
|
describe '.run!' do
|
||||||
|
let(:output) { StringIO.new }
|
||||||
|
subject(:run) { described_class.run! arguments, output: output }
|
||||||
|
|
||||||
|
it 'builds a new CLI with given arguments' do
|
||||||
|
expect(CLI).to receive(:new).with(arguments).and_call_original
|
||||||
|
run
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'runs the CLI' do
|
||||||
|
cli = double 'cli'
|
||||||
|
allow(CLI).to receive(:new) { cli }
|
||||||
|
expect(cli).to receive :run!
|
||||||
|
run
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when recipe argument is missing' do
|
||||||
|
let(:arguments) { [] }
|
||||||
|
|
||||||
|
it 'exits with a return status of 64' do
|
||||||
|
expect { run }.to raise_error(SystemExit) { |e|
|
||||||
|
expect(e.status).to eq 64
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'prints the usage' do
|
||||||
|
trap_exit { run }
|
||||||
|
expect(output.string).to match /\AUsage: .+/
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#initialize' do
|
describe '#initialize' do
|
||||||
subject(:cli) { CLI.new(arguments) }
|
subject(:cli) { CLI.new(arguments) }
|
||||||
|
|
||||||
it 'assigns $stdout as the default standard output' do
|
it 'assigns $stdout as the default standard output' do
|
||||||
expect(cli.stdout).to be $stdout
|
expect(cli.stdout).to be $stdout
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'without arguments' do
|
||||||
|
let(:arguments) { [] }
|
||||||
|
|
||||||
|
specify { expect { cli }.to raise_error described_class::ArgumentError }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#arguments' do
|
describe '#arguments' do
|
||||||
@ -32,11 +70,6 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '#run!' do
|
describe '#run!' do
|
||||||
it 'checks the arguments' do
|
|
||||||
expect(cli).to receive :check_arguments!
|
|
||||||
cli.run!
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'processes the tasks with the interpreter' do
|
it 'processes the tasks with the interpreter' do
|
||||||
allow(cli.recipe).to receive(:tasks) { [:some_task] }
|
allow(cli.recipe).to receive(:tasks) { [:some_task] }
|
||||||
expect(cli.interpreter).to receive(:process).with [:some_task]
|
expect(cli.interpreter).to receive(:process).with [:some_task]
|
||||||
@ -44,29 +77,6 @@ module Producer::Core
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#check_arguments!' do
|
|
||||||
context 'when recipe argument is provided' do
|
|
||||||
it 'does not raise any error' do
|
|
||||||
expect { cli.check_arguments! }.to_not raise_error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when recipe argument is missing' do
|
|
||||||
let(:arguments) { [] }
|
|
||||||
|
|
||||||
it 'exits with a return status of 64' do
|
|
||||||
expect { cli.check_arguments! }.to raise_error(SystemExit) { |e|
|
|
||||||
expect(e.status).to eq 64
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'prints the usage' do
|
|
||||||
trap_exit { cli.check_arguments! }
|
|
||||||
expect(stdout.string).to match /\AUsage: .+/
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#env' do
|
describe '#env' do
|
||||||
it 'builds an environment with the current recipe' do
|
it 'builds an environment with the current recipe' do
|
||||||
expect(Env).to receive :new
|
expect(Env).to receive :new
|
||||||
|
Loading…
x
Reference in New Issue
Block a user