module Aruba::Api

Constants

DEFAULT_IO_WAIT_SECONDS
DEFAULT_TIMEOUT_SECONDS

Public Instance Methods

_create_file(file_name, file_content, check_presence) click to toggle source
# File lib/aruba/api.rb, line 42
def _create_file(file_name, file_content, check_presence)
  in_current_dir do
    raise "expected #{file_name} to be present" if check_presence && !File.file?(file_name)
    _mkdir(File.dirname(file_name))
    File.open(file_name, 'w') { |f| f << file_content }
  end
end
_create_fixed_size_file(file_name, file_size, check_presence) click to toggle source
# File lib/aruba/api.rb, line 50
def _create_fixed_size_file(file_name, file_size, check_presence)
  in_current_dir do
    raise "expected #{file_name} to be present" if check_presence && !File.file?(file_name)
    _mkdir(File.dirname(file_name))
    File.open(file_name, "wb"){ |f| f.seek(file_size - 1); f.write("\00"") }
  end
end
_ensure_newline(str) click to toggle source
# File lib/aruba/api.rb, line 313
def _ensure_newline(str)
  str.chomp << "\n"
end
_mkdir(dir_name) click to toggle source
# File lib/aruba/api.rb, line 130
def _mkdir(dir_name)
  FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
end
_write_interactive(input) click to toggle source
# File lib/aruba/api.rb, line 309
def _write_interactive(input)
  @interactive.stdin.write(input)
end
all_output() click to toggle source
# File lib/aruba/api.rb, line 167
def all_output
  all_stdout << all_stderr
end
all_stderr() click to toggle source
# File lib/aruba/api.rb, line 162
def all_stderr
  stop_processes!
  only_processes.inject("") { |out, ps| out << ps.stderr(@aruba_keep_ansi) }
end
all_stdout() click to toggle source
# File lib/aruba/api.rb, line 157
def all_stdout
  stop_processes!
  only_processes.inject("") { |out, ps| out << ps.stdout(@aruba_keep_ansi) }
end
announce_or_puts(msg) click to toggle source
# File lib/aruba/api.rb, line 317
def announce_or_puts(msg)
  if(@puts)
    Kernel.puts(msg)
  else
    puts(msg)
  end
end
append_output_to(message) click to toggle source
# File lib/aruba/api.rb, line 228
def append_output_to(message)
  "#{message} Output:\n\n#{all_output}\n"
end
append_to_file(file_name, file_content) click to toggle source
# File lib/aruba/api.rb, line 64
def append_to_file(file_name, file_content)
  in_current_dir do
    _mkdir(File.dirname(file_name))
    File.open(file_name, 'a') { |f| f << file_content }
  end
end
assert_exact_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 171
def assert_exact_output(expected, actual)
  unescape(actual).should == unescape(expected)
end
assert_exit_status(status) click to toggle source
# File lib/aruba/api.rb, line 218
def assert_exit_status(status)
  last_exit_status.should eq(status),
    append_output_to("Exit status was #{last_exit_status} but expected it to be #{status}.")
end
assert_exit_status_and_output(expect_to_pass, expected_output, expect_exact_output) click to toggle source

TODO: Remove this. Call more methods elsewhere instead. Reveals more intent.

# File lib/aruba/api.rb, line 205
def assert_exit_status_and_output(expect_to_pass, expected_output, expect_exact_output)
  assert_success(expect_to_pass)
  if expect_exact_output
    assert_exact_output(expected_output, all_output)
  else
    assert_partial_output(expected_output, all_output)
  end
end
assert_exit_status_and_partial_output(expect_to_pass, expected) click to toggle source
# File lib/aruba/api.rb, line 199
def assert_exit_status_and_partial_output(expect_to_pass, expected)
  assert_success(expect_to_pass)
  assert_partial_output(expected, all_output)
end
assert_failing_with(expected) click to toggle source
# File lib/aruba/api.rb, line 195
def assert_failing_with(expected)
  assert_exit_status_and_partial_output(false, expected)
end
assert_matching_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 179
def assert_matching_output(expected, actual)
  unescape(actual).should =~ %r#{unescape(expected)}/
end
assert_no_partial_output(unexpected, actual) click to toggle source
# File lib/aruba/api.rb, line 183
def assert_no_partial_output(unexpected, actual)
  if Regexp === unexpected
    unescape(actual).should_not =~ unexpected
  else
    unescape(actual).should_not include(unexpected)
  end
end
assert_not_exit_status(status) click to toggle source
# File lib/aruba/api.rb, line 223
def assert_not_exit_status(status)
  last_exit_status.should_not eq(status),
    append_output_to("Exit status was #{last_exit_status} which was not expected.")
end
assert_partial_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 175
def assert_partial_output(expected, actual)
  unescape(actual).should include(unescape(expected))
end
assert_passing_with(expected) click to toggle source
# File lib/aruba/api.rb, line 191
def assert_passing_with(expected)
  assert_exit_status_and_partial_output(true, expected)
end
assert_success(success) click to toggle source
# File lib/aruba/api.rb, line 214
def assert_success(success)
  success ? assert_exit_status(0) : assert_not_exit_status(0)
end
cd(dir) click to toggle source
# File lib/aruba/api.rb, line 21
def cd(dir)
  dirs << dir
  raise "#{current_dir} is not a directory." unless File.directory?(current_dir)
end
check_directory_presence(paths, expect_presence) click to toggle source
# File lib/aruba/api.rb, line 113
def check_directory_presence(paths, expect_presence)
  prep_for_fs_check do
    paths.each do |path|
      if expect_presence
        File.should be_directory(path)
      else
        File.should_not be_directory(path)
      end
    end
  end
end
check_exact_file_content(file, exact_content) click to toggle source
# File lib/aruba/api.rb, line 109
def check_exact_file_content(file, exact_content)
  prep_for_fs_check { IO.read(file).should == exact_content }
end
check_file_content(file, partial_content, expect_match) click to toggle source
# File lib/aruba/api.rb, line 97
def check_file_content(file, partial_content, expect_match)
  regexp = regexp(partial_content)
  prep_for_fs_check do 
    content = IO.read(file)
    if expect_match
      content.should =~ regexp
    else
      content.should_not =~ regexp
    end
  end
end
check_file_presence(paths, expect_presence) click to toggle source
# File lib/aruba/api.rb, line 77
def check_file_presence(paths, expect_presence)
  prep_for_fs_check do
    paths.each do |path|
      if expect_presence
        File.should be_file(path)
      else
        File.should_not be_file(path)
      end
    end
  end
end
check_file_size(paths_and_sizes) click to toggle source
# File lib/aruba/api.rb, line 89
def check_file_size(paths_and_sizes)
  prep_for_fs_check do
    paths_and_sizes.each do |path, size|
      File.size(path).should == size
    end
  end
end
create_dir(dir_name) click to toggle source
# File lib/aruba/api.rb, line 71
def create_dir(dir_name)
  in_current_dir do
    _mkdir(dir_name)
  end
end
current_dir() click to toggle source
# File lib/aruba/api.rb, line 17
def current_dir
  File.join(*dirs)
end
current_ruby() click to toggle source
# File lib/aruba/api.rb, line 333
def current_ruby
  File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
end
detect_ruby(cmd) click to toggle source
# File lib/aruba/api.rb, line 325
def detect_ruby(cmd)
  if cmd =~ %r^ruby\s/
    cmd.gsub(%r^ruby\s/, "#{current_ruby} ")
  else
    cmd
  end
end
dirs() click to toggle source
# File lib/aruba/api.rb, line 26
def dirs
  @dirs ||= ['tmp/aruba']
end
exit_timeout() click to toggle source
# File lib/aruba/api.rb, line 283
def exit_timeout
  @aruba_timeout_seconds || DEFAULT_TIMEOUT_SECONDS
end
get_process(wanted) click to toggle source
# File lib/aruba/api.rb, line 253
def get_process(wanted)
  processes.reverse.find{ |name, _| name == wanted }[-1]
end
in_current_dir(&block) click to toggle source
# File lib/aruba/api.rb, line 12
def in_current_dir(&block)
  _mkdir(current_dir)
  Dir.chdir(current_dir, &block)
end
io_wait() click to toggle source
# File lib/aruba/api.rb, line 289
def io_wait
  @aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS
end
only_processes() click to toggle source
# File lib/aruba/api.rb, line 257
def only_processes
  processes.collect{ |_, process| process }
end
original_env() click to toggle source
# File lib/aruba/api.rb, line 373
def original_env
  @original_env ||= {}
end
output_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 142
def output_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).output(@aruba_keep_ansi)
end
overwrite_file(file_name, file_content) click to toggle source
# File lib/aruba/api.rb, line 38
def overwrite_file(file_name, file_content)
  _create_file(file_name, file_content, true)
end
prep_for_fs_check(&block) click to toggle source
# File lib/aruba/api.rb, line 125
def prep_for_fs_check(&block)
  stop_processes!
  in_current_dir{ block.call }
end
processes() click to toggle source
# File lib/aruba/api.rb, line 232
def processes
  @processes ||= []
end
regexp(string_or_regexp) click to toggle source
# File lib/aruba/api.rb, line 138
def regexp(string_or_regexp)
  Regexp === string_or_regexp ? string_or_regexp : Regexp.compile(Regexp.escape(string_or_regexp))
end
register_process(name, process) click to toggle source
# File lib/aruba/api.rb, line 249
def register_process(name, process)
  processes << [name, process]
end
remove_file(file_name) click to toggle source
# File lib/aruba/api.rb, line 58
def remove_file(file_name)
  in_current_dir do
    FileUtils.rm(file_name)
  end
end
restore_env() click to toggle source
# File lib/aruba/api.rb, line 367
def restore_env
  original_env.each do |key, value|
    ENV[key] = value
  end
end
run(cmd) { |process| ... } click to toggle source
# File lib/aruba/api.rb, line 261
def run(cmd)
  @commands ||= []
  @commands << cmd

  cmd = detect_ruby(cmd)

  in_current_dir do
    Aruba.config.hooks.execute(:before_cmd, self, cmd)

    announcer.dir(Dir.pwd)
    announcer.cmd(cmd)

    process = Process.new(cmd, exit_timeout, io_wait)
    register_process(cmd, process)
    process.run!

    block_given? ? yield(process) : process
  end
end
run_interactive(cmd) click to toggle source
# File lib/aruba/api.rb, line 301
def run_interactive(cmd)
  @interactive = run(cmd)
end
run_simple(cmd, fail_on_error=true) click to toggle source
# File lib/aruba/api.rb, line 293
def run_simple(cmd, fail_on_error=true)
  run(cmd) do |process|
    stop_process(process)
  end
  @timed_out = last_exit_status.nil?
  assert_exit_status(0) if fail_on_error
end
set_env(key, value) click to toggle source
# File lib/aruba/api.rb, line 361
def set_env(key, value)
  announcer.env(key, value)
  original_env[key] = ENV.delete(key)
  ENV[key] = value
end
stderr_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 152
def stderr_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).stderr(@aruba_keep_ansi)
end
stdout_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 147
def stdout_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).stdout(@aruba_keep_ansi)
end
stop_processes!() click to toggle source
# File lib/aruba/api.rb, line 236
def stop_processes!
  processes.each do |_, process|
    stop_process(process)
  end
