spec_helper.rb 4.2 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__)
R
Robert Speicher 已提交
8
require 'rspec/rails'
R
Robert Speicher 已提交
9
require 'shoulda/matchers'
K
Kamil Trzcinski 已提交
10
require 'rspec/retry'
K
Kamil Trzcinski 已提交
11

12
rspec_profiling_is_configured =
13
  ENV['RSPEC_PROFILING_POSTGRES_URL'].present? ||
14 15
  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
R
Robert Speicher 已提交
28

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

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

RSpec.configure do |config|
J
Jeroen van Baarsen 已提交
37 38
  config.use_transactional_fixtures = false
  config.use_instantiated_fixtures  = false
R
Robert Speicher 已提交
39 40
  config.mock_with :rspec

K
Kamil Trzcinski 已提交
41 42 43
  config.verbose_retry = true
  config.display_try_failure_messages = true

44 45
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::ControllerHelpers, type: :view
46
  config.include Devise::Test::IntegrationHelpers, type: :feature
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
R
Robert Speicher 已提交
51
  config.include StubConfiguration
52
  config.include EmailHelpers, type: :mailer
R
Robert Speicher 已提交
53
  config.include TestEnv
V
Valery Sizov 已提交
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
  config.include Gitlab::Routing, type: :routing
60
  config.include MigrationsHelpers, :migration
61
  config.include StubFeatureFlags
R
Robert Speicher 已提交
62

63
  config.infer_spec_type_from_file_location!
64 65 66 67 68

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

69
  config.raise_errors_for_deprecations!
R
Robert Speicher 已提交
70 71

  config.before(:suite) do
72
    TestEnv.init
R
Robert Speicher 已提交
73
  end
74

75 76 77 78
  config.after(:suite) do
    TestEnv.cleanup
  end

79 80 81
  config.before(:example) do
    # Skip pre-receive hook check so we can use the web editor and merge.
    allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, nil])
82 83
    # Enable all features by default for testing
    allow(Feature).to receive(:enabled?) { true }
84 85
  end

86 87 88 89 90 91 92 93 94
  config.before(:example, :request_store) do
    RequestStore.begin!
  end

  config.after(:example, :request_store) do
    RequestStore.end!
    RequestStore.clear!
  end

95
  if ENV['CI']
96 97
    config.around(:each) do |ex|
      ex.run_with_retry retry: 2
98 99 100
    end
  end

101
  config.around(:each, :use_clean_rails_memory_store_caching) do |example|
102
    caching_store = Rails.cache
103 104
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

105
    example.run
106

107 108
    Rails.cache = caching_store
  end
109

110 111 112 113 114 115 116 117 118 119
  config.around(:each, :clean_gitlab_redis_cache) do |example|
    Gitlab::Redis::Cache.with(&:flushall)

    example.run

    Gitlab::Redis::Cache.with(&:flushall)
  end

  config.around(:each, :clean_gitlab_redis_shared_state) do |example|
    Gitlab::Redis::SharedState.with(&:flushall)
120 121
    Sidekiq.redis(&:flushall)

122
    example.run
123

124
    Gitlab::Redis::SharedState.with(&:flushall)
125
    Sidekiq.redis(&:flushall)
126
  end
127

128 129 130 131 132 133 134
  config.before(:example, :migration) do
    ActiveRecord::Migrator
      .migrate(migrations_paths, previous_migration.version)
  end

  config.after(:example, :migration) do
    ActiveRecord::Migrator.migrate(migrations_paths)
135
  end
136

137
  config.around(:each, :nested_groups) do |example|
138
    example.run if Group.supports_nested_groups?
139 140 141 142 143
  end

  config.around(:each, :postgresql) do |example|
    example.run if Gitlab::Database.postgresql?
  end
A
Andrew8xx8 已提交
144
end
145

K
Kamil Trzcinski 已提交
146 147 148 149
FactoryGirl::SyntaxRunner.class_eval do
  include RSpec::Mocks::ExampleMethods
end

150
ActiveRecord::Migration.maintain_test_schema!
151 152 153 154 155 156 157

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end