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 48
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 73
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 402
def _ensure_newline(str)
  str.chomp << "\n"
end
_mkdir(dir_name) click to toggle source
# File lib/aruba/api.rb, line 182
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 398
def _read_interactive
  @interactive.read_stdout
end
_rm_rf(dir_name) click to toggle source
# File lib/aruba/api.rb, line 186
def _rm_rf(dir_name)
  FileUtils.rm_rf(dir_name)
end
_write_interactive(input) click to toggle source
# File lib/aruba/api.rb, line 393
def _write_interactive(input)
  @interactive.stdin.write(input)
  @interactive.stdin.flush
end
all_output() click to toggle source
# File lib/aruba/api.rb, line 225
def all_output
  all_stdout << all_stderr
end
all_stderr() click to toggle source
# File lib/aruba/api.rb, line 220
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 215
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 406
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 299
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 87
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 229
def assert_exact_output(expected, actual)
  actual.force_encoding(expected.encoding) if RUBY_VERSION >= "1.9"
  expect(unescape(actual)).to eq unescape(expected)
end
assert_exit_status(status) click to toggle source
# File lib/aruba/api.rb, line 289
def assert_exit_status(status)
  expect(last_exit_status).to 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 276
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 270
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 266
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 239
def assert_matching_output(expected, actual)
  actual.force_encoding(expected.encoding) if RUBY_VERSION >= "1.9"
  expect(unescape(actual)).to match /#{unescape(expected)}/
end
assert_no_partial_output(unexpected, actual) click to toggle source
# File lib/aruba/api.rb, line 249
def assert_no_partial_output(unexpected, actual)
  actual.force_encoding(unexpected.encoding) if RUBY_VERSION >= "1.9"
  if Regexp === unexpected
    expect(unescape(actual)).not_to match unexpected
  else
    expect(unescape(actual)).not_to include(unexpected)
  end
end
assert_not_exit_status(status) click to toggle source
# File lib/aruba/api.rb, line 294
def assert_not_exit_status(status)
  expect(last_exit_status).not_to 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 244
def assert_not_matching_output(expected, actual)
  actual.force_encoding(expected.encoding) if RUBY_VERSION >= "1.9"
  expect(unescape(actual)).not_to match /#{unescape(expected)}/
end
assert_partial_output(expected, actual) click to toggle source
# File lib/aruba/api.rb, line 234
def assert_partial_output(expected, actual)
  actual.force_encoding(expected.encoding) if RUBY_VERSION >= "1.9"
  expect(unescape(actual)).to include(unescape(expected))
end
assert_partial_output_interactive(expected) click to toggle source
# File lib/aruba/api.rb, line 258
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 262
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 285
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 27
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 165
def check_directory_presence(paths, expect_presence)
  prep_for_fs_check do
    paths.each do |path|
      if expect_presence
        expect(File).to be_directory(path)
      else
        expect(File).not_to be_directory(path)
      end
    end
  end
end
check_exact_file_content(file, exact_content) click to toggle source
# File lib/aruba/api.rb, line 161
def check_exact_file_content(file, exact_content)
  prep_for_fs_check { expect(IO.read(file)).to eq exact_content }
end
check_file_content(file, partial_content, expect_match) click to toggle source
# File lib/aruba/api.rb, line 149
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
      expect(content).to match regexp
    else
      expect(content).not_to match regexp
    end
  end
end
check_file_presence(paths, expect_presence) click to toggle source
# File lib/aruba/api.rb, line 106
def check_file_presence(paths, expect_presence)
  prep_for_fs_check do
    paths.each do |path|
      if path.kind_of? Regexp
        if expect_presence
          expect(Dir.glob('**/*')).to include_regexp(path)
        else
          expect(Dir.glob('**/*')).not_to include_regexp(path)
        end
      else
        if expect_presence
          expect(File).to be_file(path)
        else
          expect(File).not_to be_file(path)
        end
      end
    end
  end
