From dc9e7f141221a20b7a2a6a8ac815c0cf70fbc0ef Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Tue, 7 Jan 2014 18:07:42 +0000 Subject: [PATCH] Improve CLI API related to standard output --- lib/producer/core/cli.rb | 6 +++--- spec/producer/core/cli_spec.rb | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/producer/core/cli.rb b/lib/producer/core/cli.rb index c6c39d8..abb5366 100644 --- a/lib/producer/core/cli.rb +++ b/lib/producer/core/cli.rb @@ -3,11 +3,11 @@ module Producer class CLI USAGE = "Usage: #{File.basename $0} recipe_file" - attr_reader :arguments + attr_reader :arguments, :stdout - def initialize(arguments, stdout = $stdout) - @stdout = stdout + def initialize(arguments, stdout: $stdout) @arguments = arguments + @stdout = stdout end def run! diff --git a/spec/producer/core/cli_spec.rb b/spec/producer/core/cli_spec.rb index a4c1d80..8b2a30d 100644 --- a/spec/producer/core/cli_spec.rb +++ b/spec/producer/core/cli_spec.rb @@ -7,11 +7,15 @@ module Producer::Core let(:recipe_file) { fixture_path_for 'recipes/empty.rb' } let(:arguments) { [recipe_file] } - subject(:cli) { CLI.new(arguments) } + let(:stdout) { StringIO.new } + + subject(:cli) { CLI.new(arguments, stdout: stdout) } describe '#initialize' do + subject(:cli) { CLI.new(arguments) } + it 'assigns $stdout as the default standard output' do - expect(cli.instance_eval { @stdout }).to be $stdout + expect(cli.stdout).to be $stdout end end @@ -21,6 +25,12 @@ module Producer::Core end end + describe '#stdout' do + it 'returns the assigned standard output' do + expect(cli.stdout).to be stdout + end + end + describe '#run!' do it 'checks the arguments' do expect(cli).to receive :check_arguments! @@ -43,8 +53,6 @@ module Producer::Core context 'when recipe argument is missing' do let(:arguments) { [] } - let(:stdout) { StringIO.new } - subject(:cli) { CLI.new(arguments, stdout) } it 'exits with a return status of 64' do expect { cli.check_arguments! }.to raise_error(SystemExit) { |e|