test/integration/test_helper.rb in gitdocs-0.5.0 vs test/integration/test_helper.rb in gitdocs-0.6.0

- old
+ new

@@ -1,106 +1,180 @@ # -*- encoding : utf-8 -*- require 'rubygems' require 'minitest/autorun' -$LOAD_PATH.unshift File.expand_path('../../lib') -require 'gitdocs' require 'aruba' require 'aruba/api' require 'timeout' require 'capybara' require 'capybara_minitest_spec' +require 'capybara/dsl' require 'capybara/poltergeist' +require 'find' +require 'gitdocs/version' +Dir.glob(File.expand_path('../../support/**/*.rb', __FILE__)).each do |filename| + require_relative filename +end -Capybara.app_host = 'http://localhost:7777/' -Capybara.default_driver = :poltergeist -Capybara.run_server = false -Capybara.default_wait_time = 20 +Capybara.app_host = 'http://localhost:7777/' +Capybara.default_driver = :poltergeist +Capybara.run_server = false +Capybara.default_max_wait_time = ENV['TRAVIS'] ? 120 : 30 Capybara.register_driver :poltergeist do |app| - Capybara::Poltergeist::Driver.new(app, timeout: 20) + Capybara::Poltergeist::Driver.new( + app, + timeout: Capybara.default_max_wait_time, + # Disable catching javascript errors because of the possibility of errors + # from the Ace code. + js_errors: false + ) end -module MiniTest::Aruba - class ArubaApiWrapper - include Aruba::Api - end +PID_FILE = File.expand_path('../../../tmp/gitdocs.pid', __FILE__) - def aruba - @aruba ||= ArubaApiWrapper.new - end +module MiniTest + module Aruba + class ArubaApiWrapper + include ::Aruba::Api + end - def run(*args) - if args.length == 0 - super - else - aruba.run(*args) + def aruba + @aruba ||= ArubaApiWrapper.new end - end - def method_missing(method, *args, &block) - aruba.send(method, *args, &block) - end + def run(*args) + if args.empty? + super + else + aruba.run(*args) + end + end - def before_setup - super - original = (ENV['PATH'] || '').split(File::PATH_SEPARATOR) - set_env('PATH', ([File.expand_path('bin')] + original).join(File::PATH_SEPARATOR)) - FileUtils.rm_rf(current_dir) + def method_missing(method, *args, &block) + aruba.send(method, *args, &block) + end end - - def after_teardown - super - restore_env - processes.clear - end end module Helper include MiniTest::Aruba include Capybara::DSL include Capybara::RSpecMatchers def before_setup - super + clean_current_dir + + # HACK: In order to ensure that rugged/libgit2 see the expected HOME + # directory we must set it before requiring rugged. This seems to occur + # because libgit2 reads HOME only one the initial load. set_env('HOME', abs_current_dir) - ENV['TEST'] = nil + require 'rugged' + + # Make sure that we are not accidentally overwriting an existing gitconfig. + if Rugged::Config.global['user.name'] || Rugged::Config.global['user.name'] + puts <<-EOS.gsub(/^\s{8}/, '') + Unexpected git config: + user.name = #{Rugged::Config.global['user.name']} + user.email = #{Rugged::Config.global['user.email']} + Something went wrong when setting the HOME directory and the test + will not execute in isolation.' + EXITING + EOS + exit + end + + GitFactory.working_directory = abs_current_dir + Rugged::Config.global['user.name'] = GitFactory.users[0][:name] + Rugged::Config.global['user.email'] = GitFactory.users[0][:email] end def teardown Capybara.reset_sessions! Capybara.use_default_driver end def after_teardown - super + restore_env + processes.clear terminate_processes! prep_for_fs_check do - next unless File.exist?('gitdocs.pid') + next unless File.exist?(PID_FILE) - pid = IO.read('gitdocs.pid').to_i - Process.kill('KILL', pid) + pid = IO.read(PID_FILE).to_i begin + Process.kill('KILL', pid) + rescue Errno::ESRCH # rubocop:disable Lint/HandleExceptions + # Nothing to do since the process is already gone. + end + + begin Process.wait(pid) rescue SystemCallError # rubocop:disable Lint/HandleExceptions # This means that the process is already gone. # Nothing to do. end + FileUtils.rm_rf(PID_FILE) end + + return if passed? + + # Report gitdocs execution details on failure + puts "\n\n----------------------------------" + puts "Aruba details for failure: #{name}" + puts failures.inspect.to_s + + log_filename = File.join(abs_current_dir, '.gitdocs', 'log') + if File.exist?(log_filename) + puts '----------------------------------' + puts "Log file: #{log_filename}" + puts File.read(log_filename) + end + + if Dir.exist?(abs_current_dir) + puts '----------------------------------' + puts 'Aruba current directory file list:' + Find.find(abs_current_dir) do |path| + Find.prune if path =~ %r{.git/?$} + puts " #{path}" + end + end + + puts "----------------------------------\n\n" end - def start_daemon - configuration = Gitdocs::Configuration.new - configuration.shares.each do |share| + # @param [String] method pass to the CLI + # @param [String] arguments which will be passed to the CLI in addition + # @param [String] expected_output that the CLI should return + # + # @return [String] full text of the command being executed + def gitdocs_command(method, arguments, expected_output) + binary_path = File.expand_path('../../../bin/gitdocs', __FILE__) + full_command = "#{binary_path} #{method} #{arguments} --pid=#{PID_FILE}" + + run(full_command, Capybara.default_max_wait_time) + assert_success(true) + assert_partial_output(expected_output, output_from(full_command)) + + full_command + end + + # @return [void] + def gitdocs_start + # FIXME: Calling internal funcations directly because we cannot currently + # set polling or notification on the CLI. After that has been added this + # should be removed. [ASC 2015-10-26] + require 'gitdocs/initializer' + require 'gitdocs/share' + Gitdocs::Initializer.initialize_database + Gitdocs::Share.all.each do |share| share.update_attributes(polling_interval: 0.1, notification: false) end - start_cmd = 'gitdocs start --debug --pid=gitdocs.pid --port 7777' - run(start_cmd, 15) - assert_success(true) - assert_partial_output('Started gitdocs', output_from(start_cmd)) + FileUtils.rm_rf(PID_FILE) + gitdocs_command('start', '--verbose --port=7777', 'Started gitdocs') end # @overload abs_current_dir # @return [String] absolute path for current_dir # @overload abs_current_dir(relative_path) @@ -109,130 +183,90 @@ def abs_current_dir(relative_path = nil) return File.absolute_path(File.join(current_dir)) unless relative_path File.absolute_path(File.join(current_dir, relative_path)) end - # @return [String] the absolute path for the repository - def git_init_local(path = 'local') - abs_path = abs_current_dir(path) - Rugged::Repository.init_at(abs_path) - abs_path + # @param [String] path + # + # @return [#gitdocs_command] + def gitdocs_add(path) + GitFactory.init(path) + gitdocs_command('add', path, "Added path #{path} to doc list") end - # @return [String] the absolute path for the repository - def git_init_remote(path = 'remote') - abs_path = abs_current_dir(path) - Rugged::Repository.init_at(abs_path, :bare) - abs_path - end + # @param [Array<String>] destination_paths + # + # @return [void] + def gitdocs_create_from_remote(*destination_paths) + full_destination_paths = destination_paths.map { |x| GitFactory.expand_path(x) } + remote_repository_path = GitFactory.init_bare(:remote) - def wait_for_clean_workdir(path) - dirty = true - Timeout.timeout(20) do - while dirty - begin - sleep(0.1) - rugged = Rugged::Repository.new(abs_current_dir(path)) - dirty = !rugged.diff_workdir(rugged.head.target, include_untracked: true).deltas.empty? - rescue Rugged::ReferenceError - nil - rescue Rugged::InvalidError - nil - rescue Rugged::RepositoryError - nil - end - end + full_destination_paths.each do |destination_path| + gitdocs_command( + 'create', + "#{destination_path} #{remote_repository_path}", + "Added path #{destination_path} to doc list" + ) end - rescue Timeout::Error - assert(false, "#{path} workdir is still dirty") end - def wait_for_exact_file_content(file, exact_content) - in_current_dir do - begin - Timeout.timeout(20) do - sleep(0.1) until File.exist?(file) && IO.read(file) == exact_content - end - rescue Timeout::Error - nil - end - - assert(File.exist?(file), "Missing #{file}") - actual_content = IO.read(file) - assert( - actual_content == exact_content, - "Expected #{file} content: #{exact_content}\nActual content #{actual_content}" - ) + # @param [Array<String>] expected_outputs + # + # @return [void] + def gitdocs_assert_status_contains(*expected_outputs) + command = gitdocs_command('status', '', Gitdocs::VERSION) + expected_outputs.each do |expected_output| + assert_partial_output(expected_output, output_from(command)) end end - def wait_for_directory(path) - in_current_dir do - begin - Timeout.timeout(20) { sleep(0.1) until Dir.exist?(path) } - rescue Timeout::Error - nil - end - - assert(Dir.exist?(path), "Missing #{path}") + # @param [Array<String>] not_expected_outputs + # + # @return [void] + def gitdocs_assert_status_not_contain(*not_expected_outputs) + command = gitdocs_command('status', '', Gitdocs::VERSION) + not_expected_outputs.each do |not_expected_output| + assert_no_partial_output(not_expected_output, output_from(command)) end end - def wait_for_conflict_markers(path) - in_current_dir do + # @overload wait_for_assert + # @yield to a block which executes Minitest assertion + # + # @overload wait_for_assert(interval) + # @param [Float] interval + # @yield to a block which executes Minitest assertion + # + # @raise [Minitest::Assertion] + # + # @return [void] + def wait_for_assert(interval = 0.1) + Timeout.timeout(Capybara.default_max_wait_time) do begin - Timeout.timeout(20) { sleep(0.1) if File.exist?(path) } - rescue Timeout::Error - nil - ensure - assert(!File.exist?(path), "#{path} should have been removed") + yield + rescue Minitest::Assertion, Capybara::Poltergeist::Error + sleep(interval) + retry end - - begin - Timeout.timeout(20) { sleep(0.1) if Dir.glob("#{path} (*)").empty? } - rescue Timeout::Error - nil - ensure - assert(!Dir.glob("#{path} (*)").empty?, "#{path} conflict marks should have been created") - end end + rescue Timeout::Error + yield end - def gitdocs_add(path = 'local') - add_cmd = "gitdocs add #{path} --pid=gitdocs.pid" - run_simple(add_cmd, true, 15) - assert_success(true) - assert_partial_output("Added path #{path} to doc list", output_from(add_cmd)) - end - - def git_clone_and_gitdocs_add(remote_path, *clone_paths) - clone_paths.each do |clone_path| - abs_clone_path = abs_current_dir(clone_path) - FileUtils.rm_rf(abs_clone_path) - repo = Rugged::Repository.clone_at( - "file://#{remote_path}", - abs_clone_path - ) - repo.config['user.email'] = 'afish@example.com' - repo.config['user.name'] = 'Art T. Fish' - gitdocs_add(clone_path) + # @param [String] locator + # + # @raise [Minitest::Assertion] + # + # @return [void] + def visit_and_click_link(locator) + wait_for_assert(1) do + visit('http://localhost:7777/') + click_link(locator) end end - - def gitdocs_status - @status_cmd = 'gitdocs status --pid=gitdocs.pid' - run(@status_cmd, 15) - assert_success(true) - end - - def assert_gitdocs_status_contains(expected) - assert_partial_output(expected, output_from(@status_cmd)) - end - - def assert_gitdocs_status_not_contain(expected) - assert_no_partial_output(expected, output_from(@status_cmd)) - end end -class MiniTest::Spec - include Helper +module MiniTest + class Spec + include Helper + end end