class HighLine::Test::Client

Constants

DEFAULT_POST_INPUT_DELAY

Attributes

child_pid[RW]
delay[RW]

The delay in milliseconds that the client waits after sending text via the type method

Public Class Methods

new() click to toggle source
# File lib/highline/test/client.rb, line 10
def initialize
  @delay = DEFAULT_POST_INPUT_DELAY
  setup
end

Public Instance Methods

cleanup() click to toggle source
# File lib/highline/test/client.rb, line 32
def cleanup
  return unless running?

  kill_child_process
  close_pipes
  setup
end
output() click to toggle source
# File lib/highline/test/client.rb, line 51
def output
  return @output if @output
  @output_write.close unless @output_write.closed?
  @output = @output_read.readpartial(PIPE_BUFFER_SIZE)
end
run() { |driver| ... } click to toggle source
# File lib/highline/test/client.rb, line 15
def run
  raise "Supply a block to provide a context in which the application is run" unless block_given?

  create_pipes

  @child_pid = fork

  if child_pid.nil?
    partial_reader = PartialReader.new(@input_read)
    driver = Driver.new(partial_reader, @output_write)
    yield driver

    # if the subject ever returns, kill the child process
    Process.kill(:KILL, Process.pid)
  end
end
running?() click to toggle source
# File lib/highline/test/client.rb, line 40
def running?
  not child_pid.nil?
end
type(text) click to toggle source
# File lib/highline/test/client.rb, line 44
def type(text)
  raise 'Client is not running' unless running?

  @input_write << "#{text}\n"
  sleep delay
end

Private Instance Methods

close_pipes() click to toggle source
# File lib/highline/test/client.rb, line 83
def close_pipes
  [@input_read, @input_write, @output_read, @output_write].each do |stream|
    stream.close unless stream.closed?
  end
end
create_pipes() click to toggle source
# File lib/highline/test/client.rb, line 68
def create_pipes
  @input_read, @input_write = IO.pipe
  @output_read, @output_write = IO.pipe
end
kill_child_process() click to toggle source
# File lib/highline/test/client.rb, line 73
def kill_child_process
  return if child_pid.nil?

  Process.kill(:KILL, child_pid)
rescue Errno::ESRCH => e
  # swallow errors if the child process has already been killed
ensure
  @child_pid = nil
end
setup() click to toggle source
# File lib/highline/test/client.rb, line 59
def setup
  @input_read   = nil
  @input_write  = nil
  @output_read  = nil
  @output_write = nil
  @output       = nil
  @child_pid    = nil
end