debian-mirror-gitlab/spec/spec_helper.rb

314 lines
9.4 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
require './spec/simplecov_env'
SimpleCovEnv.start!
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
ENV["RAILS_ENV"] = 'test'
2017-08-17 22:00:37 +05:30
ENV["IN_MEMORY_APPLICATION_SETTINGS"] = 'true'
2015-09-11 14:41:01 +05:30
2018-11-18 11:00:15 +05:30
require File.expand_path('../config/environment', __dir__)
2014-09-02 18:07:02 +05:30
require 'rspec/rails'
2015-09-11 14:41:01 +05:30
require 'shoulda/matchers'
2016-06-02 11:05:42 +05:30
require 'rspec/retry'
2018-03-17 18:26:18 +05:30
require 'rspec-parameterized'
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
rspec_profiling_is_configured =
2017-09-10 17:25:29 +05:30
ENV['RSPEC_PROFILING_POSTGRES_URL'].present? ||
2017-08-17 22:00:37 +05:30
ENV['RSPEC_PROFILING']
branch_can_be_profiled =
ENV['GITLAB_DATABASE'] == 'postgresql' &&
(ENV['CI_COMMIT_REF_NAME'] == 'master' ||
ENV['CI_COMMIT_REF_NAME'] =~ /rspec-profile/)
if rspec_profiling_is_configured && (!ENV.key?('CI') || branch_can_be_profiled)
require 'rspec_profiling/rspec'
end
2019-03-02 22:35:43 +05:30
if ENV['CI'] && ENV['KNAPSACK_GENERATE_REPORT'] && !ENV['NO_KNAPSACK']
require 'knapsack'
Knapsack::Adapters::RSpecAdapter.bind
end
2017-09-10 17:25:29 +05:30
# require rainbow gem String monkeypatch, so we can test SystemChecks
require 'rainbow/ext/string'
2018-11-20 20:47:30 +05:30
Rainbow.enabled = false
2017-09-10 17:25:29 +05:30
2014-09-02 18:07:02 +05:30
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
2018-10-15 14:42:47 +05:30
# Requires helpers, and shared contexts/examples first since they're used in other support files
2018-12-05 23:21:45 +05:30
# Load these first since they may be required by other helpers
require Rails.root.join("spec/support/helpers/git_helpers.rb")
# Then the rest
2018-10-15 14:42:47 +05:30
Dir[Rails.root.join("spec/support/helpers/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/support/shared_contexts/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/support/shared_examples/*.rb")].each { |f| require f }
2015-09-11 14:41:01 +05:30
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
2014-09-02 18:07:02 +05:30
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.use_instantiated_fixtures = false
2018-11-20 20:47:30 +05:30
config.fixture_path = Rails.root
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
config.verbose_retry = true
config.display_try_failure_messages = true
2015-04-26 12:48:37 +05:30
config.infer_spec_type_from_file_location!
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
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
2017-08-17 22:00:37 +05:30
end
2019-05-18 00:54:41 +05:30
config.include LicenseHelpers
2018-10-15 14:42:47 +05:30
config.include ActiveJob::TestHelper
config.include ActiveSupport::Testing::TimeHelpers
config.include CycleAnalyticsHelpers
config.include ExpectOffense
config.include FactoryBot::Syntax::Methods
config.include FixtureHelpers
config.include GitlabRoutingHelper
config.include StubFeatureFlags
config.include StubGitlabCalls
config.include StubGitlabData
2018-11-08 19:23:39 +05:30
config.include ExpectNextInstanceOf
2018-10-15 14:42:47 +05:30
config.include TestEnv
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature
config.include LoginHelpers, type: :feature
config.include SearchHelpers, type: :feature
config.include EmailHelpers, :mailer, type: :mailer
config.include Warden::Test::Helpers, type: :request
config.include Gitlab::Routing, type: :routing
config.include Devise::Test::ControllerHelpers, type: :view
config.include ApiHelpers, :api
config.include CookieHelper, :js
config.include InputHelper, :js
config.include SelectionHelper, :js
config.include InspectRequests, :js
config.include WaitForRequests, :js
config.include LiveDebugger, :js
config.include MigrationsHelpers, :migration
config.include RedisHelpers
2018-11-08 19:23:39 +05:30
config.include Rails.application.routes.url_helpers, type: :routing
2019-05-18 00:54:41 +05:30
config.include PolicyHelpers, type: :policy
2014-09-02 18:07:02 +05:30
2018-03-17 18:26:18 +05:30
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)
2014-09-02 18:07:02 +05:30
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
config.before(:suite) do
Timecop.safe_mode = true
TestEnv.init
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
config.after(:all) do
TestEnv.clean_test_path
end
2019-05-03 19:53:19 +05:30
config.before do |example|
2017-09-10 17:25:29 +05:30
# Enable all features by default for testing
allow(Feature).to receive(:enabled?) { true }
2018-11-20 20:47:30 +05:30
2019-05-03 19:53:19 +05:30
enabled = example.metadata[:enable_rugged].present?
# Disable Rugged features by default
Gitlab::Git::RuggedImpl::Repository::FEATURE_FLAGS.each do |flag|
allow(Feature).to receive(:enabled?).with(flag).and_return(enabled)
end
2018-11-20 20:47:30 +05:30
# The following can be removed when we remove the staged rollout strategy
# and we can just enable it using instance wide settings
# (ie. ApplicationSetting#auto_devops_enabled)
allow(Feature).to receive(:enabled?)
.with(:force_autodevops_on_by_default, anything)
.and_return(false)
2017-09-10 17:25:29 +05:30
end
2019-03-02 22:35:43 +05:30
config.before(:example, :quarantine) do
# Skip tests in quarantine unless we explicitly focus on them.
skip('In quarantine') unless config.inclusion_filter[:quarantine]
end
2017-09-10 17:25:29 +05:30
config.before(:example, :request_store) do
RequestStore.begin!
end
config.after(:example, :request_store) do
RequestStore.end!
RequestStore.clear!
end
2019-02-15 15:39:39 +05:30
config.after do
2018-11-08 19:23:39 +05:30
Fog.unmock! if Fog.mock?
end
2019-02-15 15:39:39 +05:30
config.after do
2018-11-20 20:47:30 +05:30
Gitlab::CurrentSettings.clear_in_memory_application_settings!
end
2017-09-10 17:25:29 +05:30
config.before(:example, :mailer) do
reset_delivered_emails!
end
2018-11-08 19:23:39 +05:30
config.before(:example, :prometheus) do
matching_files = File.join(::Prometheus::Client.configuration.multiprocess_files_dir, "*.db")
Dir[matching_files].map { |filename| File.delete(filename) if File.file?(filename) }
Gitlab::Metrics.reset_registry!
end
2017-09-10 17:25:29 +05:30
config.around(:each, :use_clean_rails_memory_store_caching) do |example|
2016-08-24 12:49:21 +05:30
caching_store = Rails.cache
2017-09-10 17:25:29 +05:30
Rails.cache = ActiveSupport::Cache::MemoryStore.new
2016-08-24 12:49:21 +05:30
example.run
2017-09-10 17:25:29 +05:30
2016-08-24 12:49:21 +05:30
Rails.cache = caching_store
end
2016-11-03 12:29:30 +05:30
2017-09-10 17:25:29 +05:30
config.around(:each, :clean_gitlab_redis_cache) do |example|
2018-10-15 14:42:47 +05:30
redis_cache_cleanup!
2017-09-10 17:25:29 +05:30
example.run
2018-10-15 14:42:47 +05:30
redis_cache_cleanup!
2017-09-10 17:25:29 +05:30
end
config.around(:each, :clean_gitlab_redis_shared_state) do |example|
2018-10-15 14:42:47 +05:30
redis_shared_state_cleanup!
example.run
redis_shared_state_cleanup!
end
config.around(:each, :clean_gitlab_redis_queues) do |example|
redis_queues_cleanup!
2017-08-17 22:00:37 +05:30
2016-11-03 12:29:30 +05:30
example.run
2017-08-17 22:00:37 +05:30
2018-10-15 14:42:47 +05:30
redis_queues_cleanup!
2016-11-03 12:29:30 +05:30
end
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +05:30
config.around(:each, :use_clean_rails_memory_store_fragment_caching) do |example|
caching_store = ActionController::Base.cache_store
ActionController::Base.cache_store = ActiveSupport::Cache::MemoryStore.new
ActionController::Base.perform_caching = true
example.run
ActionController::Base.perform_caching = false
ActionController::Base.cache_store = caching_store
end
2018-03-27 19:54:05 +05:30
# 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
2017-09-10 17:25:29 +05:30
config.before(:each, :migration) do
2019-02-15 15:39:39 +05:30
use_fake_application_settings
2017-09-10 17:25:29 +05:30
schema_migrate_down!
end
config.after(:context, :migration) do
schema_migrate_up!
2019-02-15 15:39:39 +05:30
Gitlab::CurrentSettings.clear_in_memory_application_settings!
2017-09-10 17:25:29 +05:30
end
config.around(:each, :nested_groups) do |example|
2019-02-15 15:39:39 +05:30
example.run if Group.supports_nested_objects?
2017-09-10 17:25:29 +05:30
end
config.around(:each, :postgresql) do |example|
example.run if Gitlab::Database.postgresql?
end
2018-03-27 19:54:05 +05:30
config.around(:each, :mysql) do |example|
example.run if Gitlab::Database.mysql?
end
# This makes sure the `ApplicationController#can?` method is stubbed with the
# original implementation for all view specs.
config.before(:each, type: :view) do
allow(view).to receive(:can?) do |*args|
Ability.allowed?(*args)
end
end
2018-05-09 12:01:36 +05:30
config.before(:each, :http_pages_enabled) do |_|
allow(Gitlab.config.pages).to receive(:external_http).and_return(['1.1.1.1:80'])
end
config.before(:each, :https_pages_enabled) do |_|
allow(Gitlab.config.pages).to receive(:external_https).and_return(['1.1.1.1:443'])
end
config.before(:each, :http_pages_disabled) do |_|
allow(Gitlab.config.pages).to receive(:external_http).and_return(false)
end
config.before(:each, :https_pages_disabled) do |_|
allow(Gitlab.config.pages).to receive(:external_https).and_return(false)
end
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
# 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
FactoryBot::SyntaxRunner.class_eval do
2015-10-24 18:46:33 +05:30
include RSpec::Mocks::ExampleMethods
end
2015-04-26 12:48:37 +05:30
ActiveRecord::Migration.maintain_test_schema!
2017-09-10 17:25:29 +05:30
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
2018-03-17 18:26:18 +05:30
# Prevent Rugged from picking up local developer gitconfig.
Rugged::Settings['search_path_global'] = Rails.root.join('tmp/tests').to_s