From db3644a3e694a9d262c6a98677244026c1752a62 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Wed, 5 Mar 2014 06:06:42 +0000 Subject: [PATCH] Write output from `sh' task action directly: * Accept output as argument in Remote#execute; * Modify ShellCommand so that env output is directly handled. --- lib/producer/core/actions/shell_command.rb | 3 ++- lib/producer/core/remote.rb | 3 +-- lib/producer/core/testing/mock_remote.rb | 8 +++++--- spec/producer/core/remote_spec.rb | 12 +++++++++++- spec/support/test_env_helpers.rb | 4 +++- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/producer/core/actions/shell_command.rb b/lib/producer/core/actions/shell_command.rb index 9a12656..a578b48 100644 --- a/lib/producer/core/actions/shell_command.rb +++ b/lib/producer/core/actions/shell_command.rb @@ -3,7 +3,8 @@ module Producer module Actions class ShellCommand < Action def apply - output.puts remote.execute(arguments.first) + remote.execute(arguments.first, output) + output.puts end end end diff --git a/lib/producer/core/remote.rb b/lib/producer/core/remote.rb index bb11a22..c654e72 100644 --- a/lib/producer/core/remote.rb +++ b/lib/producer/core/remote.rb @@ -23,8 +23,7 @@ module Producer @fs ||= Remote::FS.new(session.sftp.connect) end - def execute(command) - output = '' + def execute(command, output = '') channel = session.open_channel do |channel| channel.exec command do |ch, success| ch.on_data do |c, data| diff --git a/lib/producer/core/testing/mock_remote.rb b/lib/producer/core/testing/mock_remote.rb index 9a472ea..bddae6b 100644 --- a/lib/producer/core/testing/mock_remote.rb +++ b/lib/producer/core/testing/mock_remote.rb @@ -6,18 +6,20 @@ module Producer raise 'no session for mock remote!' end - def execute(command) + def execute(command, output = '') tokens = command.split program = tokens.shift case program when 'echo' - tokens.join ' ' + output << tokens.join(' ') when 'true' - '' + output << '' when 'false' raise RemoteCommandExecutionError end + + output end end end diff --git a/spec/producer/core/remote_spec.rb b/spec/producer/core/remote_spec.rb index 17a6ee7..d0ceef7 100644 --- a/spec/producer/core/remote_spec.rb +++ b/spec/producer/core/remote_spec.rb @@ -79,6 +79,7 @@ module Producer::Core describe '#execute', :ssh do let(:arguments) { 'some remote command' } let(:command) { "echo #{arguments}" } + let(:output) { StringIO.new } it 'executes the given command in a new channel' do story_with_new_channel do |ch| @@ -88,7 +89,7 @@ module Producer::Core expect_story_completed { remote.execute command } end - it 'returns the output' do + it 'returns the command standard output output' do story_with_new_channel do |ch| ch.sends_exec command ch.gets_data arguments @@ -96,6 +97,15 @@ module Producer::Core expect(remote.execute command).to eq arguments end + it 'writes command standard output to provided output' do + story_with_new_channel do |ch| + ch.sends_exec command + ch.gets_data arguments + end + remote.execute command, output + expect(output.string).to eq arguments + end + it 'raises an exception when the exit status code is not 0' do story_with_new_channel do |ch| ch.sends_exec command diff --git a/spec/support/test_env_helpers.rb b/spec/support/test_env_helpers.rb index 2fa33db..ad20f58 100644 --- a/spec/support/test_env_helpers.rb +++ b/spec/support/test_env_helpers.rb @@ -15,7 +15,9 @@ module TestEnvHelpers def expect_execution(command) opts = { expected_from: caller.first } - RSpec::Mocks.expect_message(env.remote, :execute, opts).with(command) + RSpec::Mocks + .expect_message(env.remote, :execute, opts) + .with(command, env.output) end