BigW Consortium Gitlab

spec_helper.rb 2.74 KB
Newer Older
1 2
require './spec/simplecov_env'
SimpleCovEnv.start!
3

4
ENV["RAILS_ENV"] ||= 'test'
5
ENV["IN_MEMORY_APPLICATION_SETTINGS"] = 'true'
6

7
require File.expand_path("../../config/environment", __FILE__)
Robert Speicher committed
8
require 'rspec/rails'
9
require 'shoulda/matchers'
Kamil Trzcinski committed
10
require 'rspec/retry'
Kamil Trzcinski committed
11

12 13 14 15
rspec_profiling_is_configured =
  ENV['RSPEC_PROFILING_POSTGRES_URL'] ||
  ENV['RSPEC_PROFILING']
branch_can_be_profiled =
16 17 18
  ENV['GITLAB_DATABASE'] == 'postgresql' &&
  (ENV['CI_COMMIT_REF_NAME'] == 'master' ||
    ENV['CI_COMMIT_REF_NAME'] =~ /rspec-profile/)
19 20

if rspec_profiling_is_configured && (!ENV.key?('CI') || branch_can_be_profiled)
21 22 23
  require 'rspec_profiling/rspec'
end

24
if ENV['CI'] && !ENV['NO_KNAPSACK']
25 26 27
  require 'knapsack'
  Knapsack::Adapters::RSpecAdapter.bind
end
Robert Speicher committed
28 29 30

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
31
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Robert Speicher committed
32 33

RSpec.configure do |config|
Jeroen van Baarsen committed
34 35
  config.use_transactional_fixtures = false
  config.use_instantiated_fixtures  = false
Robert Speicher committed
36 37
  config.mock_with :rspec

Kamil Trzcinski committed
38 39 40
  config.verbose_retry = true
  config.display_try_failure_messages = true

41 42
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::ControllerHelpers, type: :view
43
  config.include Warden::Test::Helpers, type: :request
44 45
  config.include LoginHelpers, type: :feature
  config.include SearchHelpers, type: :feature
46
  config.include WaitForRequests, :js
47
  config.include WaitForAjax, :js
48
  config.include StubConfiguration
49
  config.include EmailHelpers, type: :mailer
Robert Speicher committed
50
  config.include TestEnv
51
  config.include ActiveJob::TestHelper
52
  config.include ActiveSupport::Testing::TimeHelpers
53 54
  config.include StubGitlabCalls
  config.include StubGitlabData
55
  config.include ApiHelpers, :api
56

57
  config.infer_spec_type_from_file_location!
58 59 60 61 62

  config.define_derived_metadata(file_path: %r{/spec/requests/(ci/)?api/}) do |metadata|
    metadata[:api] = true
  end

63
  config.raise_errors_for_deprecations!
Robert Speicher committed
64 65

  config.before(:suite) do
66
    TestEnv.init
Robert Speicher committed
67
  end
68

69 70 71 72
  config.after(:suite) do
    TestEnv.cleanup
  end

73 74 75 76 77 78 79
  if ENV['CI']
    # Retry only on feature specs that use JS
    config.around :each, :js do |ex|
      ex.run_with_retry retry: 3
    end
  end

80 81 82 83 84 85
  config.around(:each, :caching) do |example|
    caching_store = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new if example.metadata[:caching]
    example.run
    Rails.cache = caching_store
  end
86 87 88

  config.around(:each, :redis) do |example|
    Gitlab::Redis.with(&:flushall)
89 90
    Sidekiq.redis(&:flushall)

91
    example.run
92

93
    Gitlab::Redis.with(&:flushall)
94
    Sidekiq.redis(&:flushall)
95
  end
Andrew8xx8 committed
96
end
97

Kamil Trzcinski committed
98 99 100 101
FactoryGirl::SyntaxRunner.class_eval do
  include RSpec::Mocks::ExampleMethods
end

102
ActiveRecord::Migration.maintain_test_schema!