Class: Reviewer::Shell::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewer/shell/result.rb

Overview

Provides a structure interface for the results of running a command

Constant Summary collapse

EXIT_STATUS_CODES =
{
  success: 0,
  cannot_execute: 126,
  executable_not_found: 127,
  terminated: 130
}.freeze
STD_ERROR_STRINGS =

Not all command line tools use the 127 exit status when an executable cannot be found, so this provides a home for recognizeable strings in those tools’ error messages that we can translate to the appropriate exit status for internal consistency

{
  executable_not_found: "can't find executable"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = nil, stderr = nil, status = nil) ⇒ self

An instance of a result from running a local command. Captures the values for ‘$stdout`,

`$stderr`, and the exit status of the command to provide a reliable way of interpreting
the results for commands that otherwise use these values inconsistently.

Examples:

Using with Open3.capture3

captured_results = Open3.capture3(command)
result = Result.new(*captured_results)

Parameters:

  • (defaults to: nil)

    nil [String] standard out output from a command

  • (defaults to: nil)

    nil [String] standard error output from a command

  • (defaults to: nil)

    nil [ProcessStatus] an instance of ProcessStatus for a command



44
45
46
47
48
49
# File 'lib/reviewer/shell/result.rb', line 44

def initialize(stdout = nil, stderr = nil, status = nil)
  @stdout = stdout
  @stderr = stderr
  @status = status
  @exit_status = status&.exitstatus
end

Instance Attribute Details

#exit_statusInteger?

Returns the exit status code from the command.

Returns:

  • the exit status code from the command



29
# File 'lib/reviewer/shell/result.rb', line 29

attr_reader :stdout, :stderr, :status

#statusObject

Returns the value of attribute status.



29
# File 'lib/reviewer/shell/result.rb', line 29

attr_reader :stdout, :stderr, :status

#stderrString?

Returns standard error from the command.

Returns:

  • standard error from the command



29
# File 'lib/reviewer/shell/result.rb', line 29

attr_reader :stdout, :stderr, :status

#stdoutString?

Returns standard output from the command.

Returns:

  • standard output from the command



29
30
31
# File 'lib/reviewer/shell/result.rb', line 29

def stdout
  @stdout
end

Instance Method Details

#cannot_execute?Boolean

Determines whether a command simply cannot be executed.

Returns:

  • true if the exit sttaus code equals 126



62
# File 'lib/reviewer/shell/result.rb', line 62

def cannot_execute? = exit_status == EXIT_STATUS_CODES[:cannot_execute]

#executable_not_found?Boolean

Determines whether the command failed because the executable cannot be found. Since this is an error that can be corrected fairly predictably and easily, it provides the ability to tailor the error guidance to help folks recover

Returns:

  • true if the exit sttaus code is 127 or there’s a recognizable equivalent value in the standard error string



70
71
72
73
# File 'lib/reviewer/shell/result.rb', line 70

def executable_not_found?
  exit_status == EXIT_STATUS_CODES[:executable_not_found] ||
    stderr&.include?(STD_ERROR_STRINGS[:executable_not_found])
end

#exists?Boolean

Returns:



51
# File 'lib/reviewer/shell/result.rb', line 51

def exists? = [stdout, stderr, exit_status].compact.any?

#rerunnable?Boolean

Determines if re-running a command is entirely futile. Primarily to help when a command fails within a batch and needs to be re-run to show the output

Returns:

  • true if the exit status code is greater than or equal to 126



57
# File 'lib/reviewer/shell/result.rb', line 57

def rerunnable? = exit_status < EXIT_STATUS_CODES[:cannot_execute]

#to_sString

Returns a string representation of the result

Returns:

  • stdout if present, otherwise stderr



78
79
80
# File 'lib/reviewer/shell/result.rb', line 78

def to_s
  [stderr, stdout].compact.join("\n").strip
end