X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7a98271d94163cdc4afa5bfcf275db353bc062d2..002f863605469cee7e112f4c16a78e3b6278672c:/apps/workbench/test/test_helper.rb diff --git a/apps/workbench/test/test_helper.rb b/apps/workbench/test/test_helper.rb index 145914f741..05be43cb5f 100644 --- a/apps/workbench/test/test_helper.rb +++ b/apps/workbench/test/test_helper.rb @@ -1,4 +1,24 @@ ENV["RAILS_ENV"] = "test" +unless ENV["NO_COVERAGE_TEST"] + begin + require 'simplecov' + require 'simplecov-rcov' + class SimpleCov::Formatter::MergedFormatter + def format(result) + SimpleCov::Formatter::HTMLFormatter.new.format(result) + SimpleCov::Formatter::RcovFormatter.new.format(result) + end + end + SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter + SimpleCov.start do + add_filter '/test/' + add_filter 'initializers/secret_token' + end + rescue Exception => e + $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without." + end +end + require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' @@ -6,13 +26,21 @@ $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__) SERVER_PID_PATH = 'tmp/pids/server.pid' class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. + # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in + # alphabetical order. # - # Note: You'll currently still have to declare fixtures explicitly in integration tests - # -- they do not yet inherit this setting + # Note: You'll currently still have to declare fixtures explicitly + # in integration tests -- they do not yet inherit this setting fixtures :all + def use_token token_name + auth = api_fixture('api_client_authorizations')[token_name.to_s] + Thread.current[:arvados_api_token] = auth['api_token'] + end - # Add more helper methods to be used by all tests here... + def teardown + Thread.current[:arvados_api_token] = nil + super + end end module ApiFixtureLoader @@ -46,35 +74,32 @@ class ActiveSupport::TestCase end class ApiServerBackedTestRunner < MiniTest::Unit - # Make a hash that unsets Bundle's environment variables. - # We'll use this environment when we launch Bundle commands in the API - # server. Otherwise, those commands will try to use Workbench's gems, etc. - @@APIENV = Hash[ENV.map { |key, val| - (key =~ /^BUNDLE_/) ? [key, nil] : nil - }.compact] - def _system(*cmd) - if not system(@@APIENV, *cmd) - raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}" + Bundler.with_clean_env do + if not system({'RAILS_ENV' => 'test'}, *cmd) + raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}" + end end end def _run(args=[]) Capybara.javascript_driver = :poltergeist server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir| + ENV["NO_COVERAGE_TEST"] = "1" _system('bundle', 'exec', 'rake', 'db:test:load') _system('bundle', 'exec', 'rake', 'db:fixtures:load') _system('bundle', 'exec', 'rails', 'server', '-d') timeout = Time.now.tv_sec + 10 - begin + good_pid = false + while (not good_pid) and (Time.now.tv_sec < timeout) sleep 0.2 begin server_pid = IO.read(SERVER_PID_PATH).to_i - good_pid = (server_pid > 0) and (Process.kill(0, pid) rescue false) + good_pid = (server_pid > 0) and (Process.kill(0, server_pid) rescue false) rescue Errno::ENOENT good_pid = false end - end while (not good_pid) and (Time.now.tv_sec < timeout) + end if not good_pid raise RuntimeError, "could not find API server Rails pid" end