spec_helper.rb 5.68 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's avatar
Robert Speicher committed
8
require 'rspec/rails'
9
require 'shoulda/matchers'
Kamil Trzcinski's avatar
Kamil Trzcinski committed
10
require 'rspec/retry'
11
require 'rspec-parameterized'
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 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
52
  config.include StubConfiguration
53
  config.include EmailHelpers, :mailer, type: :mailer
Robert Speicher's avatar
Robert Speicher committed
54
  config.include TestEnv
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
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!
Robert Speicher's avatar
Robert Speicher committed
80

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

Robert Speicher's avatar
Robert Speicher committed
87
  config.before(:suite) do
Rémy Coutable's avatar
Rémy Coutable committed
88
    Timecop.safe_mode = true
89
    TestEnv.init
Robert Speicher's avatar
Robert Speicher committed
90
  end
91

92 93 94 95
  config.after(:suite) do
    TestEnv.cleanup
  end

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

103 104 105 106 107 108 109 110 111
  config.before(:example, :request_store) do
    RequestStore.begin!
  end

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

112 113 114 115
  config.before(:example, :mailer) do
    reset_delivered_emails!
  end

116 117 118 119 120 121 122 123 124 125 126 127
  # 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

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

132
    example.run
133

134 135
    Rails.cache = caching_store
  end
136

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

149
    example.run
150

151
    Gitlab::Redis::SharedState.with(&:flushall)
152
    Sidekiq.redis(&:flushall)
153
  end
154

155
  config.before(:each, :migration) do
156
    schema_migrate_down!
157 158
  end

159
  config.after(:context, :migration) do
160
    schema_migrate_up!
161
  end
162

163
  config.around(:each, :nested_groups) do |example|
164
    example.run if Group.supports_nested_groups?
165 166 167 168 169
  end

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

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
190 191 192 193
FactoryGirl::SyntaxRunner.class_eval do
  include RSpec::Mocks::ExampleMethods
end

194
ActiveRecord::Migration.maintain_test_schema!
195 196 197 198 199 200 201

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