# 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
# 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
# File lib/aruba/api.rb, line 313 def _ensure_newline(str) str.chomp << "\n" end
# File lib/aruba/api.rb, line 130 def _mkdir(dir_name) FileUtils.mkdir_p(dir_name) unless File.directory?(dir_name) end
# File lib/aruba/api.rb, line 309 def _write_interactive(input) @interactive.stdin.write(input) end
# File lib/aruba/api.rb, line 167 def all_output all_stdout << all_stderr end
# File lib/aruba/api.rb, line 162 def all_stderr stop_processes! only_processes.inject("") { |out, ps| out << ps.stderr(@aruba_keep_ansi) } end
# File lib/aruba/api.rb, line 157 def all_stdout stop_processes! only_processes.inject("") { |out, ps| out << ps.stdout(@aruba_keep_ansi) } end
# File lib/aruba/api.rb, line 317 def announce_or_puts(msg) if(@puts) Kernel.puts(msg) else puts(msg) end end
# File lib/aruba/api.rb, line 228 def append_output_to(message) "#{message} Output:\n\n#{all_output}\n" end
# 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
# File lib/aruba/api.rb, line 171 def assert_exact_output(expected, actual) unescape(actual).should == unescape(expected) end
# 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
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
# 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
# File lib/aruba/api.rb, line 195 def assert_failing_with(expected) assert_exit_status_and_partial_output(false, expected) end
# File lib/aruba/api.rb, line 179 def assert_matching_output(expected, actual) unescape(actual).should =~ /#{unescape(expected)}/ end
# 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
# 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
# File lib/aruba/api.rb, line 175 def assert_partial_output(expected, actual) unescape(actual).should include(unescape(expected)) end
# File lib/aruba/api.rb, line 191 def assert_passing_with(expected) assert_exit_status_and_partial_output(true, expected) end
# File lib/aruba/api.rb, line 214 def assert_success(success) success ? assert_exit_status(0) : assert_not_exit_status(0) end
# 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
# 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
# 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
# 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
# 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
# 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
# File lib/aruba/api.rb, line 71 def create_dir(dir_name) in_current_dir do _mkdir(dir_name) end end
# File lib/aruba/api.rb, line 17 def current_dir File.join(*dirs) end
# File lib/aruba/api.rb, line 333 def current_ruby File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']) end
# File lib/aruba/api.rb, line 325 def detect_ruby(cmd) if cmd =~ /^ruby\s/ cmd.gsub(/^ruby\s/, "#{current_ruby} ") else cmd end end
# File lib/aruba/api.rb, line 26 def dirs @dirs ||= ['tmp/aruba'] end
# File lib/aruba/api.rb, line 283 def exit_timeout @aruba_timeout_seconds || DEFAULT_TIMEOUT_SECONDS end
# File lib/aruba/api.rb, line 253 def get_process(wanted) processes.reverse.find{ |name, _| name == wanted }[-1] end
# File lib/aruba/api.rb, line 12 def in_current_dir(&block) _mkdir(current_dir) Dir.chdir(current_dir, &block) end
# File lib/aruba/api.rb, line 289 def io_wait @aruba_io_wait_seconds || DEFAULT_IO_WAIT_SECONDS end
# File lib/aruba/api.rb, line 257 def only_processes processes.collect{ |_, process| process } end
# File lib/aruba/api.rb, line 373 def original_env @original_env ||= {} end
# File lib/aruba/api.rb, line 142 def output_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).output(@aruba_keep_ansi) end
# File lib/aruba/api.rb, line 38 def overwrite_file(file_name, file_content) _create_file(file_name, file_content, true) end
# File lib/aruba/api.rb, line 125 def prep_for_fs_check(&block) stop_processes! in_current_dir{ block.call } end
# File lib/aruba/api.rb, line 232 def processes @processes ||= [] end
# 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
# File lib/aruba/api.rb, line 249 def register_process(name, process) processes << [name, process] end
# File lib/aruba/api.rb, line 58 def remove_file(file_name) in_current_dir do FileUtils.rm(file_name) end end
# File lib/aruba/api.rb, line 367 def restore_env original_env.each do |key, value| ENV[key] = value end end
# 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
# File lib/aruba/api.rb, line 301 def run_interactive(cmd) @interactive = run(cmd) end
# 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
# 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
# File lib/aruba/api.rb, line 152 def stderr_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).stderr(@aruba_keep_ansi) end
# File lib/aruba/api.rb, line 147 def stdout_from(cmd) cmd = detect_ruby(cmd) get_process(cmd).stdout(@aruba_keep_ansi) end
# File lib/aruba/api.rb, line 236 def stop_processes! processes.each do |_, process| stop_process(process) end end
# File lib/aruba/api.rb, line 242 def terminate_processes! processes.each do |_, process| terminate_process(process) stop_process(process) end end
# File lib/aruba/api.rb, line 305 def type(input) _write_interactive(_ensure_newline(input)) end
# File lib/aruba/api.rb, line 134 def unescape(string) string.gsub('\n', "\n").gsub('\"', '"').gsub('\e', "\e") end
# 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
# File lib/aruba/api.rb, line 337 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
# File lib/aruba/api.rb, line 30 def write_file(file_name, file_content) _create_file(file_name, file_content, false) end
# 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
# File lib/aruba/api.rb, line 394 def announcer Announcer.new(self, :stdout => @announce_stdout, :stderr => @announce_stderr, :dir => @announce_dir, :cmd => @announce_cmd, :env => @announce_env) end
TODO: move some more methods under here!
# File lib/aruba/api.rb, line 380 def last_exit_status return @last_exit_status if @last_exit_status stop_processes! @last_exit_status end
# File lib/aruba/api.rb, line 386 def stop_process(process) @last_exit_status = process.stop(announcer, @aruba_keep_ansi) end
# File lib/aruba/api.rb, line 390 def terminate_process(process) process.terminate(@aruba_keep_ansi) end