summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Armstrong <chris.armstrong@socrata.com>2013-09-24 14:46:47 -0700
committerdanielsdeleo <dan@opscode.com>2014-01-21 17:15:22 -0800
commit6baa0861a364adbac507a447b6cb3cbad4a160b1 (patch)
tree2a39182337512e17bd5651d0e277f18bc399d02d
parentfa0a9c82b50bf941f34b2480f80fb5c88fd241db (diff)
downloadmixlib-shellout-6baa0861a364adbac507a447b6cb3cbad4a160b1.tar.gz
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.
-rw-r--r--lib/mixlib/shellout.rb20
-rw-r--r--spec/mixlib/shellout_spec.rb18
2 files changed, 30 insertions, 8 deletions
diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb
index 66faa40..e4e0435 100644
--- a/lib/mixlib/shellout.rb
+++ b/lib/mixlib/shellout.rb
@@ -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
diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb
index 0a6f9b2..3d0a04f 100644
--- a/spec/mixlib/shellout_spec.rb
+++ b/spec/mixlib/shellout_spec.rb
@@ -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