Write output from `sh' task action directly:
* Accept output as argument in Remote#execute; * Modify ShellCommand so that env output is directly handled.
This commit is contained in:
parent
400fa1ee3f
commit
db3644a3e6
@ -3,7 +3,8 @@ module Producer
|
|||||||
module Actions
|
module Actions
|
||||||
class ShellCommand < Action
|
class ShellCommand < Action
|
||||||
def apply
|
def apply
|
||||||
output.puts remote.execute(arguments.first)
|
remote.execute(arguments.first, output)
|
||||||
|
output.puts
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,8 +23,7 @@ module Producer
|
|||||||
@fs ||= Remote::FS.new(session.sftp.connect)
|
@fs ||= Remote::FS.new(session.sftp.connect)
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(command)
|
def execute(command, output = '')
|
||||||
output = ''
|
|
||||||
channel = session.open_channel do |channel|
|
channel = session.open_channel do |channel|
|
||||||
channel.exec command do |ch, success|
|
channel.exec command do |ch, success|
|
||||||
ch.on_data do |c, data|
|
ch.on_data do |c, data|
|
||||||
|
@ -6,18 +6,20 @@ module Producer
|
|||||||
raise 'no session for mock remote!'
|
raise 'no session for mock remote!'
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(command)
|
def execute(command, output = '')
|
||||||
tokens = command.split
|
tokens = command.split
|
||||||
program = tokens.shift
|
program = tokens.shift
|
||||||
|
|
||||||
case program
|
case program
|
||||||
when 'echo'
|
when 'echo'
|
||||||
tokens.join ' '
|
output << tokens.join(' ')
|
||||||
when 'true'
|
when 'true'
|
||||||
''
|
output << ''
|
||||||
when 'false'
|
when 'false'
|
||||||
raise RemoteCommandExecutionError
|
raise RemoteCommandExecutionError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -79,6 +79,7 @@ module Producer::Core
|
|||||||
describe '#execute', :ssh do
|
describe '#execute', :ssh do
|
||||||
let(:arguments) { 'some remote command' }
|
let(:arguments) { 'some remote command' }
|
||||||
let(:command) { "echo #{arguments}" }
|
let(:command) { "echo #{arguments}" }
|
||||||
|
let(:output) { StringIO.new }
|
||||||
|
|
||||||
it 'executes the given command in a new channel' do
|
it 'executes the given command in a new channel' do
|
||||||
story_with_new_channel do |ch|
|
story_with_new_channel do |ch|
|
||||||
@ -88,7 +89,7 @@ module Producer::Core
|
|||||||
expect_story_completed { remote.execute command }
|
expect_story_completed { remote.execute command }
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the output' do
|
it 'returns the command standard output output' do
|
||||||
story_with_new_channel do |ch|
|
story_with_new_channel do |ch|
|
||||||
ch.sends_exec command
|
ch.sends_exec command
|
||||||
ch.gets_data arguments
|
ch.gets_data arguments
|
||||||
@ -96,6 +97,15 @@ module Producer::Core
|
|||||||
expect(remote.execute command).to eq arguments
|
expect(remote.execute command).to eq arguments
|
||||||
end
|
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
|
it 'raises an exception when the exit status code is not 0' do
|
||||||
story_with_new_channel do |ch|
|
story_with_new_channel do |ch|
|
||||||
ch.sends_exec command
|
ch.sends_exec command
|
||||||
|
@ -15,7 +15,9 @@ module TestEnvHelpers
|
|||||||
|
|
||||||
def expect_execution(command)
|
def expect_execution(command)
|
||||||
opts = { expected_from: caller.first }
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user