end
check_file_size(paths_and_sizes) click to toggle source
# File lib/aruba/api.rb, line 134
def check_file_size(paths_and_sizes)
  prep_for_fs_check do
    paths_and_sizes.each do |path, size|
      expect(File.size(path)).to eq size
    end
  end
end
chmod(mode, name) click to toggle source
# File lib/aruba/api.rb, line 56
def chmod(mode, name)
  in_current_dir do
    raise "expected #{name} to be present" unless FileTest.exists?(name)
    if mode.kind_of? String
      FileUtils.chmod(mode.to_i(8),name)
    else
      FileUtils.chmod(mode,name)
    end
  end
end
clean_current_dir() click to toggle source
# File lib/aruba/api.rb, line 18
def clean_current_dir
  _rm_rf(current_dir)
  _mkdir(current_dir)
end
close_input() click to toggle source
# File lib/aruba/api.rb, line 384
def close_input
  @interactive.stdin.close
end
create_dir(dir_name) click to toggle source
# File lib/aruba/api.rb, line 94
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 23
def current_dir
  File.join(*dirs)
end
current_ruby() click to toggle source
# File lib/aruba/api.rb, line 422
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 414
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 32
def dirs
  @dirs ||= ['tmp', 'aruba']
end
eot() click to toggle source
# File lib/aruba/api.rb, line 388
def eot
  warn(%Q{\e[35m    The \"#eot\"-method is deprecated. It will be deleted with the next major version. Please use \"#close_input\"-method instead.\e[0m})
  close_input
end
exit_timeout() click to toggle source
# File lib/aruba/api.rb, line 357
def exit_timeout
  @aruba_timeout_seconds || DEFAULT_TIMEOUT_SECONDS
end
get_process(wanted) click to toggle source
# File lib/aruba/api.rb, line 324
def get_process(wanted)
  matching_processes = processes.reverse.find{ |name, _| name == wanted }
  raise ArgumentError.new("No process named '#{wanted}' has been started") unless matching_processes
  matching_processes.last
end
in_current_dir(&block) click to toggle source
# File lib/aruba/api.rb, line 13
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 363
def io_wait
  @aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS
end
mod?(mode, name) click to toggle source
# File lib/aruba/api.rb, line 67
def mod?(mode, name)
  in_current_dir do
    mode == sprintf( "%o", File::Stat.new(name).mode )[-4,4].to_i(8)
  end
end
only_processes() click to toggle source
# File lib/aruba/api.rb, line 330
def only_processes
  processes.collect{ |_, process| process }
end
original_env() click to toggle source
# File lib/aruba/api.rb, line 462
def original_env
  @original_env ||= {}
end
output_from(cmd) click to toggle source
# File lib/aruba/api.rb, line 200
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 44
def overwrite_file(file_name, file_content)
  _create_file(file_name, file_content, true)
end
pipe_in_file(file) click to toggle source
# File lib/aruba/api.rb, line 126
def pipe_in_file(file)
  in_current_dir do
    File.open(file, 'r').each_line do |line|
      _write_interactive(line)
    end
  end
end
prep_for_fs_check(&block) click to toggle source
# File lib/aruba/api.rb, line 177
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 303
def processes
  @processes ||= []
end
regexp(string_or_regexp) click to toggle source
# File lib/aruba/api.rb, line 196
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 320
def register_process(name, process)
  processes << [name, process]
end
remove_dir(directory_name) click to toggle source
# File lib/aruba/api.rb, line 100
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 81
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 456
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 334
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 375
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 367
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 450
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 210
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 205
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 307
def stop_processes!
  processes.each do |_, process|
    stop_process(process)
  end
end
terminate_processes!() click to toggle source
# File lib/aruba/api.rb, line 313
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 379
def type(input)
  return close_input if "" == input
  _write_interactive(_ensure_newline(input))
end
unescape(string) click to toggle source
# File lib/aruba/api.rb, line 190
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 444
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 426
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 142
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 36
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 40
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 483
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 469
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 475
def stop_process(process)
  @last_exit_status = process.stop(announcer)
end
terminate_process(process) click to toggle source
# File lib/aruba/api.rb, line 479
def terminate_process(process)
  process.terminate
end