Module: Lich::Common::ProcessLauncher

Defined in:
documented/common/process_launcher.rb

Overview

Spawns a process from an explicit environment and argv without allowing a one-element argv to fall back to Ruby's shell-interpreted command form.

Class Method Summary collapse

Class Method Details

.call(environment, argv, spawner: Process.method(:spawn)) ⇒ Integer

Launches one process and returns its PID.

Inputs are passed directly to Process.spawn after validation. A one-element argv uses Ruby's [executable, argv0] form so executable paths containing spaces or shell metacharacters remain atomic.

Parameters:

  • environment (Hash)

    child-process environment overrides

  • argv (Array<String>)

    executable followed by zero or more arguments

  • spawner (#call) (defaults to: Process.method(:spawn))

    injectable Process.spawn-compatible callable

Returns:

  • (Integer)

    child process ID

Raises:

  • (ArgumentError)

    when environment or argv is invalid

  • (SystemCallError)

    when the operating system cannot spawn the process



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'documented/common/process_launcher.rb', line 23

def call(environment, argv, spawner: Process.method(:spawn))
  raise ArgumentError, 'environment must be a Hash' unless environment.is_a?(Hash)
  unless argv.is_a?(Array) && !argv.empty? && argv.all?(String) && !argv.first.empty?
    raise ArgumentError, 'argv must contain a non-empty String executable followed by String arguments'
  end

  executable, *arguments = argv
  if arguments.empty?
    spawner.call(environment, [executable, File.basename(executable)])
  else
    spawner.call(environment, executable, *arguments)
  end
end