Replace env.output interface with an IO like object

This commit is contained in:
Thibault Jouan 2013-12-23 00:50:25 +00:00
parent e8b0900721
commit d670d5dbdd
6 changed files with 23 additions and 21 deletions

View File

@ -3,7 +3,7 @@ module Producer
module Actions module Actions
class Echo < Action class Echo < Action
def apply def apply
env.output arguments.first env.output.puts arguments.first
end end
end end
end end

View File

@ -3,7 +3,7 @@ module Producer
module Actions module Actions
class ShellCommand < Action class ShellCommand < Action
def apply def apply
env.output env.remote.execute(arguments.first) env.output.puts env.remote.execute(arguments.first)
end end
end end
end end

View File

@ -1,18 +1,13 @@
module Producer module Producer
module Core module Core
class Env class Env
attr_writer :output attr_accessor :output, :target
attr_accessor :target
def initialize def initialize(output: $stdout)
@output = $stdout @output = output
@target = nil @target = nil
end end
def output(str)
@output.puts str
end
def remote def remote
@remote ||= Remote.new(target) @remote ||= Remote.new(target)
end end

View File

@ -2,14 +2,14 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::Echo do describe Actions::Echo do
let(:env) { double 'env' } let(:env) { Env.new(output: StringIO.new) }
let(:text) { 'hello' } let(:text) { 'hello' }
subject(:echo) { Actions::Echo.new(env, text) } subject(:echo) { Actions::Echo.new(env, text) }
describe '#apply' do describe '#apply' do
it 'outputs the string given as argument through env.output' do it 'writes the given string to env.output with a record separator' do
expect(env).to receive(:output).with(text)
echo.apply echo.apply
expect(env.output.string).to eq "hello\n"
end end
end end
end end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Actions::ShellCommand do describe Actions::ShellCommand do
let(:env) { Env.new } let(:env) { Env.new(output: StringIO.new) }
let(:command_args) { 'hello from remote host' } let(:command_args) { 'hello from remote host' }
let(:command) { "echo #{command_args}" } let(:command) { "echo #{command_args}" }
subject(:sh) { Actions::ShellCommand.new(env, command) } subject(:sh) { Actions::ShellCommand.new(env, command) }
@ -15,10 +15,10 @@ module Producer::Core
sh.apply sh.apply
end 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 } allow(env.remote).to receive(:execute) { command_args }
expect(env).to receive(:output).with(command_args)
sh.apply sh.apply
expect(env.output.string).to eq "#{command_args}\n"
end end
end end
end end

View File

@ -2,6 +2,7 @@ require 'spec_helper'
module Producer::Core module Producer::Core
describe Env do describe Env do
let(:output) { double 'output' }
subject(:env) { Env.new } subject(:env) { Env.new }
describe '#initialize' do describe '#initialize' do
@ -12,15 +13,21 @@ module Producer::Core
it 'assigns no default target' do it 'assigns no default target' do
expect(env.target).not_to be expect(env.target).not_to be
end 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 end
describe '#output' do 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 it 'returns the assigned output' do
env.output = stdout expect(env.output).to eq output
env.output 'some content'
expect(stdout.string).to eq "some content\n"
end end
end end