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
class Echo < Action
def apply
env.output arguments.first
env.output.puts arguments.first
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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