spec_helper.rb 5.7 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
  config.include WaitForRequests, :js
R
Robert Speicher 已提交
52
  config.include StubConfiguration
53
  config.include EmailHelpers, :mailer, type: :mailer
R
Robert Speicher 已提交
54
  config.include TestEnv
V
Valery Sizov 已提交
55
  config.include ActiveJob::TestHelper
56
  config.include ActiveSupport::Testing::TimeHelpers
57 58
  config.include StubGitlabCalls
  config.include StubGitlabData
59
  config.include ApiHelpers, :api
60
  config.include Gitlab::Routing, type: :routing
61
  config.include MigrationsHelpers, :migration
62
  config.include StubFeatureFlags
63
  config.include StubENV
R
Robert Speicher 已提交
64

65
  config.infer_spec_type_from_file_location!
66

67 68 69 70 71 72 73 74 75 76
  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
77 78
  end

79
  config.raise_errors_for_deprecations!
R
Robert Speicher 已提交
80

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

R
Robert Speicher 已提交
90
  config.before(:suite) do
R
Rémy Coutable 已提交
91
    Timecop.safe_mode = true
92
    TestEnv.init
R
Robert Speicher 已提交
93
  end
94

95 96 97 98
  config.after(:suite) do
    TestEnv.cleanup
  end

99 100 101
  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])
102 103
    # Enable all features by default for testing
    allow(Feature).to receive(:enabled?) { true }
104 105
  end

106 107 108 109 110 111 112 113 114
  config.before(:example, :request_store) do
    RequestStore.begin!
  end

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

115 116 117 118
  config.before(:example, :mailer) do
    reset_delivered_emails!
  end

119 120 121 122 123 124 125 126 127 128 129 130
  # Stub the `ForkedStorageCheck.storage_available?` method unless
  # `:broken_storage` metadata is defined
  #
  # This check can be slow and is unnecessary in a test environment where we
  # know the storage is available, because we create it at runtime
  config.before(:example) do |example|
    unless example.metadata[:broken_storage]
      allow(Gitlab::Git::Storage::ForkedStorageCheck)
        .to receive(:storage_available?).and_return(true)
    end
  end

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

135
    example.run
136

137 138
    Rails.cache = caching_store
  end
139

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

152
    example.run
153

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

158
  config.before(:each, :migration) do
159
    schema_migrate_down!
160 161
  end

162
  config.after(:context, :migration) do
163
    schema_migrate_up!
164
  end
165

166
  config.around(:each, :nested_groups) do |example|
167
    example.run if Group.supports_nested_groups?
168 169 170 171 172
  end

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

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
# 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

K
Kamil Trzcinski 已提交
193 194 195 196
FactoryGirl::SyntaxRunner.class_eval do
  include RSpec::Mocks::ExampleMethods
end

197
ActiveRecord::Migration.maintain_test_schema!
198 199 200 201 202 203 204

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