Class: Maze::InteractiveCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/interactive_cli.rb

Overview

Encapsulates a shell session, retaining state and input streams for interactive tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell = '/bin/sh', stop_command = 'exit') ⇒ InteractiveCLI

Creates an InteractiveCLI instance

Parameters:

  • shell (String) (defaults to: '/bin/sh')

    A path to the shell to run, defaults to ‘/bin/sh`

  • stop_command (String) (defaults to: 'exit')

    The stop command, defaults to ‘exit`



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/maze/interactive_cli.rb', line 28

def initialize(shell = '/bin/sh', stop_command = 'exit')
  @shell = shell
  @stop_command = stop_command
  @stdout_lines = []
  @stderr_lines = []
  @on_exit_blocks = []
  @current_buffer = ''
  # TODO: Removed pending PLAT-6322
  # @boring = Boring.new

  start_threaded_shell(shell)
end

Instance Attribute Details

#current_bufferObject (readonly)

Returns the value of attribute current_buffer.



22
23
24
# File 'lib/maze/interactive_cli.rb', line 22

def current_buffer
  @current_buffer
end

#pidObject (readonly)

Returns the value of attribute pid.



18
19
20
# File 'lib/maze/interactive_cli.rb', line 18

def pid
  @pid
end

#stderr_linesObject (readonly)

Returns the value of attribute stderr_lines.



14
15
16
# File 'lib/maze/interactive_cli.rb', line 14

def stderr_lines
  @stderr_lines
end

#stdout_linesObject (readonly)

Returns the value of attribute stdout_lines.



10
11
12
# File 'lib/maze/interactive_cli.rb', line 10

def stdout_lines
  @stdout_lines
end

Instance Method Details

#on_exit(&block) ⇒ Object



83
84
85
# File 'lib/maze/interactive_cli.rb', line 83

def on_exit(&block)
  @on_exit_blocks << block
end

#run_command(command) ⇒ Boolean

Runs the given command if the shell is running

Parameters:

  • command (String)

    The command to run

Returns:

  • (Boolean)

    true if the command is executed, false otherwise



72
73
74
75
76
77
78
79
80
81
# File 'lib/maze/interactive_cli.rb', line 72

def run_command(command)
  return false unless running?

  @in_stream.puts(command)

  true
rescue ::Errno::EIO => err
  $logger.debug(pid) { "EIO error: #{err}" }
  false
end

#running?Boolean

Returns Whether the shell is currently running.

Returns:

  • (Boolean)

    Whether the shell is currently running



63
64
65
# File 'lib/maze/interactive_cli.rb', line 63

def running?
  !@pid.nil?
end

#start(threaded: true) ⇒ Object



41
42
43
# File 'lib/maze/interactive_cli.rb', line 41

def start(threaded: true)
  threaded ? start_threaded_shell(@shell) : start_shell(@shell)
end

#stopBoolean

Attempts to stop the shell using the preset command and wait for it to exit

Returns:

  • (Boolean)

    If the shell stopped successfully



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/maze/interactive_cli.rb', line 48

def stop
  run_command(@stop_command)

  @in_stream.close

  maybe_thread = @thread.join(15)

  # The thread did not exit!
  return false if maybe_thread.nil?

  @pid = nil
  true
end