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 41
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 49
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 346
def _ensure_newline(str)
  str.chomp << "\n"
end
_mkdir(dir_name) click to toggle source
# File lib/aruba/api.rb, line 142
def _mkdir(dir_name)
  FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name)
end
_read_interactive() click to toggle source
# File lib/aruba/api.rb, line 342
def _read_interactive
  @interactive.read_stdout
end
_write_interactive(input) click to toggle source
# File lib/aruba/api.rb, line 337
def _write_interactive(input)
  @interactive.stdin.write(input)
  @interactive.stdin.flush
end
all_output() click to toggle source
# File lib/aruba/api.rb, line 181
def all_output
  all_stdout << all_stderr
end
all_stderr() click to toggle source
# File lib/aruba/api.rb, line 176
def all_stderr
  stop_processes!
  only_processes.inject("") { |out, ps| out << ps.stderr }
end
all_stdout() click to toggle source
# File lib/aruba/api.rb, line 171
def all_stdout
  stop_processes!
  only_processes.inject("") { |out, ps| out << ps.stdout }
end
announce_or_puts(msg) click to toggle source
# File lib/aruba/api.rb, line 350
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 250
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 63
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 185
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 240
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 227
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 221
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 217
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 193
def assert_matching_output(expected, actual)
  unescape(actual).should =~ /#{unescape(expected)}/
end
assert_no_partial_output(unexpected, actual) click to toggle source
# File lib/aruba/api.rb, line 201
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 245
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_not_matching_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 197
def assert_not_matching_output(expected, actual)
  unescape(actual).should_not =~ /#{unescape(expected)}/
end
assert_partial_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 189
def assert_partial_output(expected, actual)
  unescape(actual).should include(unescape(expected))
end
assert_partial_output_interactive(expected) click to toggle source
# File lib/aruba/api.rb, line 209
def assert_partial_output_interactive(expected)
  unescape(_read_interactive).include?(unescape(expected)) ? true : false
end
assert_passing_with(expected) click to toggle source
# File lib/aruba/api.rb, line 213
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 236
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 20
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 125
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 121
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 109
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 82
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 94
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 70
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 16
def current_dir
  File.join(*dirs)
end
current_ruby() click to toggle source
# File lib/aruba/api.rb, line 366
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 358
def detect_ruby(cmd)
  if cmd =~ /^ruby\s/
    cmd.gsub(/^ruby\s/, "#{current_ruby} ")
  else
    cmd
  end
end
dirs() click to toggle source
# File lib/aruba/api.rb, line 25
def dirs
  @dirs ||= ['tmp', 'aruba']
end
eot() click to toggle source
# File lib/aruba/api.rb, line 333
def eot
  @interactive.stdin.close
end
exit_timeout() click to toggle source
# File lib/aruba/api.rb, line 306
def exit_timeout
  @aruba_timeout_seconds || DEFAULT_TIMEOUT_SECONDS
end
get_process(wanted) click to toggle source
# File lib/aruba/api.rb, line 275
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 11
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 312
def io_wait
  @aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS
end
only_processes() click to toggle source
# File lib/aruba/api.rb, line 279
def only_processes
  processes.collect{ |_, process| process }
end
original_env() click to toggle source
# File lib/aruba/api.rb, line 406
def original_env
  @original_env ||= {}
end
output_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 156
def output_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).output
end
overwrite_file(file_name, file_content) click to toggle source
# File lib/aruba/api.rb, line 37
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 137
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 254
def processes
  @processes ||= []
end
regexp(string_or_regexp) click to toggle source
# File lib/aruba/api.rb, line 152
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 271
def register_process(name, process)
  processes << [name, process]
end
remove_dir(directory_name) click to toggle source
# File lib/aruba/api.rb, line 76
def remove_dir(directory_name)
  in_current_dir do
    FileUtils.rmdir(directory_name)
  end
end
remove_file(file_name) click to toggle source
# File lib/aruba/api.rb, line 57
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 400
def restore_env
  original_env.each do |key, value|
    ENV[key] = value
  end
end
run(cmd, timeout = nil) { |process| ... } click to toggle source
# File lib/aruba/api.rb, line 283
def run(cmd, timeout = nil)
  timeout ||= exit_timeout
  @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 = Aruba.process.new(cmd, 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 324
def run_interactive(cmd)
  @interactive = run(cmd)
end
run_simple(cmd, fail_on_error=true, timeout = nil) click to toggle source
# File lib/aruba/api.rb, line 316
def run_simple(cmd, fail_on_error=true, timeout = nil)
  run(cmd, timeout) 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 394
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 166
def stderr_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).stderr
end
stdout_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 161
def stdout_from(cmd)
  cmd = detect_ruby(cmd)
  get_process(cmd).stdout
end
stop_processes!() click to toggle source
# File lib/aruba/api.rb, line 258
def stop_processes!
  processes.each do |_, process|
    stop_process(process)
  end
end
terminate_processes!() click to toggle source
# File lib/aruba/api.rb, line 264
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 328
def type(input)
  return eot if "" == input
  _write_interactive(_ensure_newline(input))
end
unescape(string) click to toggle source
# File lib/aruba/api.rb, line 146
def unescape(string)
  string = string.gsub('\n', "\n").gsub('\"', '"').gsub('\e', "\e")
  string = string.gsub(/\e\[\d+(?>(;\d+)*)m/, '') unless @aruba_keep_ansi
  string
end
unset_bundler_env_vars() click to toggle source
# File lib/aruba/api.rb, line 388
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 370
def use_clean_gemset(gemset)
  run_simple(%Q{rvm gemset create "#{gemset}"}, true)
  if all_stdout =~ /'#{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
with_file_content(file) { |content| ... } click to toggle source
# File lib/aruba/api.rb, line 102
def with_file_content(file, &block)
  prep_for_fs_check do 
    content = IO.read(file)
    yield(content)
  end
end
write_file(file_name, file_content) click to toggle source
# File lib/aruba/api.rb, line 29
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 33
def write_fixed_size_file(file_name, file_size)
  _create_fixed_size_file(file_name, file_size, false)
end

Private Instance Methods

announcer() click to toggle source
# File lib/aruba/api.rb, line 427
def announcer
  Announcer.new(self, 
                :stdout => @announce_stdout, 
                :stderr => @announce_stderr,
                :dir => @announce_dir,
                :cmd => @announce_cmd,
                :env => @announce_env)
end
last_exit_status() click to toggle source

TODO: move some more methods under here!

# File lib/aruba/api.rb, line 413
def last_exit_status
  return @last_exit_status if @last_exit_status
  stop_processes!
  @last_exit_status
end
stop_process(process) click to toggle source
# File lib/aruba/api.rb, line 419
def stop_process(process)
  @last_exit_status = process.stop(announcer)
end
terminate_process(process) click to toggle source
# File lib/aruba/api.rb, line 423
def terminate_process(process)
  process.terminate
end