Sha256: 55841e175a31bae5315a723e3051bde0a61b0c922385dc6b0ebf3008c5daa3c0

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

autoload :ChildProcess, 'childprocess'
autoload :Tempfile, 'tempfile'

module ChildProcessHelper
  module_function def call(cmd, env: nil, cwd: nil)
    process = ChildProcess.new(*cmd)
    process.io.inherit!
    if cwd
      process.cwd = cwd
    end
    if env
      env.each do |k, v|
        process.environment[k.to_s] = v
      end
    end
    process.start
    process.wait
    process
  end

  module_function def check_call(cmd, env: nil, cwd: nil)
    process = call(cmd, env: env, cwd: cwd)
    unless process.exit_code == 0
      raise "Failed to execute: #{cmd}"
    end
  end

  module_function def get_output(cmd, env: nil, cwd: nil)
    process = ChildProcess.new(*cmd)
    process.io.inherit!
    if cwd
      process.cwd = cwd
    end
    if env
      env.each do |k, v|
        process.environment[k.to_s] = v
      end
    end

    output = ''
    r, w = IO.pipe

    begin
      process.io.stdout = w
      process.start
      w.close

      thread = Thread.new do
        begin
          loop do
            output << r.readpartial(16384)
          end
        rescue EOFError
        end
      end

      process.wait
      thread.join
    ensure
      r.close
    end

    [process, output]
  end

  module_function def check_output(*args)
    process, output = get_output(*args)
    unless process.exit_code == 0
      raise "Failed to execute: #{args}"
    end
    output
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/support/child_process_helper.rb
mongoid-7.1.7 spec/support/child_process_helper.rb
mongoid-7.0.12 spec/support/child_process_helper.rb
mongoid-7.1.6 spec/support/child_process_helper.rb
mongoid-7.0.11 spec/support/child_process_helper.rb
mongoid-7.1.5 spec/support/child_process_helper.rb
mongoid-7.1.4 spec/support/child_process_helper.rb
mongoid-7.0.10 spec/support/child_process_helper.rb
mongoid-7.0.8 spec/support/child_process_helper.rb
mongoid-7.1.2 spec/support/child_process_helper.rb