spec_helper.rb 6.5 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'
11
require 'rspec-parameterized'
K
Kamil Trzcinski 已提交
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
R
Robert Speicher 已提交
29

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

R
Robert Speicher 已提交
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 }
R
Robert Speicher 已提交
36 37

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

K
Kamil Trzcinski 已提交
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 Devise::Test::IntegrationHelpers, type: :feature
48
  config.include Warden::Test::Helpers, type: :request
49 50
  config.include LoginHelpers, type: :feature
  config.include SearchHelpers, type: :feature
51 52
  config.include CookieHelper, :js
  config.include InputHelper, :js
53
  config.include SelectionHelper, :js
54
  config.include InspectRequests, :js
55
  config.include WaitForRequests, :js
56
  config.include LiveDebugger, :js
R
Robert Speicher 已提交
57
  config.include StubConfiguration
58
  config.include EmailHelpers, :mailer, type: :mailer
R
Robert Speicher 已提交
59
  config.include TestEnv
V
Valery Sizov 已提交
60
  config.include ActiveJob::TestHelper
61
  config.include ActiveSupport::Testing::TimeHelpers
62 63
  config.include StubGitlabCalls
  config.include StubGitlabData
64
  config.include ApiHelpers, :api
65
  config.include Gitlab::Routing, type: :routing
66
  config.include MigrationsHelpers, :migration
67
  config.include StubFeatureFlags
68
  config.include StubENV
R
Robert Speicher 已提交
69

70
  config.infer_spec_type_from_file_location!
71

72 73 74 75 76 77 78 79 80 81
  config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
    location = metadata[:location]

    metadata[:api] = true if location =~ %r{/spec/requests/api/}

    # do not overwrite type if it's already set
    next if metadata.key?(:type)

    match = location.match(%r{/spec/([^/]+)/})
    metadata[:type] = match[1].singularize.to_sym if match
82 83
  end

84
  config.raise_errors_for_deprecations!
R
Robert Speicher 已提交
85

86 87 88
  if ENV['CI']
    # This includes the first try, i.e. tests will be run 4 times before failing.
    config.default_retry_count = 4
89 90 91 92
    config.reporter.register_listener(
      RspecFlaky::Listener.new,
      :example_passed,
      :dump_summary)
93 94
  end

R
Robert Speicher 已提交
95
  config.before(:suite) do
R
Rémy Coutable 已提交
96
    Timecop.safe_mode = true
97
    TestEnv.init
R
Robert Speicher 已提交
98
  end
99

100 101 102
  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])
103 104 105 106 107 108 109 110 111 112

    allow_any_instance_of(Gitlab::Git::GitlabProjects).to receive(:fork_repository).and_wrap_original do |m, *args|
      m.call(*args)

      shard_path, repository_relative_path = args
      # We can't leave the hooks in place after a fork, as those would fail in tests
      # The "internal" API is not available
      FileUtils.rm_rf(File.join(shard_path, repository_relative_path, 'hooks'))
    end

113 114
    # Enable all features by default for testing
    allow(Feature).to receive(:enabled?) { true }
115 116
  end

117 118 119 120 121 122 123 124 125
  config.before(:example, :request_store) do
    RequestStore.begin!
  end

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

126 127 128 129
  config.before(:example, :mailer) do
    reset_delivered_emails!
  end

130
  config.around(:each, :use_clean_rails_memory_store_caching) do |example|
131
    caching_store = Rails.cache
132 133
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

134
    example.run
135

136 137
    Rails.cache = caching_store
  end
138

139 140 141 142 143 144 145 146 147 148
  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)
149 150
    Sidekiq.redis(&:flushall)

151
    example.run
152

153
    Gitlab::Redis::SharedState.with(&:flushall)
154
    Sidekiq.redis(&:flushall)
155
  end
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  # The :each scope runs "inside" the example, so this hook ensures the DB is in the
  # correct state before any examples' before hooks are called. This prevents a
  # problem where `ScheduleIssuesClosedAtTypeChange` (or any migration that depends
  # on background migrations being run inline during test setup) can be broken by
  # altering Sidekiq behavior in an unrelated spec like so:
  #
  # around do |example|
  #   Sidekiq::Testing.fake! do
  #     example.run
  #   end
  # end
  config.before(:context, :migration) do
    schema_migrate_down!
  end

  # Each example may call `migrate!`, so we must ensure we are migrated down every time
173
  config.before(:each, :migration) do
174
    schema_migrate_down!
175 176
  end

177
  config.after(:context, :migration) do
178
    schema_migrate_up!
179
  end
180

181
  config.around(:each, :nested_groups) do |example|
182
    example.run if Group.supports_nested_groups?
183 184 185 186 187
  end

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

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
# add simpler way to match asset paths containing digest strings
RSpec::Matchers.define :match_asset_path do |expected|
  match do |actual|
    path = Regexp.escape(expected)
    extname = Regexp.escape(File.extname(expected))
    digest_regex = Regexp.new(path.sub(extname, "(?:-\\h+)?#{extname}") << '$')
    digest_regex =~ actual
  end

  failure_message do |actual|
    "expected that #{actual} would include an asset path for #{expected}"
  end

  failure_message_when_negated do |actual|
    "expected that #{actual} would not include an asset path for  #{expected}"
  end
end

208
FactoryBot::SyntaxRunner.class_eval do
K
Kamil Trzcinski 已提交
209 210 211
  include RSpec::Mocks::ExampleMethods
end

212
ActiveRecord::Migration.maintain_test_schema!
213 214 215 216 217 218 219

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

# Prevent Rugged from picking up local developer gitconfig.
Rugged::Settings['search_path_global'] = Rails.root.join('tmp/tests').to_s