Class: JetBlack::TerminalSession

Inherits:
Object
  • Object
show all
Defined in:
lib/jet_black/terminal_session.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_command, env:, directory:) ⇒ TerminalSession

Returns a new instance of TerminalSession.



11
12
13
14
15
# File 'lib/jet_black/terminal_session.rb', line 11

def initialize(raw_command, env:, directory:)
  @stderr_reader, @stderr_writer = IO.pipe
  @output, @input, @pid = PTY.spawn(env, raw_command, chdir: directory, err: stderr_writer.fileno)
  self.raw_stdout = []
end

Instance Attribute Details

#exit_statusObject

Returns the value of attribute exit_status.



9
10
11
# File 'lib/jet_black/terminal_session.rb', line 9

def exit_status
  @exit_status
end

Instance Method Details

#end_session(signal: "INT") ⇒ Object



48
49
50
51
52
53
# File 'lib/jet_black/terminal_session.rb', line 48

def end_session(signal: "INT")
  Process.kill(signal, pid)
  finalize_io

  self.exit_status = wait_for_exit_status
end

#expect(expected_value, reply: nil, timeout: DEFAULT_TIMEOUT, signal_on_timeout: "KILL") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jet_black/terminal_session.rb', line 17

def expect(expected_value, reply: nil, timeout: DEFAULT_TIMEOUT, signal_on_timeout: "KILL")
  output_matches = output.expect(expected_value, timeout)

  if output_matches.nil?
    end_session(signal: signal_on_timeout)
    raise TerminalSessionTimeoutError.new(self, expected_value, timeout)
  end

  raw_stdout.concat(output_matches)

  if reply != nil
    input.puts(reply)
  end
end

#finished?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/jet_black/terminal_session.rb', line 55

def finished?
  !exit_status.nil?
end

#stderrObject



36
37
38
# File 'lib/jet_black/terminal_session.rb', line 36

def stderr
  raw_std_err
end

#stdoutObject



32
33
34
# File 'lib/jet_black/terminal_session.rb', line 32

def stdout
  raw_stdout.join.gsub("\r", "")
end

#wait_for_finishObject



40
41
42
43
44
45
46
# File 'lib/jet_black/terminal_session.rb', line 40

def wait_for_finish
  return if finished?

  finalize_io

  self.exit_status = wait_for_exit_status
end