end
terminate_processes!() click to toggle source
# File lib/aruba/api.rb, line 242
def terminate_processes!
  processes.each do |_, process|
    terminate_process(process)
    stop_process(process)
  end
end
type(input) click to toggle source
# File lib/aruba/api.rb, line 305
def type(input)
  _write_interactive(_ensure_newline(input))
end
unescape(string) click to toggle source
# File lib/aruba/api.rb, line 134
def unescape(string)
  string.gsub('\n', "\n").gsub('\"', '"').gsub('\e', "\e")
end
unset_bundler_env_vars() click to toggle source
# File lib/aruba/api.rb, line 355
def unset_bundler_env_vars
  %w[RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE].each do |key|
    set_env(key, nil)
  end
end
use_clean_gemset(gemset) click to toggle source
# File lib/aruba/api.rb, line 337
def use_clean_gemset(gemset)
  run_simple(%Q{rvm gemset create "#{gemset}"}, true)
  if all_stdout =~ %r'#{gemset}' gemset created \((.*)\)\./
    gem_home = $1
    set_env('GEM_HOME', gem_home)
    set_env('GEM_PATH', gem_home)
    set_env('BUNDLE_PATH', gem_home)

    paths = (ENV['PATH'] || "").split(File::PATH_SEPARATOR)
    paths.unshift(File.join(gem_home, 'bin'))
    set_env('PATH', paths.uniq.join(File::PATH_SEPARATOR))

    run_simple("gem install bundler", true)
  else
    raise "I didn't understand rvm's output: #{all_stdout}"
  end
end
write_file(file_name, file_content) click to toggle source
# File lib/aruba/api.rb, line 30
def write_file(file_name, file_content)
  _create_file(file_name, file_content, false)
end
write_fixed_size_file(file_name, file_size) click to toggle source
# File lib/aruba/api.rb, line 34
def write_fixed_size_file(file_name, file_size)
  _create_fixed_size_file(file_name, file_size, false)
end