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:

  • stdout (defaults to: nil)

    nil [String] standard out output from a command

  • stderr (defaults to: nil)

    nil [String] standard error output from a command

  • status (defaults to: nil)

    nil [ProcessStatus] an instance of ProcessStatus for a command



35
36
37
38
39
40
# File 'lib/reviewer/shell/result.rb', line 35

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

Instance Attribute Details

#exit_statusObject

Returns the value of attribute exit_status.



21
22
23
# File 'lib/reviewer/shell/result.rb', line 21

def exit_status
  @exit_status
end

#statusObject

Returns the value of attribute status.



21
22
23
# File 'lib/reviewer/shell/result.rb', line 21

def status
  @status
end

#stderrObject

Returns the value of attribute stderr.



21
22
23
# File 'lib/reviewer/shell/result.rb', line 21

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



21
22
23
# File 'lib/reviewer/shell/result.rb', line 21

def stdout
  @stdout
end

Instance Method Details

#cannot_execute?Boolean

Determines whether a command simply cannot be executed.

Returns:

  • (Boolean)

    true if the exit sttaus code equals 126



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

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

#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:

  • (Boolean)

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



67
68
69
70
# File 'lib/reviewer/shell/result.rb', line 67

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

#exists?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/reviewer/shell/result.rb', line 42

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

#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:

  • (Boolean)

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



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

def rerunnable?
  exit_status < EXIT_STATUS_CODES[:cannot_execute]
end

#to_sString

Returns a string representation of the result

Returns:

  • (String)

    stdout if present, otherwise stderr



75
76
77
78
79
80
81
# File 'lib/reviewer/shell/result.rb', line 75

def to_s
  result_string = ''
  result_string += stderr
  result_string += stdout

  result_string.strip
end