From 52e183b277f35f7e50f1a8faa2ea15e383ba1e89 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Mon, 22 Sep 2014 13:00:50 +0000 Subject: [PATCH] Refactor CLI --- lib/producer/core/cli.rb | 4 +-- spec/fixtures/recipes/raise.rb | 1 + spec/producer/core/cli_spec.rb | 66 ++++++++++------------------------ 3 files changed, 21 insertions(+), 50 deletions(-) create mode 100644 spec/fixtures/recipes/raise.rb diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index 1417203..a0de5fa 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -25,7 +25,7 @@ module Producer end end - attr_reader :arguments, :stdin, :stdout, :stderr, :env + attr_reader :arguments, :env def initialize(args, stdin: $stdin, stdout: $stdout, stderr: $stderr) @arguments = args @@ -49,7 +49,7 @@ module Producer m end - fail ArgumentError unless arguments.any? + fail ArgumentError unless @arguments.any? end def run(worker: Worker.new(@env)) diff --git a/spec/fixtures/recipes/raise.rb b/spec/fixtures/recipes/raise.rb new file mode 100644 index 0000000..779f17d --- /dev/null +++ b/spec/fixtures/recipes/raise.rb @@ -0,0 +1 @@ +fail Producer::Core::RemoteCommandExecutionError, 'false' diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index 14680d0..0499e0f 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -8,82 +8,52 @@ 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(: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) } 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 - described_class.run! 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 + context 'when given arguments are invalid' do + let(:arguments) { [] } 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 end end it 'prints the usage on the error stream' do - trap_exit { run } + trap_exit { run! } expect(stderr.string).to match /\AUsage: .+/ end end context 'when a runtime error is raised' do - before do - allow(cli).to receive(:run) - .and_raise RuntimeError, 'some message' - end + let(:recipe_file) { fixture_path_for 'recipes/raise.rb' } 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 end end it 'prints exception name and message and the error stream' do - trap_exit { run } - expect(stderr.string).to match /\ARuntimeError: some message/ + trap_exit { run! } + expect(stderr.string).to eq "RemoteCommandExecutionError: false\n" end end end 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 expect(cli.env).to be_an Env end