Show command when remote execution fail
This commit is contained in:
parent
8168e68a2d
commit
0db2d90b72
@ -37,3 +37,15 @@ Feature: `sh' task action
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must not contain "after_fail"
|
||||
|
||||
Scenario: prints command when execution fail
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :some_task do
|
||||
sh '\false'
|
||||
end
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must match /\A\w+Error:\s+\\false/
|
||||
|
@ -5,18 +5,22 @@ module Producer
|
||||
|
||||
USAGE = "Usage: #{File.basename $0} [-v] [-n] recipe_file".freeze
|
||||
|
||||
EX_USAGE = 64
|
||||
EX_USAGE = 64
|
||||
EX_SOFTWARE = 70
|
||||
|
||||
class << self
|
||||
def run!(arguments, output: $stderr)
|
||||
cli = new(arguments)
|
||||
begin
|
||||
cli = new(arguments)
|
||||
cli.parse_arguments!
|
||||
cli.run
|
||||
rescue ArgumentError
|
||||
output.puts USAGE
|
||||
exit EX_USAGE
|
||||
rescue RuntimeError => e
|
||||
output.puts "#{e.class.name.split('::').last}: #{e.message}"
|
||||
exit EX_SOFTWARE
|
||||
end
|
||||
cli.run
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
module Producer
|
||||
module Core
|
||||
Error = Class.new(StandardError)
|
||||
RuntimeError = Class.new(RuntimeError)
|
||||
ConditionNotMetError = Class.new(Error)
|
||||
RemoteCommandExecutionError = Class.new(Error)
|
||||
RemoteCommandExecutionError = Class.new(RuntimeError)
|
||||
end
|
||||
end
|
||||
|
@ -32,7 +32,7 @@ module Producer
|
||||
|
||||
ch.on_request 'exit-status' do |c, data|
|
||||
exit_status = data.read_long
|
||||
raise RemoteCommandExecutionError if exit_status != 0
|
||||
raise RemoteCommandExecutionError, command if exit_status != 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -17,35 +17,33 @@ module Producer::Core
|
||||
let(:output) { StringIO.new }
|
||||
subject(:run) { described_class.run! arguments, output: output }
|
||||
|
||||
before { allow(described_class).to receive(:new) { cli } }
|
||||
|
||||
it 'builds a new CLI with given arguments' do
|
||||
expect(described_class)
|
||||
.to receive(:new).with(arguments).and_call_original
|
||||
expect(described_class).to receive(:new).with(arguments)
|
||||
run
|
||||
end
|
||||
|
||||
it 'runs the CLI' do
|
||||
allow(described_class).to receive(:new) { cli }
|
||||
expect(cli).to receive :run
|
||||
run
|
||||
end
|
||||
|
||||
it 'parses CLI arguments' do
|
||||
allow(described_class).to receive(:new) { cli }
|
||||
expect(cli).to receive :parse_arguments!
|
||||
run
|
||||
end
|
||||
|
||||
context 'when an argument error is raised' do
|
||||
before do
|
||||
allow(described_class).to receive(:new) { cli }
|
||||
allow(cli).to receive(:parse_arguments!)
|
||||
.and_raise described_class::ArgumentError
|
||||
end
|
||||
|
||||
it 'exits with a return status of 64' do
|
||||
expect { run }.to raise_error(SystemExit) { |e|
|
||||
expect { run }.to raise_error(SystemExit) do |e|
|
||||
expect(e.status).to eq 64
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
it 'prints the usage' do
|
||||
@ -53,6 +51,24 @@ module Producer::Core
|
||||
expect(output.string).to match /\AUsage: .+/
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a runtime error is raised' do
|
||||
before do
|
||||
allow(cli).to receive(:run)
|
||||
.and_raise RuntimeError, 'some message'
|
||||
end
|
||||
|
||||
it 'exits with a return status of 70' do
|
||||
expect { run }.to raise_error(SystemExit) do |e|
|
||||
expect(e.status).to eq 70
|
||||
end
|
||||
end
|
||||
|
||||
it 'prints exception name and message' do
|
||||
trap_exit { run }
|
||||
expect(output.string).to match /\ARuntimeError: some message/
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
|
@ -106,14 +106,23 @@ module Producer::Core
|
||||
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
|
||||
ch.gets_data arguments
|
||||
ch.gets_exit_status 1
|
||||
context 'when command execution fails' do
|
||||
before do
|
||||
story_with_new_channel do |ch|
|
||||
ch.sends_exec command
|
||||
ch.gets_data arguments
|
||||
ch.gets_exit_status 1
|
||||
end
|
||||
end
|
||||
|
||||
it 'raises an exception' do
|
||||
expect { remote.execute command }
|
||||
.to raise_error(RemoteCommandExecutionError)
|
||||
end
|
||||
|
||||
it 'includes the command in the exception message' do
|
||||
expect { remote.execute command }.to raise_error /#{command}/
|
||||
end
|
||||
expect { remote.execute command }
|
||||
.to raise_error(RemoteCommandExecutionError)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user