debian-mirror-gitlab/spec/spec_helper.rb

257 lines
7.6 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
2015-04-26 12:48:37 +05:30
require File.expand_path("../../config/environment", __FILE__)
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
if ENV['CI'] && !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'
2014-09-02 18:07:02 +05:30
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
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
config.mock_with :rspec
2016-06-02 11:05:42 +05:30
config.verbose_retry = true
config.display_try_failure_messages = true
2017-08-17 22:00:37 +05:30
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
2017-09-10 17:25:29 +05:30
config.include Devise::Test::IntegrationHelpers, type: :feature
2016-09-29 09:46:39 +05:30
config.include Warden::Test::Helpers, type: :request
2017-08-17 22:00:37 +05:30
config.include LoginHelpers, type: :feature
config.include SearchHelpers, type: :feature
2018-03-17 18:26:18 +05:30
config.include CookieHelper, :js
config.include InputHelper, :js
config.include SelectionHelper, :js
config.include InspectRequests, :js
2017-08-17 22:00:37 +05:30
config.include WaitForRequests, :js
2018-03-17 18:26:18 +05:30
config.include LiveDebugger, :js
2015-09-11 14:41:01 +05:30
config.include StubConfiguration
2017-09-10 17:25:29 +05:30
config.include EmailHelpers, :mailer, type: :mailer
2014-09-02 18:07:02 +05:30
config.include TestEnv
2015-12-23 02:04:40 +05:30
config.include ActiveJob::TestHelper
2016-09-13 17:45:13 +05:30
config.include ActiveSupport::Testing::TimeHelpers
2015-09-25 12:07:36 +05:30
config.include StubGitlabCalls
config.include StubGitlabData
2017-08-17 22:00:37 +05:30
config.include ApiHelpers, :api
2017-09-10 17:25:29 +05:30
config.include Gitlab::Routing, type: :routing
config.include MigrationsHelpers, :migration
config.include StubFeatureFlags
config.include StubENV
2018-05-09 12:01:36 +05:30
config.include ExpectOffense
2015-09-11 14:41:01 +05:30
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
2015-04-26 12:48:37 +05:30
config.raise_errors_for_deprecations!
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
2017-09-10 17:25:29 +05:30
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])
2018-03-17 18:26:18 +05:30
allow_any_instance_of(Gitlab::Git::GitlabProjects).to receive(:fork_repository).and_wrap_original do |m, *args|
m.call(*args)
2018-05-09 12:01:36 +05:30
shard_name, repository_relative_path = args
shard_path = Gitlab.config.repositories.storages.fetch(shard_name).legacy_disk_path
2018-03-17 18:26:18 +05:30
# 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
2017-09-10 17:25:29 +05:30
# Enable all features by default for testing
allow(Feature).to receive(:enabled?) { true }
end
config.before(:example, :request_store) do
RequestStore.begin!
end
config.after(:example, :request_store) do
RequestStore.end!
RequestStore.clear!
end
config.before(:example, :mailer) do
reset_delivered_emails!
end
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|
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)
2017-08-17 22:00:37 +05:30
Sidekiq.redis(&:flushall)
2016-11-03 12:29:30 +05:30
example.run
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
Gitlab::Redis::SharedState.with(&:flushall)
2017-08-17 22:00:37 +05:30
Sidekiq.redis(&:flushall)
2016-11-03 12:29:30 +05:30
end
2017-09-10 17:25:29 +05:30
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
schema_migrate_down!
end
config.after(:context, :migration) do
schema_migrate_up!
end
config.around(:each, :nested_groups) do |example|
example.run if Group.supports_nested_groups?
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