module Baf::Testing
Constants
- ENV_WHITELIST
- EXEC_TIMEOUT_ERROR_FMT
- EXIT_STATUS_MISMATCH_FMT
- ExecutionTimeout
- ExitStatusMismatch
- OUTPUT_SEPARATOR
- WAIT_MESSAGE_FMT
- WAIT_OUTPUT_MESSAGE_FMT
- WAIT_TIMEOUT
- WORKING_DIR
Public Class Methods
build_regexp(pattern, options = '')
click to toggle source
# File lib/baf/testing.rb, line 53 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
exercise_scenario(dir: WORKING_DIR) { || ... }
click to toggle source
# File lib/baf/testing.rb, line 63 def exercise_scenario dir: WORKING_DIR FileUtils.remove_entry_secure dir, true FileUtils.mkdir_p dir Dir.chdir dir do yield end end
expect_ex(process, exit_status)
click to toggle source
# File lib/baf/testing.rb, line 71 def expect_ex process, exit_status return if process.exit_status == exit_status fail ExitStatusMismatch, EXIT_STATUS_MISMATCH_FMT % { expected: exit_status, actual: process.exit_status, separator: OUTPUT_SEPARATOR, output: process.output.chomp } end
run(command, wait: true, env_allow: [], timeout: nil)
click to toggle source
# File lib/baf/testing.rb, line 82 def run command, wait: true, env_allow: [], timeout: nil Process.new( command, env_allow: ENV_WHITELIST + env_allow, timeout: timeout || Process::TIMEOUT ).tap do |process| process.start wait process if wait end end
unescape_step_arg(str)
click to toggle source
# File lib/baf/testing.rb, line 100 def unescape_step_arg str str.gsub '\n', "\n" end
wait(process)
click to toggle source
# File lib/baf/testing.rb, line 93 def wait process process.wait do process.stop fail ExecutionTimeout, EXEC_TIMEOUT_ERROR_FMT % process.timeout end end
wait_output(pattern, stream:, times: 1, timeout: WAIT_TIMEOUT)
click to toggle source
# File lib/baf/testing.rb, line 114 def wait_output pattern, stream:, times: 1, timeout: WAIT_TIMEOUT results = nil wait_until timeout: timeout do case pattern when Regexp then (results = stream.call.scan(pattern)).size >= times when String then stream.call.include? pattern end end results rescue Baf::Testing::WaitError => e fail Baf::Testing::WaitError.new(WAIT_OUTPUT_MESSAGE_FMT % { pattern: pattern, timeout: timeout, separator: OUTPUT_SEPARATOR, output: stream.call.chomp }, timeout) end
wait_until(message: WAIT_MESSAGE_FMT, timeout: WAIT_TIMEOUT) { || ... }
click to toggle source
# File lib/baf/testing.rb, line 104 def wait_until message: WAIT_MESSAGE_FMT, timeout: WAIT_TIMEOUT return if yield deadline = Time.now + timeout until Time.now >= deadline return if yield sleep 0.05 end fail WaitError.new message % timeout, timeout end
write_file(path, content)
click to toggle source
# File lib/baf/testing.rb, line 132 def write_file path, content FileUtils.mkdir_p File.dirname path IO.write path, content end