diff --git a/lib/producer/core/actions/echo.rb b/lib/producer/core/actions/echo.rb index 88454ed..67da13b 100644 --- a/lib/producer/core/actions/echo.rb +++ b/lib/producer/core/actions/echo.rb @@ -3,7 +3,7 @@ module Producer module Actions class Echo < Action def apply - env.output arguments.first + env.output.puts arguments.first end end end diff --git a/lib/producer/core/actions/shell_command.rb b/lib/producer/core/actions/shell_command.rb index 6836878..2117783 100644 --- a/lib/producer/core/actions/shell_command.rb +++ b/lib/producer/core/actions/shell_command.rb @@ -3,7 +3,7 @@ module Producer module Actions class ShellCommand < Action def apply - env.output env.remote.execute(arguments.first) + env.output.puts env.remote.execute(arguments.first) end end end diff --git a/lib/producer/core/env.rb b/lib/producer/core/env.rb index a1e29f6..183d195 100644 --- a/lib/producer/core/env.rb +++ b/lib/producer/core/env.rb @@ -1,18 +1,13 @@ module Producer module Core class Env - attr_writer :output - attr_accessor :target + attr_accessor :output, :target - def initialize - @output = $stdout + def initialize(output: $stdout) + @output = output @target = nil end - def output(str) - @output.puts str - end - def remote @remote ||= Remote.new(target) end diff --git a/spec/producer/core/actions/echo_spec.rb b/spec/producer/core/actions/echo_spec.rb index 0fc02d7..af4d545 100644 --- a/spec/producer/core/actions/echo_spec.rb +++ b/spec/producer/core/actions/echo_spec.rb @@ -2,14 +2,14 @@ require 'spec_helper' module Producer::Core describe Actions::Echo do - let(:env) { double 'env' } + let(:env) { Env.new(output: StringIO.new) } let(:text) { 'hello' } subject(:echo) { Actions::Echo.new(env, text) } describe '#apply' do - it 'outputs the string given as argument through env.output' do - expect(env).to receive(:output).with(text) + it 'writes the given string to env.output with a record separator' do echo.apply + expect(env.output.string).to eq "hello\n" end end end diff --git a/spec/producer/core/actions/shell_command_spec.rb b/spec/producer/core/actions/shell_command_spec.rb index 8022bf4..4edb571 100644 --- a/spec/producer/core/actions/shell_command_spec.rb +++ b/spec/producer/core/actions/shell_command_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' module Producer::Core describe Actions::ShellCommand do - let(:env) { Env.new } + let(:env) { Env.new(output: StringIO.new) } let(:command_args) { 'hello from remote host' } let(:command) { "echo #{command_args}" } subject(:sh) { Actions::ShellCommand.new(env, command) } @@ -15,10 +15,10 @@ module Producer::Core sh.apply end - it 'forwards the returned output to env.output' do + it 'writes the returned output to env.output with a record separator' do allow(env.remote).to receive(:execute) { command_args } - expect(env).to receive(:output).with(command_args) sh.apply + expect(env.output.string).to eq "#{command_args}\n" end end end diff --git a/spec/producer/core/env_spec.rb b/spec/producer/core/env_spec.rb index ead1f6c..e921254 100644 --- a/spec/producer/core/env_spec.rb +++ b/spec/producer/core/env_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' module Producer::Core describe Env do + let(:output) { double 'output' } subject(:env) { Env.new } describe '#initialize' do @@ -12,15 +13,21 @@ module Producer::Core it 'assigns no default target' do expect(env.target).not_to be end + + context 'when output is given as argument' do + subject(:env) { Env.new(output: output) } + + it 'assigns the given output' do + expect(env.instance_eval { @output }).to eq output + end + end end describe '#output' do - let(:stdout) { StringIO.new } + subject(:env) { Env.new(output: output) } - it 'writes the given string to the assigned IO with a record separator' do - env.output = stdout - env.output 'some content' - expect(stdout.string).to eq "some content\n" + it 'returns the assigned output' do + expect(env.output).to eq output end end