Class: Toys::Utils::Exec::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/utils/exec.rb

Overview

The result returned from a subcommand execution. This includes the identifying name of the execution (if any), the result status of the execution, and any captured stream output.

Possible result statuses are:

  • The process failed to start. #failed? will return true, and #exception will return an exception describing the failure (often an errno).
  • The process executed and exited with a normal exit code. Either #success? or #error? will return true, and #exit_code will return the numeric exit code.
  • The process executed but was terminated by an uncaught signal. #signaled? will return true, and #signal_code will return the numeric signal code.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#captured_errString? (readonly)

The captured error string.

Returns:

  • (String)

    The string captured from stderr.

  • (nil)

    if the command was not configured to capture stderr.



899
900
901
# File 'lib/toys/utils/exec.rb', line 899

def captured_err
  @captured_err
end

#captured_outString? (readonly)

The captured output string.

Returns:

  • (String)

    The string captured from stdout.

  • (nil)

    if the command was not configured to capture stdout.



891
892
893
# File 'lib/toys/utils/exec.rb', line 891

def captured_out
  @captured_out
end

#exceptionException? (readonly)

The exception raised if a process couldn't be started.

Exactly one of #exception and #status will be non-nil. Exactly one of #exception, #exit_code, or #signal_code will be non-nil.

Returns:

  • (Exception)

    The exception raised from process start.

  • (nil)

    if the process started successfully.



923
924
925
# File 'lib/toys/utils/exec.rb', line 923

def exception
  @exception
end

#nameObject (readonly)

The subcommand's name.

Returns:

  • (Object)


883
884
885
# File 'lib/toys/utils/exec.rb', line 883

def name
  @name
end

#statusProcess::Status? (readonly)

The Ruby process status object, providing various information about the ending state of the process.

Exactly one of #exception and #status will be non-nil.

Returns:

  • (Process::Status)

    The status, if the process was successfully spawned and terminated.

  • (nil)

    if the process could not be started.



911
912
913
# File 'lib/toys/utils/exec.rb', line 911

def status
  @status
end

Instance Method Details

#effective_codeInteger

Returns an "effective" exit code, which is always an integer if the process has terminated for any reason. In general, this code will be:

  • The same as #exit_code if the process terminated normally with an exit code,
  • The convention of 128+signalnum if the process terminated due to a signal,
  • The convention of 126 if the process could not start due to lack of execution permissions,
  • The convention of 127 if the process could not start because the command was not recognized or could not be found, or
  • An undefined value between 1 and 255 for other failures.

Note that the normal exit code and signal number cases are stable, but any other cases are subject to change on future releases.

Returns:

  • (Integer)


1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/toys/utils/exec.rb', line 1018

def effective_code
  code = exit_code
  return code unless code.nil?
  code = signal_code
  return code + 128 unless code.nil?
  case exception
  when ::Errno::ENOENT
    127
  else
    # This is the intended result for ENOEXEC/EACCES.
    # For now, any other error (e.g. EBADARCH on MacOS) will also map
    # to this result. We can change this in the future since the
    # documentation explicitly allows it.
    126
  end
end

#error?boolean

Returns true if the subprocess terminated with a nonzero status, or false if the process failed to start, terminated due to a signal, or returned a zero status.

Returns:

  • (boolean)


994
995
996
997
# File 'lib/toys/utils/exec.rb', line 994

def error?
  code = exit_code
  !code.nil? && !code.zero?
end

#exit_codeInteger?

The numeric status code for a process that exited normally,

Exactly one of #exception, #exit_code, or #signal_code will be non-nil.

Returns:

  • (Integer)

    the numeric status code, if the process started successfully and exited normally.

  • (nil)

    if the process did not start successfully, or was terminated by an uncaught signal.



936
937
938
# File 'lib/toys/utils/exec.rb', line 936

def exit_code
  status&.exitstatus
end

#failed?boolean

Returns true if the subprocess failed to start, or false if the process was able to execute.

Returns:

  • (boolean)


961
962
963
# File 'lib/toys/utils/exec.rb', line 961

def failed?
  status.nil?
end

#signal_codeInteger? Also known as: term_signal

The numeric signal code that caused process termination.

Exactly one of #exception, #exit_code, or #signal_code will be non-nil.

Returns:

  • (Integer)

    The signal that caused the process to terminate.

  • (nil)

    if the process did not start successfully, or executed and exited with a normal exit code.



950
951
952
# File 'lib/toys/utils/exec.rb', line 950

def signal_code
  status&.termsig
end

#signaled?boolean

Returns true if the subprocess terminated due to an unhandled signal, or false if the process failed to start or exited normally.

Returns:

  • (boolean)


971
972
973
# File 'lib/toys/utils/exec.rb', line 971

def signaled?
  !signal_code.nil?
end

#success?boolean

Returns true if the subprocess terminated with a zero status, or false if the process failed to start, terminated due to a signal, or returned a nonzero status.

Returns:

  • (boolean)


982
983
984
985
# File 'lib/toys/utils/exec.rb', line 982

def success?
  code = exit_code
  !code.nil? && code.zero?
end