Top Level Namespace

Defined Under Namespace

Modules: Baf

Instance Method Summary collapse

Instance Method Details

#build_regexp(pattern, options) ⇒ Object



1
2
3
4
5
6
7
8
9
# File 'lib/baf/testing/cucumber/steps/output.rb', line 1

def build_regexp pattern, options
  Regexp.new(pattern, options.each_char.inject(0) do |m, e|
    m | case e
      when ?i then Regexp::IGNORECASE
      when ?m then Regexp::MULTILINE
      when ?x then Regexp::EXTENDED
    end
  end)
end

#expect_output(content, stream: :output) ⇒ Object



11
12
13
14
# File 'lib/baf/testing/cucumber/steps/output.rb', line 11

def expect_output content, stream: :output
  stream = :stderr if stream == :error
  expect(last_command_started.send stream).to eq unescape_text content
end

#program_run(check: false, opts: nil, args: nil, wait: true) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
# File 'lib/baf/testing/cucumber/steps/execution.rb', line 1

def program_run check: false, opts: nil, args: nil, wait: true
  cmd = [*@_baf_program ||= %w[ruby baf]]
  cmd << opts if opts
  cmd << args.split(' ') if args
  if wait
    run_simple cmd.join(' '), fail_on_error: false
  else
    run cmd.join ' '
  end
  program_run_check if check
end

#program_run_check(status: 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/baf/testing/cucumber/steps/execution.rb', line 13

def program_run_check status: 0
  expect(last_command_started).to have_exit_status status
rescue RSpec::Expectations::ExpectationNotMetError => e
  if ENV.key? 'BAF_TEST_DEBUG'
    fail RSpec::Expectations::ExpectationNotMetError, <<-eoh
#{e.message} Output was:
  ```\n#{last_command_started.output.lines.map { |l| "  #{l}" }.join}  ```
    eoh
  else
    raise
  end
end

#wait_output!(pattern, times: 1, results: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/baf/testing/cucumber/steps/output_wait.rb', line 16

def wait_output! pattern, times: 1, results: nil
  output = -> { last_command_started.output }
  wait_until do
    case pattern
    when Regexp then (results = output.call.scan(pattern)).size >= times
    when String then output.call.include? pattern
    end
  end
  results
rescue Baf::Testing::WaitError => e
  fail <<-eoh
expected `#{pattern}' not seen after #{e.timeout} seconds in:
  ```\n#{output.call.lines.map { |l| "  #{l}" }.join}  ```
  eoh
end

#wait_until(message: 'condition not met after %d seconds') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/baf/testing/cucumber/steps/output_wait.rb', line 32

def wait_until message: 'condition not met after %d seconds'
  timeout = ENV.key?('BAF_TEST_TIMEOUT') ?
    ENV['BAF_TEST_TIMEOUT'].to_i :
    2
  Timeout.timeout timeout do
    loop do
      break if yield
      sleep 0.05
    end
  end
rescue Timeout::Error
  raise Baf::Testing::WaitError.new(message % timeout, timeout)
end