From dc18e883ce0206b0719ef444542a82f908ca163e Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 22 Jul 2021 18:52:09 +0200 Subject: [PATCH] Refactoring: Move all RSpec configurations to dedicated support files. --- spec/rails_helper.rb | 57 +------------------ ...kage_migrations.rb => db_initial_state.rb} | 5 ++ spec/support/db_transactional_fixtures.rb | 8 +++ spec/support/fixtures.rb | 5 ++ .../otrs_import_json_fixture_helper.rb | 10 +++- spec/support/rails_backtrace_filter.rb | 8 +++ spec/support/shoulda_matchers.rb | 8 +++ 7 files changed, 45 insertions(+), 56 deletions(-) rename spec/support/{perform_package_migrations.rb => db_initial_state.rb} (61%) create mode 100644 spec/support/db_transactional_fixtures.rb create mode 100644 spec/support/fixtures.rb create mode 100644 spec/support/rails_backtrace_filter.rb create mode 100644 spec/support/shoulda_matchers.rb diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 1d48f429b..bfbecc491 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,8 +6,10 @@ require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort('The Rails environment is running in production mode!') if Rails.env.production? + require 'spec_helper' require 'rspec/rails' + # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in @@ -24,58 +26,3 @@ require 'rspec/rails' # require only the support files necessary. # Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f } - -# Reset database to be ready for tests -Rake::Task['zammad:db:reset'].execute - -# make sure that all migrations of linked packages are executed -Package::Migration.linked - -RSpec.configure do |config| - # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = "#{::Rails.root}/spec/fixtures" - - # make usage of time travel helpers possible - config.include ActiveSupport::Testing::TimeHelpers - config.after do - travel_back - end - - # Zammad specific helpers - config.include ZammadHelper - - # skip Zammad helper functions in the stacktrace to lower noise - config.backtrace_exclusion_patterns << %r{/spec/spec_helper/} - - # If you're not using ActiveRecord, or you'd prefer not to run each of your - # examples within a transaction, remove the following line or assign false - # instead of true. - config.use_transactional_fixtures = true - - # RSpec Rails can automatically mix in different behaviours to your tests - # based on their file location, for example enabling you to call `get` and - # `post` in specs under `spec/controllers`. - # - # You can disable this behaviour by removing the line below, and instead - # explicitly tag your specs with their type, e.g.: - # - # RSpec.describe UsersController, :type => :controller do - # # ... - # end - # - # The different available types are documented in the features, such as in - # https://relishapp.com/rspec/rspec-rails/docs - # config.infer_spec_type_from_file_location! - - # Filter lines from Rails gems in backtraces. - config.filter_rails_from_backtrace! - # arbitrary gems may also be filtered via: - # config.filter_gems_from_backtrace("gem name") -end - -Shoulda::Matchers.configure do |config| - config.integrate do |with| - with.test_framework :rspec - with.library :rails - end -end diff --git a/spec/support/perform_package_migrations.rb b/spec/support/db_initial_state.rb similarity index 61% rename from spec/support/perform_package_migrations.rb rename to spec/support/db_initial_state.rb index 495b74955..1c67262a4 100644 --- a/spec/support/perform_package_migrations.rb +++ b/spec/support/db_initial_state.rb @@ -1,4 +1,9 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ +require_relative './rake' + +# Reset database to be ready for tests +Rake::Task['zammad:db:reset'].execute + # make sure that all migrations of linked packages are executed Package::Migration.linked diff --git a/spec/support/db_transactional_fixtures.rb b/spec/support/db_transactional_fixtures.rb new file mode 100644 index 000000000..6f2d7ac6e --- /dev/null +++ b/spec/support/db_transactional_fixtures.rb @@ -0,0 +1,8 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +RSpec.configure do |config| + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true +end diff --git a/spec/support/fixtures.rb b/spec/support/fixtures.rb new file mode 100644 index 000000000..735d4e2ac --- /dev/null +++ b/spec/support/fixtures.rb @@ -0,0 +1,5 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +RSpec.configure do |config| + config.fixture_path = "#{::Rails.root}/spec/fixtures" +end diff --git a/spec/support/otrs_import_json_fixture_helper.rb b/spec/support/otrs_import_json_fixture_helper.rb index 85c2f63a8..ea2b6ddfd 100644 --- a/spec/support/otrs_import_json_fixture_helper.rb +++ b/spec/support/otrs_import_json_fixture_helper.rb @@ -1,7 +1,15 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ -module ZammadHelper +module OtrsImportJsonFixtureHelper def json_fixture(file) JSON.parse(File.read("spec/fixtures/#{file}.json")) end end + +RSpec.configure do |config| + # Zammad specific helpers + config.include OtrsImportJsonFixtureHelper + + # skip OtrsImportJsonFixtureHelper functions in the backtraces to lower noise + config.backtrace_exclusion_patterns << %r{/spec/spec_helper/} +end diff --git a/spec/support/rails_backtrace_filter.rb b/spec/support/rails_backtrace_filter.rb new file mode 100644 index 000000000..d8f283bfb --- /dev/null +++ b/spec/support/rails_backtrace_filter.rb @@ -0,0 +1,8 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +RSpec.configure do |config| # rubocop:disable Style/SymbolProc + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/support/shoulda_matchers.rb b/spec/support/shoulda_matchers.rb new file mode 100644 index 000000000..8780911b9 --- /dev/null +++ b/spec/support/shoulda_matchers.rb @@ -0,0 +1,8 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end