File: fast_spec_helper.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (98 lines) | stat: -rw-r--r-- 3,769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

if $LOADED_FEATURES.include?(File.expand_path('spec_helper.rb', __dir__))
  # There's no need to load anything here if spec_helper is already loaded
  # because spec_helper is more extensive than fast_spec_helper
  return
end

require_relative '../config/bundler_setup'
require 'benchmark'

module FastSpecHelper
  def self.slower_app_requires
    require 'active_support/all'
    require 'pry'
  end

  def self.app_requires
    require_relative 'deprecation_warnings'
    require 'gitlab/utils/all'
    require_relative 'rails_autoload'
    ENV['IN_MEMORY_APPLICATION_SETTINGS'] = 'true'
    require_relative '../config/settings'
    require_relative '../lib/gitlab'
  end

  def self.slower_spec_requires
    require 'rspec-parameterized'
    require_relative 'support/rspec'
  end

  def self.spec_requires_and_configuration
    require 'gitlab/rspec/next_instance_of'
    require_relative 'support/matchers/result_matchers'
    require_relative 'support/railway_oriented_programming'
    require_relative 'simplecov_env'

    # NOTE: Consider making any common RSpec configuration tweaks in `spec/support/rspec.rb` instead of here,
    # because it is also used by `spec/spec_helper.rb`.
    RSpec.configure do |config|
      config.include NextInstanceOf
      config.disable_monkey_patching! # Enable zero monkey patching mode before loading any other RSpec code.
      config.mock_with :rspec do |mocks|
        mocks.verify_doubled_constant_names = false # Allow mocking of non-lib module/class names from Rails
      end
    end

    Time.zone = 'UTC' # rubocop:disable Gitlab/ChangeTimezone -- allow Time.zone to not be nil in fast_spec_helper, so Time.zone.now works
  end

  def self.domain_specific_spec_helper_support
    # If you want to extensively use `fast_spec_helper` for your domain or
    # bounded context (https://handbook.gitlab.com/handbook/engineering/architecture/design-documents/modular_monolith/bounded_contexts/),
    # but don't want to have to repeat the same require statement or configuration across multiple spec files, you can
    # add a custom fast_spec_helper for your domain and require it here.
    # Just make sure your additions don't do anything to noticably increase the runtime of `fast_spec_helper`!

    # Remote Development domain
    require_relative('../ee/spec/support/fast_spec/remote_development/fast_spec_helper_support') if Gitlab.ee?

    # Web IDE domain
    require_relative 'support/fast_spec/web_ide/fast_spec_helper_support'
  end

  def self.post_require_configuration
    SimpleCovEnv.start!
    ActiveSupport::XmlMini.backend = 'Nokogiri'
  end

  def self.with_slow_execution_warning(max_allowed:)
    data = Benchmark.measure do
      yield
    end

    total = data.total

    return if total < max_allowed

    warn "\n\nWarning: fast_spec_helper submodule took longer than max allowed execution time " \
      "of #{max_allowed}: #{total}\n"
    warn "Slow submodule invoked from: #{caller[0]}\n\n"
  end

  def self.run
    # NOTE: These max_allowed times are generally 2-4 times higher than the actual average
    #       execution times, to avoid false warnings on slower machines or CI runners.
    with_slow_execution_warning(max_allowed: 2.0) { slower_app_requires }
    with_slow_execution_warning(max_allowed: 0.2) { app_requires }
    with_slow_execution_warning(max_allowed: 1.0) { slower_spec_requires }
    with_slow_execution_warning(max_allowed: 0.2) { spec_requires_and_configuration }
    with_slow_execution_warning(max_allowed: 1.0) { domain_specific_spec_helper_support }
    with_slow_execution_warning(max_allowed: 0.2) { post_require_configuration }
  end
end

FastSpecHelper.run

require_relative('../jh/spec/fast_spec_helper') if Gitlab.jh?