Module: Scarpe::Components::ProcessHelpers

Includes:
FileHelpers
Included in:
Test::Helpers
Defined in:
lib/scarpe/components/process_helpers.rb

Instance Method Summary collapse

Methods included from FileHelpers

#with_tempfile, #with_tempfiles

Instance Method Details

#run_out_err_result(cmd) ⇒ Array(String,String,bool)

Run the command and capture its stdout and stderr output, and whether it succeeded or failed. Return after the command has completed. The awkward name is because this is normally a component of another library. Ordinarily you’d want to raise a library-specific exception on failure, print a library-specific message or delimiter, or otherwise handle success and failure. This is too general as-is.

Parameters:

  • cmd (String, Array<String>)

    the command to run in Kernel#spawn format

Returns:

  • (Array(String,String,bool))

    the stdout output, stderr output and success/failure of the command in a 3-element Array



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scarpe/components/process_helpers.rb', line 19

def run_out_err_result(cmd)
  out_str = ""
  err_str = ""
  success = nil

  with_tempfiles([
    ["scarpe_cmd_stdout", ""],
    ["scarpe_cmd_stderr", ""],
  ]) do |stdout_file, stderr_file|
    pid = Kernel.spawn(cmd, out: stdout_file, err: stderr_file)
    Process.wait(pid)
    success = $?.success?
    out_str = File.read stdout_file
    err_str = File.read stderr_file
  end

  [out_str, err_str, success]
end