Commit 023d4f6f authored by Rémy Coutable's avatar Rémy Coutable

Move spec helpers/matchers/shared examples/contexts to their relevant folder

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent eb1cb7be
...@@ -143,7 +143,7 @@ Lint/MissingCopEnableDirective: ...@@ -143,7 +143,7 @@ Lint/MissingCopEnableDirective:
Lint/NestedPercentLiteral: Lint/NestedPercentLiteral:
Exclude: Exclude:
- 'lib/gitlab/git/repository.rb' - 'lib/gitlab/git/repository.rb'
- 'spec/support/email_format_shared_examples.rb' - 'spec/support/shared_examples/email_format_shared_examples.rb'
# Offense count: 1 # Offense count: 1
Lint/ReturnInVoidContext: Lint/ReturnInVoidContext:
...@@ -195,8 +195,8 @@ Naming/HeredocDelimiterCase: ...@@ -195,8 +195,8 @@ Naming/HeredocDelimiterCase:
- 'spec/lib/gitlab/diff/parser_spec.rb' - 'spec/lib/gitlab/diff/parser_spec.rb'
- 'spec/lib/json_web_token/rsa_token_spec.rb' - 'spec/lib/json_web_token/rsa_token_spec.rb'
- 'spec/models/commit_spec.rb' - 'spec/models/commit_spec.rb'
- 'spec/support/repo_helpers.rb' - 'spec/support/helpers/repo_helpers.rb'
- 'spec/support/seed_repo.rb' - 'spec/support/helpers/seed_repo.rb'
# Offense count: 112 # Offense count: 112
# Configuration parameters: Blacklist. # Configuration parameters: Blacklist.
...@@ -496,7 +496,7 @@ Style/EmptyLiteral: ...@@ -496,7 +496,7 @@ Style/EmptyLiteral:
- 'spec/lib/gitlab/request_context_spec.rb' - 'spec/lib/gitlab/request_context_spec.rb'
- 'spec/lib/gitlab/workhorse_spec.rb' - 'spec/lib/gitlab/workhorse_spec.rb'
- 'spec/requests/api/jobs_spec.rb' - 'spec/requests/api/jobs_spec.rb'
- 'spec/support/chat_slash_commands_shared_examples.rb' - 'spec/support/shared_examples/chat_slash_commands_shared_examples.rb'
# Offense count: 102 # Offense count: 102
# Cop supports --auto-correct. # Cop supports --auto-correct.
......
require './spec/support/sidekiq' require './spec/support/sidekiq'
require './spec/support/test_env' require './spec/support/helpers/test_env'
class Gitlab::Seeder::CycleAnalytics class Gitlab::Seeder::CycleAnalytics
def initialize(project, perf: false) def initialize(project, perf: false)
......
...@@ -9,7 +9,7 @@ At a minimum, requiring the Rake helper will redirect `stdout`, include the ...@@ -9,7 +9,7 @@ At a minimum, requiring the Rake helper will redirect `stdout`, include the
runtime task helpers, and include the `RakeHelpers` Spec support module. runtime task helpers, and include the `RakeHelpers` Spec support module.
The `RakeHelpers` module exposes a `run_rake_task(<task>)` method to make The `RakeHelpers` module exposes a `run_rake_task(<task>)` method to make
executing tasks simple. See `spec/support/rake_helpers.rb` for all available executing tasks simple. See `spec/support/helpers/rake_helpers.rb` for all available
methods. methods.
Example: Example:
......
...@@ -12,7 +12,11 @@ end ...@@ -12,7 +12,11 @@ end
WebMock.enable! WebMock.enable!
%w(select2_helper test_env repo_helpers wait_for_requests sidekiq project_forks_helper webmock).each do |f| %w(select2_helper test_env repo_helpers wait_for_requests project_forks_helper).each do |f|
require Rails.root.join('spec', 'support', 'helpers', f)
end
%w(sidekiq webmock).each do |f|
require Rails.root.join('spec', 'support', f) require Rails.root.join('spec', 'support', f)
end end
......
require_relative '../support/repo_helpers' require_relative '../support/helpers/repo_helpers'
FactoryBot.define do FactoryBot.define do
factory :commit do factory :commit do
......
require_relative '../support/gpg_helpers'
FactoryBot.define do FactoryBot.define do
factory :gpg_key_subkey do factory :gpg_key_subkey do
gpg_key gpg_key
......
require_relative '../support/gpg_helpers' require_relative '../support/helpers/gpg_helpers'
FactoryBot.define do FactoryBot.define do
factory :gpg_key do factory :gpg_key do
......
require_relative '../support/gpg_helpers'
FactoryBot.define do FactoryBot.define do
factory :gpg_signature do factory :gpg_signature do
commit_sha { Digest::SHA1.hexdigest(SecureRandom.hex) } commit_sha { Digest::SHA1.hexdigest(SecureRandom.hex) }
......
require_relative '../support/repo_helpers' require_relative '../support/helpers/repo_helpers'
include ActionDispatch::TestProcess include ActionDispatch::TestProcess
......
require_relative '../support/test_env' require_relative '../support/helpers/test_env'
FactoryBot.define do FactoryBot.define do
# Project without repository # Project without repository
......
...@@ -32,42 +32,19 @@ require 'rainbow/ext/string' ...@@ -32,42 +32,19 @@ require 'rainbow/ext/string'
# Requires supporting ruby files with custom matchers and macros, etc, # Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories. # in spec/support/ and its subdirectories.
# Requires helpers, and shared contexts/examples first since they're used in other support files
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 }
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config| RSpec.configure do |config|
config.use_transactional_fixtures = false config.use_transactional_fixtures = false
config.use_instantiated_fixtures = false config.use_instantiated_fixtures = false
config.mock_with :rspec
config.verbose_retry = true config.verbose_retry = true
config.display_try_failure_messages = true config.display_try_failure_messages = true
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include Devise::Test::IntegrationHelpers, type: :feature
config.include Warden::Test::Helpers, type: :request
config.include LoginHelpers, type: :feature
config.include SearchHelpers, type: :feature
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 StubConfiguration
config.include EmailHelpers, :mailer, type: :mailer
config.include TestEnv
config.include ActiveJob::TestHelper
config.include ActiveSupport::Testing::TimeHelpers
config.include StubGitlabCalls
config.include StubGitlabData
config.include ApiHelpers, :api
config.include Gitlab::Routing, type: :routing
config.include MigrationsHelpers, :migration
config.include StubFeatureFlags
config.include StubENV
config.include ExpectOffense
config.infer_spec_type_from_file_location! config.infer_spec_type_from_file_location!
config.define_derived_metadata(file_path: %r{/spec/}) do |metadata| config.define_derived_metadata(file_path: %r{/spec/}) do |metadata|
...@@ -82,7 +59,33 @@ RSpec.configure do |config| ...@@ -82,7 +59,33 @@ RSpec.configure do |config|
metadata[:type] = match[1].singularize.to_sym if match metadata[:type] = match[1].singularize.to_sym if match
end end
config.raise_errors_for_deprecations! 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
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
if ENV['CI'] if ENV['CI']
# This includes the first try, i.e. tests will be run 4 times before failing. # This includes the first try, i.e. tests will be run 4 times before failing.
......
...@@ -60,6 +60,8 @@ Capybara::Screenshot.register_driver(:chrome) do |driver, path| ...@@ -60,6 +60,8 @@ Capybara::Screenshot.register_driver(:chrome) do |driver, path|
end end
RSpec.configure do |config| RSpec.configure do |config|
config.include CapybaraHelpers, type: :feature
config.before(:context, :js) do config.before(:context, :js) do
next if $capybara_server_already_started next if $capybara_server_already_started
......
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
# #
# Usage: # Usage:
# #
# ./spec/support/generate-seed-repo-rb > spec/support/seed_repo.rb # ./spec/support/generate-seed-repo-rb > spec/support/helpers/seed_repo.rb
# #
# #
......
...@@ -12,5 +12,5 @@ inflate the size of the gitlab-ce repository. ...@@ -12,5 +12,5 @@ inflate the size of the gitlab-ce repository.
- make changes in your local clone of gitlab-git-test - make changes in your local clone of gitlab-git-test
- run `git push` which will push to your local source `gitlab-ce/spec/support/gitlab-git-test.git` - run `git push` which will push to your local source `gitlab-ce/spec/support/gitlab-git-test.git`
- in gitlab-ce: run `spec/support/prepare-gitlab-git-test-for-commit` - in gitlab-ce: run `spec/support/prepare-gitlab-git-test-for-commit`
- in gitlab-ce: `git add spec/support/seed_repo.rb spec/support/gitlab-git-test.git` - in gitlab-ce: `git add spec/support/helpers/seed_repo.rb spec/support/gitlab-git-test.git`
- commit your changes in gitlab-ce - commit your changes in gitlab-ce
...@@ -41,7 +41,3 @@ module CapybaraHelpers ...@@ -41,7 +41,3 @@ module CapybaraHelpers
page.driver.browser.manage.delete_cookie('_gitlab_session') page.driver.browser.manage.delete_cookie('_gitlab_session')
end end
end end
RSpec.configure do |config|
config.include CapybaraHelpers, type: :feature
end
...@@ -135,7 +135,3 @@ module CycleAnalyticsHelpers ...@@ -135,7 +135,3 @@ module CycleAnalyticsHelpers
end end
end end
end end
RSpec.configure do |config|
config.include CycleAnalyticsHelpers
end
...@@ -9,7 +9,3 @@ module FixtureHelpers ...@@ -9,7 +9,3 @@ module FixtureHelpers
File.expand_path(Rails.root.join(dir, 'spec', 'fixtures', filename)) File.expand_path(Rails.root.join(dir, 'spec', 'fixtures', filename))
end end
end end
RSpec.configure do |config|
config.include FixtureHelpers
end
module GitlabVerifyHelpers
def collect_ranges(args = {})
verifier = described_class.new(args.merge(batch_size: 1))
collect_results(verifier).map { |range, _| range }
end
def collect_failures
verifier = described_class.new(batch_size: 1)
out = {}
collect_results(verifier).map { |_, failures| out.merge!(failures) }
out
end
def collect_results(verifier)
out = []
verifier.run_batches { |*args| out << args }
out
end
end
module ActiveRecord
class QueryRecorder
attr_reader :log, :cached
def initialize(&block)
@log = []
@cached = []
ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block)
end
def show_backtrace(values)
Rails.logger.debug("QueryRecorder SQL: #{values[:sql]}")
caller.each { |line| Rails.logger.debug(" --> #{line}") }
end
def callback(name, start, finish, message_id, values)
show_backtrace(values) if ENV['QUERY_RECORDER_DEBUG']
if values[:name]&.include?("CACHE")
@cached << values[:sql]
elsif !values[:name]&.include?("SCHEMA")
@log << values[:sql]
end
end
def count
@log.count
end
def cached_count
@cached.count
end
def log_message
@log.join("\n\n")
end
end
end
module QuickActionsHelpers
def write_note(text)
Sidekiq::Testing.fake! do
page.within('.js-main-target-form') do
fill_in 'note[note]', with: text
find('.js-comment-submit-button').click
end
end
end
end
...@@ -9,7 +9,7 @@ TEST_MUTABLE_REPO_PATH = 'mutable-repo.git'.freeze ...@@ -9,7 +9,7 @@ TEST_MUTABLE_REPO_PATH = 'mutable-repo.git'.freeze
TEST_BROKEN_REPO_PATH = 'broken-repo.git'.freeze TEST_BROKEN_REPO_PATH = 'broken-repo.git'.freeze
module SeedHelper module SeedHelper
GITLAB_GIT_TEST_REPO_URL = File.expand_path('../gitlab-git-test.git', __FILE__).freeze GITLAB_GIT_TEST_REPO_URL = File.expand_path('../gitlab-git-test.git', __dir__).freeze
def ensure_seeds def ensure_seeds
if File.exist?(SEED_STORAGE_PATH) if File.exist?(SEED_STORAGE_PATH)
...@@ -108,11 +108,3 @@ bla/bla.txt ...@@ -108,11 +108,3 @@ bla/bla.txt
{ 'GIT_TEMPLATE_DIR' => '' } { 'GIT_TEMPLATE_DIR' => '' }
end end
end end
RSpec.configure do |config|
config.include SeedHelper, :seed_helper
config.before(:all, :seed_helper) do
ensure_seeds
end
end
# Helper allows you to sort items
#
# Params
# value - value for sorting
#
# Usage:
# include SortingHelper
#
# sorting_by('Oldest updated')
#
module SortingHelper
def sorting_by(value)
find('button.dropdown-toggle').click
page.within('.content ul.dropdown-menu.dropdown-menu-align-right li') do
click_link value
end
end
end
require 'active_support/core_ext/hash/transform_values'
require 'active_support/hash_with_indifferent_access'
module StubConfiguration module StubConfiguration
def stub_application_setting(messages) def stub_application_setting(messages)
add_predicates(messages) add_predicates(messages)
......
module StubConfiguration module StubObjectStorage
def stub_object_storage_uploader( def stub_object_storage_uploader(
config:, config:,
uploader:, uploader:,
......
shared_context 'JSON response' do
let(:json_response) { JSON.parse(response.body) }
end
RSpec.configure do |config| RSpec.configure do |config|
config.include_context 'JSON response' config.include_context 'JSON response'
config.include_context 'JSON response', type: :request config.include_context 'JSON response', type: :request
......
module ActiveRecord
class QueryRecorder
attr_reader :log, :cached
def initialize(&block)
@log = []
@cached = []
ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block)
end
def show_backtrace(values)
Rails.logger.debug("QueryRecorder SQL: #{values[:sql]}")
caller.each { |line| Rails.logger.debug(" --> #{line}") }
end
def callback(name, start, finish, message_id, values)
show_backtrace(values) if ENV['QUERY_RECORDER_DEBUG']
if values[:name]&.include?("CACHE")
@cached << values[:sql]
elsif !values[:name]&.include?("SCHEMA")
@log << values[:sql]
end
end
def count
@log.count
end
def cached_count
@cached.count
end
def log_message
@log.join("\n\n")
end
end
end
RSpec::Matchers.define :exceed_query_limit do |expected| RSpec::Matchers.define :exceed_query_limit do |expected|
supports_block_expectations supports_block_expectations
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
abort unless [ abort unless [
system('spec/support/generate-seed-repo-rb', out: 'spec/support/seed_repo.rb'), system('spec/support/generate-seed-repo-rb', out: 'spec/support/helpers/seed_repo.rb'),
system('spec/support/unpack-gitlab-git-test') system('spec/support/unpack-gitlab-git-test')
].all? ].all?
......
RSpec.configure do |config|
config.include GitlabRoutingHelper
end
require_relative "helpers/stub_configuration"
require_relative "helpers/stub_object_storage"
require_relative "helpers/stub_env"
RSpec.configure do |config|
config.mock_with :rspec
config.raise_errors_for_deprecations!
config.include StubConfiguration
config.include StubObjectStorage
config.include StubENV
end
RSpec.configure do |config|
config.include SeedHelper, :seed_helper
config.before(:all, :seed_helper) do
ensure_seeds
end
end
shared_context 'JSON response' do
let(:json_response) { JSON.parse(response.body) }
end
...@@ -17,29 +17,3 @@ RSpec.shared_examples 'Gitlab::Verify::BatchVerifier subclass' do ...@@ -17,29 +17,3 @@ RSpec.shared_examples 'Gitlab::Verify::BatchVerifier subclass' do
end end
end end
end end
module GitlabVerifyHelpers
def collect_ranges(args = {})
verifier = described_class.new(args.merge(batch_size: 1))
collect_results(verifier).map { |range, _| range }
end
def collect_failures
verifier = described_class.new(batch_size: 1)
out = {}
collect_results(verifier).map { |_, failures| out.merge!(failures) }
out
end
def collect_results(verifier)
out = []
verifier.run_batches { |*args| out << args }
out
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment