Class: RSpec::Cli::CliProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/cli/cli_process.rb

Overview

This class spawns your process and provides some helpers to interact with the process

Constant Summary collapse

TERMINAL_COLOURS =
/\e\[(\d+)(;\d+)*m/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CliProcess

Returns a new instance of CliProcess.

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/rspec/cli/cli_process.rb', line 17

def initialize(*args)
  raise ArgumentError, "wrong number of arguments" if args.empty?
  args.unshift(*args.shift) if Array === args.first
  @command = args
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



15
16
17
# File 'lib/rspec/cli/cli_process.rb', line 15

def pid
  @pid
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
# File 'lib/rspec/cli/cli_process.rb', line 81

def alive?

  begin
    return status.nil?
  rescue
    return false
  end

end

#flushObject



23
24
25
26
# File 'lib/rspec/cli/cli_process.rb', line 23

def flush
  assert_spawned
  @master.flush
end

#getsObject



34
35
36
37
# File 'lib/rspec/cli/cli_process.rb', line 34

def gets
  assert_spawned
  @master.gets
end

#kill!(signal = "TERM") ⇒ Object



91
92
93
94
95
96
97
# File 'lib/rspec/cli/cli_process.rb', line 91

def kill!(signal = "TERM")

  assert_spawned
  @master.close
  Process.kill(signal, @pid)

end

#puts(*args) ⇒ Object



39
40
41
42
43
# File 'lib/rspec/cli/cli_process.rb', line 39

def puts(*args)
  assert_spawned
  @master.puts args
  @master.flush
end

#read_all(*args) ⇒ Object



28
29
30
31
32
# File 'lib/rspec/cli/cli_process.rb', line 28

def read_all(*args)
  assert_spawned
  @master.flush
  @master.read_all *args
end

#run!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/cli/cli_process.rb', line 60

def run!
  # Create master and slave pseudo terminal devices
  master_tty, slave_tty = PTY.open
  @master =  IODecorator.new master_tty
  @slave = slave_tty

  @pid =  PTY.spawn(*@command, in: @slave, out: @slave, err: @slave)[2]

  @slave.close

  self

end

#statusObject



74
75
76
77
78
79
# File 'lib/rspec/cli/cli_process.rb', line 74

def status

  assert_spawned
  PTY.check(@pid)

end

#stdinObject



49
50
51
# File 'lib/rspec/cli/cli_process.rb', line 49

def stdin
  @slave
end

#stdoutObject



45
46
47
# File 'lib/rspec/cli/cli_process.rb', line 45

def stdout
  @master
end

#write(arg) ⇒ Object



53
54
55
56
57
58
# File 'lib/rspec/cli/cli_process.rb', line 53

def write(arg)
  # This method can block if the argument is huge
  assert_spawned
  @master.write "#{arg}\n"
  @master.flush
end