Sha256: 5e231be898481520771f990baa6a1db2323974d19b7b398232a358f50c3a17c2

Contents?: true

Size: 1.65 KB

Versions: 13

Compression:

Stored size: 1.65 KB

Contents

require 'bpm/cli/base'

module SpecHelpers
  attr_reader :stdin, :stdout, :stderr

  def bpm(*argv)
    opts = Hash === argv.last ? argv.pop : {}

    kill!
    create_pipes

    @pid = Process.fork do
      Dir.chdir opts[:chdir] if opts[:chdir]

      @stdout.close
      STDOUT.reopen @stdout_child

      @stdin.close
      STDIN.reopen @stdin_child

      if opts[:track_stderr]
        @stderr.close
        STDERR.reopen @stderr_child
      end

      env.each do |key, val|
        ENV[key] = val
      end

      BPM::CLI::Base.start(argv)
    end

    @stdout_child.close
    @stdin_child.close
    @stderr_child.close
    @pid
  end

  def out_until_block(io = stdout)
    # read 1 first so we wait until the process is done processing the last write
    chars  = io.read(1)

    loop do
      chars << io.read_nonblock(1000)
      sleep 0.05
    end
  rescue Errno::EAGAIN, EOFError
    chars
  end

  def input(line, opts = {})
    if on = opts[:on]
      should_block_on on
    end
    stdin << "#{line}\n"
  end

  def wait
    return unless @pid

    pid, status = Process.wait2(@pid, 0)

    @exit_status = status
    @pid = nil
  end

  def exit_status
    wait
    @exit_status
  end

  def kill!
    Process.kill(9, @pid) if @pid
  end

  def create_pipes
    @stdout, @stdout_child = IO.pipe
    @stdin_child, @stdin   = IO.pipe
    @stderr, @stderr_child = IO.pipe
  end

  def write_api_key(api_key)
    write_creds("user@example.com", api_key)
  end

  def write_creds(email, api_key)
    FileUtils.mkdir_p(bpm_dir)
    File.open(bpm_dir("credentials"), "w") do |file|
      file.write YAML.dump(:bpm_api_key => api_key, :bpm_email => email)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bpm-1.0.0.beta.13 spec/support/cli.rb
bpm-1.0.0.beta.12 spec/support/cli.rb
bpm-1.0.0.beta.11 spec/support/cli.rb
bpm-1.0.0.beta.10 spec/support/cli.rb
bpm-1.0.0.beta.9 spec/support/cli.rb
bpm-1.0.0.beta.8 spec/support/cli.rb
bpm-1.0.0.beta.6 spec/support/cli.rb
bpm-1.0.0.beta.5 spec/support/cli.rb
bpm-1.0.0.beta.4 spec/support/cli.rb
bpm-0.1.4 spec/support/cli.rb
bpm-0.1.3 spec/support/cli.rb
bpm-0.1.2 spec/support/cli.rb
bpm-0.1.0 spec/support/cli.rb