Commit 6baa0861 authored by Chris Armstrong's avatar Chris Armstrong Committed by danielsdeleo
Browse files

Adds error? method to check whether the process exited successfully. This...

Adds error? method to check whether the process exited successfully. This allows the user to use custom error-handling logic. error! now uses error? internally.
parent fa0a9c82
......@@ -49,8 +49,8 @@ module Mixlib
# Working directory for the subprocess. Normally set via options to new
attr_accessor :cwd
# An Array of acceptable exit codes. #error! uses this list to determine if
# the command was successful. Normally set via options to new
# An Array of acceptable exit codes. #error? (and #error!) use this list
# to determine if the command was successful. Normally set via options to new
attr_accessor :valid_exit_codes
# When live_stream is set, stdout of the subprocess will be copied to it as
......@@ -227,17 +227,21 @@ module Mixlib
super
end
# Checks the +exitstatus+ against the set of +valid_exit_codes+. If
# +exitstatus+ is not in the list of +valid_exit_codes+, calls +invalid!+,
# which raises an Exception.
# Checks the +exitstatus+ against the set of +valid_exit_codes+.
# === Returns
# +true+ if +exitstatus+ is not in the list of +valid_exit_codes+, false
# otherwise.
def error?
!Array(valid_exit_codes).include?(exitstatus)
end
# If #error? is true, calls +invalid!+, which raises an Exception.
# === Returns
# nil::: always returns nil when it does not raise
# === Raises
# ::ShellCommandFailed::: via +invalid!+
def error!
unless Array(valid_exit_codes).include?(exitstatus)
invalid!("Expected process to exit with #{valid_exit_codes.inspect}, but received '#{exitstatus}'")
end
invalid!("Expected process to exit with #{valid_exit_codes.inspect}, but received '#{exitstatus}'") if error?
end
# Raises a ShellCommandFailed exception, appending the
......
......@@ -750,6 +750,24 @@ describe Mixlib::ShellOut do
lambda { executed_cmd.invalid!("I expected this to exit 42, not 0") }.should raise_error(Mixlib::ShellOut::ShellCommandFailed)
end
end
describe "#error?" do
context 'when exiting with invalid code' do
let(:exit_code) { 2 }
it "should return true" do
executed_cmd.error?.should be_true
end
end
context 'when exiting with valid code' do
let(:exit_code) { 0 }
it "should return false" do
executed_cmd.error?.should be_false
end
end
end
end
context "when handling the subprocess" do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment