Refuse to run rake tasks without "bundle exec".
[arvados.git] / apps / workbench / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
3   begin
4     require 'simplecov'
5     require 'simplecov-rcov'
6     class SimpleCov::Formatter::MergedFormatter
7       def format(result)
8         SimpleCov::Formatter::HTMLFormatter.new.format(result)
9         SimpleCov::Formatter::RcovFormatter.new.format(result)
10       end
11     end
12     SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
13     SimpleCov.start do
14       add_filter '/test/'
15       add_filter 'initializers/secret_token'
16     end
17   rescue Exception => e
18     $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
19   end
20 end
21
22 require File.expand_path('../../config/environment', __FILE__)
23 require 'rails/test_help'
24
25 $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
26 SERVER_PID_PATH = 'tmp/pids/server.pid'
27
28 class ActiveSupport::TestCase
29   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
30   # alphabetical order.
31   #
32   # Note: You'll currently still have to declare fixtures explicitly
33   # in integration tests -- they do not yet inherit this setting
34   fixtures :all
35   def use_token token_name
36     auth = api_fixture('api_client_authorizations')[token_name.to_s]
37     Thread.current[:arvados_api_token] = auth['api_token']
38   end
39
40   def teardown
41     Thread.current[:arvados_api_token] = nil
42     super
43   end
44 end
45
46 module ApiFixtureLoader
47   def self.included(base)
48     base.extend(ClassMethods)
49   end
50
51   module ClassMethods
52     @@api_fixtures = {}
53     def api_fixture(name)
54       # Returns the data structure from the named API server test fixture.
55       @@api_fixtures[name] ||= \
56       begin
57         path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml")
58         YAML.load(IO.read(path))
59       end
60     end
61   end
62   def api_fixture name
63     self.class.api_fixture name
64   end
65 end
66
67 class ActiveSupport::TestCase
68   include ApiFixtureLoader
69   def session_for api_client_auth_name
70     {
71       arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
72     }
73   end
74 end
75
76 class ApiServerBackedTestRunner < MiniTest::Unit
77   # Make a hash that unsets Bundle's environment variables.
78   # We'll use this environment when we launch Bundle commands in the API
79   # server.  Otherwise, those commands will try to use Workbench's gems, etc.
80   @@APIENV = Hash[ENV.map { |key, val|
81                     (key =~ /^BUNDLE_/) ? [key, nil] : nil
82                   }.compact]
83
84   def _system(*cmd)
85     if not system(@@APIENV, *cmd)
86       raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
87     end
88   end
89
90   def _run(args=[])
91     Capybara.javascript_driver = :poltergeist
92     server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir|
93       ENV["NO_COVERAGE_TEST"] = "1"
94       _system('bundle', 'exec', 'rake', 'db:test:load')
95       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
96       _system('bundle', 'exec', 'rails', 'server', '-d')
97       timeout = Time.now.tv_sec + 10
98       begin
99         sleep 0.2
100         begin
101           server_pid = IO.read(SERVER_PID_PATH).to_i
102           good_pid = (server_pid > 0) and (Process.kill(0, pid) rescue false)
103         rescue Errno::ENOENT
104           good_pid = false
105         end
106       end while (not good_pid) and (Time.now.tv_sec < timeout)
107       if not good_pid
108         raise RuntimeError, "could not find API server Rails pid"
109       end
110       server_pid
111     end
112     begin
113       super(args)
114     ensure
115       Process.kill('TERM', server_pid)
116     end
117   end
118 end
119
120 MiniTest::Unit.runner = ApiServerBackedTestRunner.new