Class: Wukong::SpecHelpers::IntegrationTestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb

Overview

A runner for running commands in a subprocess.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options) ⇒ IntegrationTestRunner

Initialize a new IntegrationTestRunner to run a given command.



48
49
50
51
52
53
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 48

def initialize args, options
  @args   = args
  @env    = options[:env]
  @cwd    = options[:cwd]
  @inputs = []
end

Instance Attribute Details

#cmdObject

The command to execute



10
11
12
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 10

def cmd
  @cmd
end

#cwdObject

The directory in which to execute the command.



13
14
15
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 13

def cwd
  @cwd
end

#exit_codeObject

The exit code of the spawned process.



25
26
27
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 25

def exit_code
  @exit_code
end

#pidObject

The ID of the spawned subprocess (while it was running).



16
17
18
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 16

def pid
  @pid
end

#stderrObject

The STDERR of the spawned process.



22
23
24
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 22

def stderr
  @stderr
end

#stdoutObject

The STDOUT of the spawned process.



19
20
21
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 19

def stdout
  @stdout
end

Instance Method Details

#cmd_summaryObject



83
84
85
86
87
88
89
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 83

def cmd_summary
  [
   cmd,
   "with env #{env_summary}",
   "in dir #{cwd}"
  ].join("\n")
end

#envObject



75
76
77
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 75

def env
  ENV.to_hash.merge(@env || {})
end

#env_summaryObject



91
92
93
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 91

def env_summary
  { "PATH" => env["PATH"], "RUBYLIB" => env["RUBYLIB"] }.inspect
end

#in(dir) ⇒ Object



65
66
67
68
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 65

def in dir
  @cwd = dir
  self
end

#on(*events) ⇒ Object Also known as: <



59
60
61
62
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 59

def on *events
  @inputs.concat(events)
  self
end

#ran?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 79

def ran?
  @ran
end

#run!true, false

Run the command and capture its outputs and exit code.

Returns:

  • (true, false)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 30

def run!
  return false if ran?
  FileUtils.cd(cwd) do
    Open3.popen3(env, cmd) do |i, o, e, wait_thr|
      self.pid = wait_thr.pid
      
      @inputs.each { |input| i.puts(input) }
      i.close
      
      self.stdout    = o.read
      self.stderr    = e.read
      self.exit_code = wait_thr.value.to_i
    end
  end
  @ran = true
end

#using(env) ⇒ Object



70
71
72
73
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb', line 70

def using env
  @env = env
  self
end