Refactor CLI
This commit is contained in:
parent
ea6875e9ee
commit
52e183b277
@ -25,7 +25,7 @@ module Producer
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :arguments, :stdin, :stdout, :stderr, :env
|
attr_reader :arguments, :env
|
||||||
|
|
||||||
def initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr)
|
def initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr)
|
||||||
@arguments = args
|
@arguments = args
|
||||||
@ -49,7 +49,7 @@ module Producer
|
|||||||
m
|
m
|
||||||
end
|
end
|
||||||
|
|
||||||
fail ArgumentError unless arguments.any?
|
fail ArgumentError unless @arguments.any?
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(worker: Worker.new(@env))
|
def run(worker: Worker.new(@env))
|
||||||
|
1
spec/fixtures/recipes/raise.rb
vendored
Normal file
1
spec/fixtures/recipes/raise.rb
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
fail Producer::Core::RemoteCommandExecutionError, 'false'
|
@ -8,82 +8,52 @@ 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(:stdout) { StringIO.new }
|
|
||||||
let(:stderr) { StringIO.new }
|
|
||||||
|
|
||||||
subject(:cli) { described_class.new(arguments,
|
subject(:cli) { described_class.new(arguments) }
|
||||||
stdin: stdin, stdout: stdout, stderr: stderr) }
|
|
||||||
|
|
||||||
describe '.run!' do
|
describe '.run!' do
|
||||||
let(:cli) { double('cli').as_null_object }
|
let(:stderr) { StringIO.new }
|
||||||
|
subject(:run!) { described_class.run! arguments, stderr: stderr }
|
||||||
|
|
||||||
subject(:run) do
|
context 'when given arguments are invalid' do
|
||||||
described_class.run! arguments,
|
let(:arguments) { [] }
|
||||||
stdin: stdin,
|
|
||||||
stdout: stdout,
|
|
||||||
stderr: stderr
|
|
||||||
end
|
|
||||||
|
|
||||||
before { allow(described_class).to receive(:new) { cli } }
|
|
||||||
|
|
||||||
it 'builds a new CLI with given arguments and streams' do
|
|
||||||
expect(described_class).to receive(:new).with(arguments,
|
|
||||||
stdin: stdin,
|
|
||||||
stdout: stdout,
|
|
||||||
stderr: stderr
|
|
||||||
)
|
|
||||||
run
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'runs the CLI' do
|
|
||||||
expect(cli).to receive :run
|
|
||||||
run
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'parses CLI arguments' do
|
|
||||||
expect(cli).to receive :parse_arguments!
|
|
||||||
run
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when an argument error is raised' do
|
|
||||||
before do
|
|
||||||
allow(cli).to receive(:parse_arguments!)
|
|
||||||
.and_raise described_class::ArgumentError
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'exits with a return status of 64' do
|
it 'exits with a return status of 64' do
|
||||||
expect { run }.to raise_error(SystemExit) do |e|
|
expect { run! }.to raise_error(SystemExit) do |e|
|
||||||
expect(e.status).to eq 64
|
expect(e.status).to eq 64
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'prints the usage on the error stream' do
|
it 'prints the usage on the error stream' do
|
||||||
trap_exit { run }
|
trap_exit { run! }
|
||||||
expect(stderr.string).to match /\AUsage: .+/
|
expect(stderr.string).to match /\AUsage: .+/
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a runtime error is raised' do
|
context 'when a runtime error is raised' do
|
||||||
before do
|
let(:recipe_file) { fixture_path_for 'recipes/raise.rb' }
|
||||||
allow(cli).to receive(:run)
|
|
||||||
.and_raise RuntimeError, 'some message'
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'exits with a return status of 70' do
|
it 'exits with a return status of 70' do
|
||||||
expect { run }.to raise_error(SystemExit) do |e|
|
expect { run! }.to raise_error(SystemExit) do |e|
|
||||||
expect(e.status).to eq 70
|
expect(e.status).to eq 70
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'prints exception name and message and the error stream' do
|
it 'prints exception name and message and the error stream' do
|
||||||
trap_exit { run }
|
trap_exit { run! }
|
||||||
expect(stderr.string).to match /\ARuntimeError: some message/
|
expect(stderr.string).to eq "RemoteCommandExecutionError: false\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#env' do
|
describe '#env' do
|
||||||
|
let(:stdin) { StringIO.new }
|
||||||
|
let(:stdout) { StringIO.new }
|
||||||
|
let(:stderr) { StringIO.new }
|
||||||
|
|
||||||
|
subject(:cli) { described_class.new(arguments,
|
||||||
|
stdin: stdin, stdout: stdout, stderr: stderr) }
|
||||||
|
|
||||||
it 'returns an env' do
|
it 'returns an env' do
|
||||||
expect(cli.env).to be_an Env
|
expect(cli.env).to be_an Env
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user