A platform for community discussion. Free, open, simple.
DEV: Decrease rspec startup time (#42946)
Martin Brennan8b8dbf5bae20
diff --git a/Gemfile b/Gemfileindex 86a9655684d..636de710f11 100644--- a/Gemfile+++ b/Gemfile@@ -80,13 +80,13 @@ gem "loofah"gem "css_parser", require: falsegem "omniauth"-gem "omniauth-facebook"-gem "omniauth-twitter"-gem "omniauth-github"+gem "omniauth-facebook", require: false+gem "omniauth-twitter", require: false+gem "omniauth-github", require: falsegem "omniauth-oauth2", require: false-gem "omniauth-google-oauth2"+gem "omniauth-google-oauth2", require: falsegem "oj"@@ -114,7 +114,7 @@ gem "rack-protection" # securitygem "cbor", require: falsegem "cose", require: falsegem "addressable"-gem "json_schemer"+gem "json_schemer", require: falsegem "net-smtp", require: falsegem "net-pop", require: false@@ -126,37 +126,37 @@ group :test dogem "capybara", require: falsegem "webmock", require: falsegem "simplecov", require: false- gem "test-prof"+ gem "test-prof", require: falsegem "rails-dom-testing", require: falsegem "minio_runner", require: false- gem "capybara-playwright-driver"+ gem "capybara-playwright-driver", require: falsegem "puma", require: falseendgroup :test, :development do- gem "rspec"+ gem "rspec", require: falsegem "listen", require: falsegem "certified", require: falsegem "fabrication", require: falsegem "mocha", require: false- gem "rb-fsevent", require: RUBY_PLATFORM =~ /darwin/i ? "rb-fsevent" : false+ gem "rb-fsevent", require: false- gem "rspec-rails"+ gem "rspec-rails", require: falsegem "shoulda-matchers", require: false- gem "rspec-html-matchers"+ gem "rspec-html-matchers", require: falsegem "debug", ">= 1.0.0", require: "debug/prelude"gem "rubocop-discourse", require: false- gem "parallel_tests"+ gem "parallel_tests", require: false- gem "rswag-specs"+ gem "rswag-specs", require: false- gem "annotaterb"+ gem "annotaterb", require: false- gem "syntax_tree"+ gem "syntax_tree", require: false- gem "rspec-multi-mock"+ gem "rspec-multi-mock", require: falseendgroup :development do@@ -175,8 +175,8 @@ if ENV["ALLOW_DEV_POPULATE"] == "1"gem "faker"elsegroup :development, :test do- gem "discourse_dev_assets"- gem "faker"+ gem "discourse_dev_assets", require: false+ gem "faker", require: falseendend@@ -224,7 +224,7 @@ gem "sassc-embedded"gem "rotp", require: false-gem "rqrcode"+gem "rqrcode", require: falsegem "rubyzip", require: falsediff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 4bcb8c36da0..c39d344f2b8 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -1674,6 +1674,7 @@ class UsersController < ApplicationControllerdef create_second_factor_totprequire "rotp" if !defined?(ROTP)+ require "rqrcode" if !defined?(RQRCode)totp_data = ROTP::Base32.randomserver_session["staged-totp-#{current_user.id}"] = totp_dataqrcode_png =diff --git a/app/services/categories/types/base.rb b/app/services/categories/types/base.rbindex 604e062d1bd..a99063444e8 100644--- a/app/services/categories/types/base.rb+++ b/app/services/categories/types/base.rb@@ -284,6 +284,8 @@ module Categories# Also validates that any site_settings keys are real SiteSettings (a# runtime check that cannot be expressed in JSON Schema).def validate_schema!+ require "json_schemer" if !defined?(JSONSchemer)+# Normalize Ruby symbol keys/values to strings via JSON round-tripschema_as_json = JSON.parse(configuration_schema.to_json)diff --git a/bin/rspec b/bin/rspecindex 054240e05d5..46f90c873b2 100755--- a/bin/rspec+++ b/bin/rspec@@ -14,14 +14,19 @@ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE_require "rubygems"require "bundler/setup"-if ENV["LOAD_PLUGINS"].nil? &&- ARGV.any? { |pattern|- pattern.include?("plugins/") ||- (- ENV.key?("DISCOURSE_REPO_BASE_DIRECTORY") &&- !pattern.include?(ENV["DISCOURSE_REPO_BASE_DIRECTORY"] + "/spec")- )- }+core_spec_path = File.expand_path("../spec", __dir__)+plugin_spec_requested =+ ARGV.any? do |pattern|+ next false if pattern.start_with?("-")+ next true if pattern.include?("plugins/")+ next false if !ENV.key?("DISCOURSE_REPO_BASE_DIRECTORY")++ requested_path = File.expand_path(pattern.sub(/:\d+(?::\d+)?\z/, ""))+ File.exist?(requested_path) && requested_path != core_spec_path &&+ !requested_path.start_with?("#{core_spec_path}/")+ end++if ENV["LOAD_PLUGINS"].nil? && plugin_spec_requestedSTDERR.puts "Detected plugin spec path, setting LOAD_PLUGINS to 1"ENV["LOAD_PLUGINS"] = "1"enddiff --git a/config/application.rb b/config/application.rbindex 47937689c78..ef70bfe90fd 100644--- a/config/application.rb+++ b/config/application.rb@@ -56,6 +56,17 @@ if defined?(Bundler)Bundler.require(*bundler_groups)end+if Rails.env.production?+ require "json_schemer"+ require "omniauth-facebook"+ require "omniauth-github"+ require "omniauth-google-oauth2"+ require "omniauth-twitter"+ require "rqrcode"+end++require "discourse_dev_assets" if Rails.env.development?+require_relative "../lib/require_dependency_backward_compatibility"module Discoursediff --git a/config/initializers/009-omniauth.rb b/config/initializers/009-omniauth.rbindex 6fca1ff6ecc..305e7e76f80 100644--- a/config/initializers/009-omniauth.rb+++ b/config/initializers/009-omniauth.rb@@ -35,6 +35,8 @@ OmniAuth.config.on_failure do |env|next OmniAuth::FailureEndpoint.call(env) if !is_rescued_error # let the default behavior handle it+ require "oauth" if !defined?(OAuth)+case exceptionwhen OAuth::Unauthorized# OAuth1 (i.e. Twitter) makes a web request during the setup phasediff --git a/lib/auth.rb b/lib/auth.rbindex 3938f379a20..54b017e1411 100644--- a/lib/auth.rb+++ b/lib/auth.rb@@ -9,6 +9,7 @@ require "auth/auth_provider"require "auth/result"require "auth/authenticator"require "auth/managed_authenticator"+require "omniauth-oauth2"require "auth/facebook_authenticator"require "auth/github_authenticator"require "auth/twitter_authenticator"diff --git a/lib/auth/facebook_authenticator.rb b/lib/auth/facebook_authenticator.rbindex fc3d4dac95d..23f8277df71 100644--- a/lib/auth/facebook_authenticator.rb+++ b/lib/auth/facebook_authenticator.rb@@ -20,6 +20,7 @@ class Auth::FacebookAuthenticator < Auth::ManagedAuthenticatorenddef register_middleware(omniauth)+ require "omniauth-facebook"omniauth.provider :facebook,setup:lambda { |env|diff --git a/lib/auth/github_authenticator.rb b/lib/auth/github_authenticator.rbindex de70ff75e83..363dc617712 100644--- a/lib/auth/github_authenticator.rb+++ b/lib/auth/github_authenticator.rb@@ -56,6 +56,7 @@ class Auth::GithubAuthenticator < Auth::ManagedAuthenticatorenddef register_middleware(omniauth)+ require "omniauth-github"omniauth.provider :github,setup:lambda { |env|diff --git a/lib/auth/google_oauth2_authenticator.rb b/lib/auth/google_oauth2_authenticator.rbindex 65574204bad..c383c165994 100644--- a/lib/auth/google_oauth2_authenticator.rb+++ b/lib/auth/google_oauth2_authenticator.rb@@ -29,6 +29,7 @@ class Auth::GoogleOAuth2Authenticator < Auth::ManagedAuthenticatorenddef register_middleware(omniauth)+ require "omniauth-google-oauth2"options = {setup:lambda do |env|diff --git a/lib/auth/twitter_authenticator.rb b/lib/auth/twitter_authenticator.rbindex c2b44b0e1ba..4778ef3b80c 100644--- a/lib/auth/twitter_authenticator.rb+++ b/lib/auth/twitter_authenticator.rb@@ -18,6 +18,7 @@ class Auth::TwitterAuthenticator < Auth::ManagedAuthenticatorenddef healthy?+ require "omniauth-twitter"consumer_key = SiteSetting.twitter_consumer_keyconsumer_secret = SiteSetting.twitter_consumer_secret@@ -40,6 +41,7 @@ class Auth::TwitterAuthenticator < Auth::ManagedAuthenticatorenddef register_middleware(omniauth)+ require "omniauth-twitter"omniauth.provider :twitter,setup:lambda { |env|diff --git a/lib/tasks/api_docs.rake b/lib/tasks/api_docs.rakeindex fb0fa509a6a..8f64de7b7f9 100644--- a/lib/tasks/api_docs.rake+++ b/lib/tasks/api_docs.rake@@ -15,6 +15,8 @@ if Rails.env.local?desc "Generate Swagger JSON files from integration specs (including plugins)"RSpec::Core::RakeTask.new("swaggerize") do |t|+ require "rswag/specs"+# Automatically load plugins for API documentationENV["LOAD_PLUGINS"] = "1" unless ENV["LOAD_PLUGINS"]diff --git a/lib/tasks/parallel_tests.rake b/lib/tasks/parallel_tests.rakenew file mode 100644index 00000000000..e499471c5d0--- /dev/null+++ b/lib/tasks/parallel_tests.rake@@ -0,0 +1,3 @@+# frozen_string_literal: true++require "parallel_tests/tasks"diff --git a/plugins/chat/spec/support/api_schema_matcher.rb b/plugins/chat/spec/support/api_schema_matcher.rbindex e78e158d79c..89ed45fa71a 100644--- a/plugins/chat/spec/support/api_schema_matcher.rb+++ b/plugins/chat/spec/support/api_schema_matcher.rb@@ -1,5 +1,7 @@# frozen_string_literal: true+require "json-schema"+RSpec::Matchers.define :match_response_schema do |schema|match do |object|schema_directory = "#{Dir.pwd}/plugins/chat/spec/support/api/schemas"diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex eed7d46c5a6..0348716c93d 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -3,8 +3,6 @@RSpec.describe User dosubject(:user) { Fabricate(:user, last_seen_at: 1.day.ago) }- fab!(:group)-it_behaves_like "it has custom fields"describe "#reload" do@@ -18,9 +16,9 @@ RSpec.describe User doendit do- is_expected.to have_many(:pending_posts).class_name("ReviewableQueuedPost").with_foreign_key(- :target_created_by_id,- )+ expect(described_class.allocate).to have_many(:pending_posts).class_name(+ "ReviewableQueuedPost",+ ).with_foreign_key(:target_created_by_id)enddescribe ".in_any_groups?" do@@ -260,6 +258,8 @@ RSpec.describe User doenddescribe "when group with a same name already exists" do+ fab!(:group)+it "should not be valid" donew_user = Fabricate.build(:user, username: group.name.upcase)@@ -2076,6 +2076,8 @@ RSpec.describe User doendcontext "when the user has a group" do+ fab!(:group)+before dogroup.usernames = user.usernamegroup.savediff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex f3b58e1f3b5..d51e79d5159 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -15,13 +15,13 @@ if ENV["COVERAGE"]endrequire "rubygems"-require "rbtrace" if RUBY_ENGINE == "ruby"-require "pry"-require "pry-rails"+require "rbtrace" if RUBY_ENGINE == "ruby" && ENV["RBTRACE"] == "1"require "fabrication"require "mocha/api"require "certified"require "webmock/rspec"+require "rspec-html-matchers"+require "rspec-multi-mock"require_relative "support/server_error_tracking"@@ -29,10 +29,28 @@ ENV["RAILS_ENV"] ||= "test"ENV["ENABLE_LOGSTASH_LOGGER"] ||= "1"require File.expand_path("../../config/environment", __FILE__)Discourse.singleton_class.prepend(RspecWarnExceptionCapture)-require "rspec/rails"++spec_files = RSpec.configuration.files_to_run+system_specs_requested = spec_files.any? { |file| file.match?(%r{(^|/)spec/system/}) }+request_specs_requested = spec_files.any? { |file| file.match?(%r{(^|/)spec/requests/}) }++if system_specs_requested || request_specs_requested+ require "rspec/rails"+else+ capybara_path = Gem::Specification.find_by_name("capybara").full_gem_path+ skipped_capybara_features =+ %w[capybara/rspec.rb capybara/rails.rb].map { |file| File.join(capybara_path, "lib", file) }+ skipped_capybara_features.reject! { |feature| $LOADED_FEATURES.include?(feature) }+ $LOADED_FEATURES.concat(skipped_capybara_features)+ begin+ require "rspec/rails"+ ensure+ skipped_capybara_features.each { |feature| $LOADED_FEATURES.delete(feature) }+ end+end+require "shoulda-matchers"require "sidekiq/testing"-require "capybara/rails"# The shoulda-matchers gem no longer detects the test framework# you're using or mixes itself into that framework automatically.@@ -46,26 +64,48 @@ end# Requires supporting ruby files with custom matchers and macros, etc,# in spec/support/ and its subdirectories.-Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }+system_support_files = [+ Rails.root.join("spec/support/system_helpers.rb").to_s,+ Rails.root.join("spec/support/theme_screenshot_marker.rb").to_s,+ *Dir[Rails.root.join("spec/support/system/**/*.rb")],+]++lazy_support_files = {+ DiscourseConnectHelpers:+ Rails.root.join("spec/support/discourse_connect_support_helpers.rb").to_s,+ FakeS3: Rails.root.join("spec/support/fake_s3.rb").to_s,+ ImageOrientationHelpers: Rails.root.join("spec/support/image_orientation_helpers.rb").to_s,+}+lazy_support_files.each { |constant, file| Object.autoload(constant, file) }++(+ Dir[Rails.root.join("spec/support/**/*.rb")] - system_support_files - lazy_support_files.values+).each { |f| require f }Dir[Rails.root.join("spec/requests/examples/*.rb")].each { |f| require f }-Dir[Rails.root.join("spec/system/helpers/**/*.rb")].each { |f| require f }-Dir[Rails.root.join("spec/system/page_objects/**/base.rb")].each { |f| require f }-Dir[Rails.root.join("spec/system/page_objects/**/*_base.rb")].each { |f| require f }-Dir[Rails.root.join("spec/system/page_objects/**/*.rb")].each { |f| require f }+if system_specs_requested+ require "capybara-playwright-driver"+ system_support_files.each { |f| require f }+ Dir[Rails.root.join("spec/system/helpers/**/*.rb")].each { |f| require f }+ Dir[Rails.root.join("spec/system/page_objects/**/base.rb")].each { |f| require f }+ Dir[Rails.root.join("spec/system/page_objects/**/*_base.rb")].each { |f| require f }+ Dir[Rails.root.join("spec/system/page_objects/**/*.rb")].each { |f| require f }+end-Dir[Rails.root.join("spec/fabricators/*.rb")].each { |f| require f }require_relative "helpers/redis_snapshot_helper"# Require plugin helpers at plugin/[plugin]/spec/plugin_helper.rb (includes symlinked plugins).if ENV["LOAD_PLUGINS"] == "1"+ Dir[Rails.root.join("spec/fabricators/*.rb")].each { |f| require f }Dir[Rails.root.join("plugins/*/spec/plugin_helper.rb")].each { |f| require f }Dir[Rails.root.join("plugins/*/spec/support/**/*.rb")].sort.each { |f| require f }Dir[Rails.root.join("plugins/*/spec/fabricators/**/*.rb")].each { |f| require f }- Dir[Rails.root.join("plugins/*/spec/system/page_objects/**/*.rb")].each { |f| require f }+ if system_specs_requested+ Dir[Rails.root.join("plugins/*/spec/system/page_objects/**/*.rb")].each { |f| require f }+ endendRSpec.configure do |config|@@ -83,8 +123,10 @@ RSpec.configure do |config|config.include MessageBusconfig.include RSpecHtmlMatchersconfig.include IntegrationHelpers, type: :request- config.include SystemHelpers, type: :system- config.include ThemeScreenshotMarker, type: :system+ if system_specs_requested+ config.include SystemHelpers, type: :system+ config.include ThemeScreenshotMarker, type: :system+ endconfig.include DiscourseWebauthnIntegrationHelpersconfig.include SiteSettingsHelpersconfig.include SidekiqHelpers@@ -171,7 +213,7 @@ RSpec.configure do |config|SiteIconManager.clear_cache!end- config.after(:suite) { Downloads.clear }+ config.after(:suite) { Downloads.clear } if system_specs_requestedconfig.before(:each) { TestSetup.test_setup }diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rbindex 9c06281d8a2..e70e0854438 100644--- a/spec/requests/omniauth_callbacks_controller_spec.rb+++ b/spec/requests/omniauth_callbacks_controller_spec.rb@@ -1,6 +1,8 @@# frozen_string_literal: truerequire "discourse_connect_base"+require "oauth"+require "omniauth-google-oauth2"RSpec.describe Users::OmniauthCallbacksController dofab!(:user)diff --git a/spec/support/ci.rb b/spec/support/ci.rbindex 28cd8fa074b..f8b61ef9d0f 100644--- a/spec/support/ci.rb+++ b/spec/support/ci.rb@@ -82,22 +82,24 @@ RSpec.configure do |config|# this workaround for now where we will retry the block once before raising the error.## Potentially related: https://bugs.ruby-lang.org/issues/20172- module Capybara- class << self- def using_session_with_localhost_resolution(name, &block)- attempts = 0- _using_session(name, &block)- rescue Socket::ResolutionError- puts "Socket::ResolutionError error encountered... Current thread count: #{Thread.list.size}"- attempts += 1- attempts <= 1 ? retry : raise+ if defined?(Capybara) && Capybara.respond_to?(:using_session)+ module Capybara+ class << self+ def using_session_with_localhost_resolution(name, &block)+ attempts = 0+ _using_session(name, &block)+ rescue Socket::ResolutionError+ puts "Socket::ResolutionError error encountered... Current thread count: #{Thread.list.size}"+ attempts += 1+ attempts <= 1 ? retry : raise+ endendend- end- Capybara.singleton_class.class_eval do- alias_method :_using_session, :using_session- alias_method :using_session, :using_session_with_localhost_resolution+ Capybara.singleton_class.class_eval do+ alias_method :_using_session, :using_session+ alias_method :using_session, :using_session_with_localhost_resolution+ endendenddiff --git a/spec/support/helpers.rb b/spec/support/helpers.rbindex 1f5c980cc9c..380ffe57adf 100644--- a/spec/support/helpers.rb+++ b/spec/support/helpers.rb@@ -1,8 +1,5 @@# frozen_string_literal: true-GIT_INITIAL_BRANCH_SUPPORTED =- Gem::Version.new(`git --version`.match(/[\d\.]+/)[0]) >= Gem::Version.new("2.28.0")-module Helpersextend ActiveSupport::Concern@@ -180,7 +177,7 @@ module Helpersdef setup_git_repo(files)repo_dir = Dir.mktmpdirsystem(- "git -C #{repo_dir} init -q . #{"--initial-branch=main" if GIT_INITIAL_BRANCH_SUPPORTED}",+ "git -C #{repo_dir} init -q . #{"--initial-branch=main" if git_initial_branch_supported?}",exception: true,)system("git -C #{repo_dir} config user.email '[email protected]'", exception: true)@@ -200,6 +197,11 @@ module Helpersrepo_dirend+ def git_initial_branch_supported?+ @git_initial_branch_supported ||=+ Gem::Version.new(`git --version`.match(/[\d\.]+/)[0]) >= Gem::Version.new("2.28.0")+ end+def setup_remote_upstream(path)system("git -C #{path} remote add origin #{path}/.git", exception: true)system("git -C #{path} fetch -q", exception: true)diff --git a/spec/support/lazy_fabricators.rb b/spec/support/lazy_fabricators.rbnew file mode 100644index 00000000000..3563670532b--- /dev/null+++ b/spec/support/lazy_fabricators.rb@@ -0,0 +1,47 @@+# frozen_string_literal: true++module LazyFabricators+ FABRICATOR_PATTERN = /Fabricator\(\s*:([a-zA-Z0-9_]+)/++ def register(...)+ @registering_fabricator = true+ super+ ensure+ @registering_fabricator = false+ end++ def [](name)+ schematic = super+ return schematic if schematic || @registering_fabricator++ if (file = fabricator_files[name.to_sym]) && !loading_fabricator_files.include?(file)+ loading_fabricator_files << file+ begin+ Kernel.load(file)+ ensure+ loading_fabricator_files.delete(file)+ end+ end++ super+ end++ def empty?+ fabricator_files.empty? && super+ end++ private++ def fabricator_files+ @fabricator_files ||=+ Dir[Rails.root.join("spec/fabricators/*.rb")].each_with_object({}) do |file, files|+ File.read(file).scan(FABRICATOR_PATTERN).flatten.each { |name| files[name.to_sym] = file }+ end+ end++ def loading_fabricator_files+ @loading_fabricator_files ||= Set.new+ end+end++Fabrication::Schematic::Manager.prepend(LazyFabricators)diff --git a/spec/support/lazy_faker.rb b/spec/support/lazy_faker.rbnew file mode 100644index 00000000000..79367bb53df--- /dev/null+++ b/spec/support/lazy_faker.rb@@ -0,0 +1,12 @@+# frozen_string_literal: true++module LazyFaker+ def const_missing(name)+ require "faker"+ return const_get(name, false) if const_defined?(name, false)++ super+ end+end++Faker.singleton_class.prepend(LazyFaker)diff --git a/spec/support/lazy_pry.rb b/spec/support/lazy_pry.rbnew file mode 100644index 00000000000..f1f9ceae14b--- /dev/null+++ b/spec/support/lazy_pry.rb@@ -0,0 +1,12 @@+# frozen_string_literal: true++module LazyPry+ def pry(...)+ require "pry"+ require "pry-rails"+ LazyPry.remove_method(:pry)+ public_send(:pry, ...)+ end+end++Object.prepend(LazyPry)diff --git a/spec/support/suite_setup.rb b/spec/support/suite_setup.rbindex 81543e33db7..d18f4389e3d 100644--- a/spec/support/suite_setup.rb+++ b/spec/support/suite_setup.rb@@ -3,6 +3,9 @@# One-time suite bootstrap: disable expensive subsystems, check for pending# migrations, (on CI) widen INT foreign-key columns to BIGINT, quiet noisy# output, wire the test current-user provider, and reset themes.+task_specs_requested =+ RSpec.configuration.files_to_run.any? { |file| file.match?(%r{(^|/)spec/(?:lib/)?tasks/}) }+RSpec.configure do |config|config.before(:suite) doCachedCounting.disable@@ -50,14 +53,14 @@ RSpec.configure do |config|SeedFu.quiet = true# json-schema's MultiJSON support is deprecated.- JSON::Validator.use_multi_json = false+ JSON::Validator.use_multi_json = false if defined?(JSON::Validator)# Ugly, but needed until we have a user creatorUser.skip_callback(:create, :after, :ensure_in_trust_level_group)DiscoursePluginRegistry.reset! if ENV["LOAD_PLUGINS"] != "1"Discourse.current_user_provider = TestCurrentUserProvider- Discourse::Application.load_tasks+ Discourse::Application.load_tasks if task_specs_requestedSystemThemesManager.clear_system_theme_user_history!ThemeField.delete_alldiff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rbindex c1df3b5b3ae..b9d2a11b772 100644--- a/spec/swagger_helper.rb+++ b/spec/swagger_helper.rb@@ -1,6 +1,7 @@# frozen_string_literal: truerequire "json_schemer"+require "rswag/specs"# Require schema filesDir["./spec/requests/api/schemas/*.rb"].each { |file| require file }