spec_helper.rb 3.09 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
# ENV['prometheus_multiproc_dir'] = 'tmp/prometheus_multiproc_dir_test'
7

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

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

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

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

30 31 32
# require rainbow gem String monkeypatch, so we can test SystemChecks
require 'rainbow/ext/string'

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

RSpec.configure do |config|
Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
38 39
  config.use_transactional_fixtures = false
  config.use_instantiated_fixtures  = false
Robert Speicher's avatar
Robert Speicher committed
40 41
  config.mock_with :rspec

Kamil Trzcinski's avatar
Kamil Trzcinski committed
42 43 44
  config.verbose_retry = true
  config.display_try_failure_messages = true

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

60
  config.infer_spec_type_from_file_location!
61 62 63 64 65

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

66
  config.raise_errors_for_deprecations!
Robert Speicher's avatar
Robert Speicher committed
67 68

  config.before(:suite) do
69
    TestEnv.init
Robert Speicher's avatar
Robert Speicher committed
70
  end
71

72 73 74 75
  config.after(:suite) do
    TestEnv.cleanup
  end

76 77 78 79 80 81 82
  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

83 84 85 86 87 88
  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
89 90 91

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

94
    example.run
95

96
    Gitlab::Redis.with(&:flushall)
97
    Sidekiq.redis(&:flushall)
98
  end
99 100

  config.around(:each, :nested_groups) do |example|
101
    example.run if Group.supports_nested_groups?
102 103 104 105 106
  end

  config.around(:each, :postgresql) do |example|
    example.run if Gitlab::Database.postgresql?
  end
Andrew8xx8's avatar
Andrew8xx8 committed
107
end
108

Kamil Trzcinski's avatar
Kamil Trzcinski committed
109 110 111 112
FactoryGirl::SyntaxRunner.class_eval do
  include RSpec::Mocks::ExampleMethods
end

113
ActiveRecord::Migration.maintain_test_schema!