Sha256: f22a1ec0b42aee652baa0221c3e78a91e7f57da0f3ad21ff9252499cd9eebfe3

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

require "pty"

class SpeckerRunner
  def initialize(*args)
    @stdout, slave = PTY.open
    system("stty raw", :in => slave)
    read, @stdin = IO.pipe

    @pid = spawn(*(args.push(:in => read, :out => slave, :err => slave)))

    @expector = TrackingExpector.new(@stdout, ENV["DEBUG_BACON"])

    yield self
  end

  def expect(matcher, timeout = 30)
    case matcher
    when Hash
      expect_branches(matcher, timeout)
    else
      @expector.expect(matcher, timeout)
    end
  end

  def send_keys(text_to_send)
    @stdin.puts(text_to_send)
  end

  def exit_code
    return @status if @status

    status = nil
    Timeout.timeout(5) do
      _, status = Process.waitpid2(@pid)
    end

    @status = numeric_exit_code(status)
  end

  alias_method :wait_for_exit, :exit_code

  def exited?
    !running?
  end

  def running?
    !!Process.getpgid(@pid)
  end

  def output
    @expector.output
  end

  def debug
    @expector.debug
  end

  def debug=(x)
    @expector.debug = x
  end

  private

  def expect_branches(branches, timeout)
    branch_names = /#{branches.keys.collect { |k| Regexp.quote(k) }.join("|")}/
    expected = @expector.expect(branch_names, timeout)
    return unless expected

    data = expected.first.match(/(#{branch_names})$/)
    matched = data[1]
    branches[matched].call
  end

  def numeric_exit_code(status)
    status.exitstatus
  rescue NoMethodError
    status
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
static-1.0.3 vendor/vmc-0.5.0/spec/support/specker_runner.rb
static-1.0.1 vendor/vmc-0.5.0/spec/support/specker_runner.rb
vmc-0.5.0 spec/support/specker_runner.rb
vmc-0.5.0.rc4 spec/support/specker_runner.rb
vmc-0.5.0.rc3 spec/support/specker_runner.rb
vmc-0.5.0.rc2 spec/support/specker_runner.rb