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:
Thibault Jouan
2014-01-07 21:02:00 +00:00
parent dc9e7f1412
commit f7efd9c42f
3 changed files with 54 additions and 42 deletions

View File

@@ -11,12 +11,50 @@ module Producer::Core
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
subject(:cli) { CLI.new(arguments) }
it 'assigns $stdout as the default standard output' do
expect(cli.stdout).to be $stdout
end
context 'without arguments' do
let(:arguments) { [] }
specify { expect { cli }.to raise_error described_class::ArgumentError }
end
end
describe '#arguments' do
@@ -32,11 +70,6 @@ module Producer::Core
end
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
allow(cli.recipe).to receive(:tasks) { [:some_task] }
expect(cli.interpreter).to receive(:process).with [:some_task]
@@ -44,29 +77,6 @@ module Producer::Core
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
it 'builds an environment with the current recipe' do
expect(Env).to receive :new