diff options
Diffstat (limited to 'rubocop/cop/rspec')
-rw-r--r-- | rubocop/cop/rspec/any_instance_of.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/be_success_matcher.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/env_assignment.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/expect_gitlab_tracking.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/factories_in_migration_specs.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/factory_bot/avoid_create.rb | 46 | ||||
-rw-r--r-- | rubocop/cop/rspec/factory_bot/inline_association.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/have_gitlab_http_status.rb | 1 | ||||
-rw-r--r-- | rubocop/cop/rspec/httparty_basic_auth.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/modify_sidekiq_middleware.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/timecop_freeze.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/timecop_travel.rb | 2 | ||||
-rw-r--r-- | rubocop/cop/rspec/top_level_describe_path.rb | 19 | ||||
-rw-r--r-- | rubocop/cop/rspec/web_mock_enable.rb | 2 |
14 files changed, 74 insertions, 14 deletions
diff --git a/rubocop/cop/rspec/any_instance_of.rb b/rubocop/cop/rspec/any_instance_of.rb index e1cacfebfd3..7016a76ec93 100644 --- a/rubocop/cop/rspec/any_instance_of.rb +++ b/rubocop/cop/rspec/any_instance_of.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/be_success_matcher.rb b/rubocop/cop/rspec/be_success_matcher.rb index 5a011845075..1ed55762965 100644 --- a/rubocop/cop/rspec/be_success_matcher.rb +++ b/rubocop/cop/rspec/be_success_matcher.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/env_assignment.rb b/rubocop/cop/rspec/env_assignment.rb index add7897c624..6994f3f0969 100644 --- a/rubocop/cop/rspec/env_assignment.rb +++ b/rubocop/cop/rspec/env_assignment.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/expect_gitlab_tracking.rb b/rubocop/cop/rspec/expect_gitlab_tracking.rb index 4f92980baa4..13fc7eace71 100644 --- a/rubocop/cop/rspec/expect_gitlab_tracking.rb +++ b/rubocop/cop/rspec/expect_gitlab_tracking.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'rack/utils' +require 'rubocop-rspec' module RuboCop module Cop diff --git a/rubocop/cop/rspec/factories_in_migration_specs.rb b/rubocop/cop/rspec/factories_in_migration_specs.rb index 6dde3d4524c..7dce1264b0e 100644 --- a/rubocop/cop/rspec/factories_in_migration_specs.rb +++ b/rubocop/cop/rspec/factories_in_migration_specs.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/factory_bot/avoid_create.rb b/rubocop/cop/rspec/factory_bot/avoid_create.rb new file mode 100644 index 00000000000..e9bccfb37cd --- /dev/null +++ b/rubocop/cop/rspec/factory_bot/avoid_create.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rubocop-rspec' + +module RuboCop + module Cop + module RSpec + module FactoryBot + # This cop checks for the creation of ActiveRecord objects in serializers specs specs + # + # @example + # + # # bad + # let(:user) { create(:user) } + # let(:users) { create_list(:user, 2) } + # + # # good + # let(:user) { build_stubbed(:user) } + # let(:user) { build(:user) } + # let(:users) { build_stubbed_list(:user, 2) } + # let(:users) { build_list(:user, 2) } + class AvoidCreate < RuboCop::Cop::Base + MESSAGE = "Prefer using `build_stubbed` or similar over `%{method_name}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#optimize-factory-usage" + FORBIDDEN_METHODS = %i[create create_list].freeze + RESTRICT_ON_SEND = FORBIDDEN_METHODS + + def_node_matcher :forbidden_factory_usage, <<~PATTERN + ( + send + {(const nil? :FactoryBot) nil?} + ${ #{FORBIDDEN_METHODS.map(&:inspect).join(' ')} } + ... + ) + PATTERN + + def on_send(node) + method_name = forbidden_factory_usage(node) + return unless method_name + + add_offense(node, message: format(MESSAGE, method_name: method_name)) + end + end + end + end + end +end diff --git a/rubocop/cop/rspec/factory_bot/inline_association.rb b/rubocop/cop/rspec/factory_bot/inline_association.rb index ccc6364fb73..8d7c73b99a0 100644 --- a/rubocop/cop/rspec/factory_bot/inline_association.rb +++ b/rubocop/cop/rspec/factory_bot/inline_association.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/have_gitlab_http_status.rb b/rubocop/cop/rspec/have_gitlab_http_status.rb index 86ece72b4f5..29577598ba7 100644 --- a/rubocop/cop/rspec/have_gitlab_http_status.rb +++ b/rubocop/cop/rspec/have_gitlab_http_status.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'rack/utils' +require 'rubocop-rspec' module RuboCop module Cop diff --git a/rubocop/cop/rspec/httparty_basic_auth.rb b/rubocop/cop/rspec/httparty_basic_auth.rb index 1e0f7ae7af0..d188002673f 100644 --- a/rubocop/cop/rspec/httparty_basic_auth.rb +++ b/rubocop/cop/rspec/httparty_basic_auth.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/modify_sidekiq_middleware.rb b/rubocop/cop/rspec/modify_sidekiq_middleware.rb index 78e3ba223b0..2e27288933f 100644 --- a/rubocop/cop/rspec/modify_sidekiq_middleware.rb +++ b/rubocop/cop/rspec/modify_sidekiq_middleware.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/timecop_freeze.rb b/rubocop/cop/rspec/timecop_freeze.rb index 70e37ecfa55..b13f5050040 100644 --- a/rubocop/cop/rspec/timecop_freeze.rb +++ b/rubocop/cop/rspec/timecop_freeze.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/timecop_travel.rb b/rubocop/cop/rspec/timecop_travel.rb index 586567fa0cd..03f978be349 100644 --- a/rubocop/cop/rspec/timecop_travel.rb +++ b/rubocop/cop/rspec/timecop_travel.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec diff --git a/rubocop/cop/rspec/top_level_describe_path.rb b/rubocop/cop/rspec/top_level_describe_path.rb index 3cc1ee8df90..ee1a0bcc4b6 100644 --- a/rubocop/cop/rspec/top_level_describe_path.rb +++ b/rubocop/cop/rspec/top_level_describe_path.rb @@ -1,21 +1,20 @@ # frozen_string_literal: true -require 'rubocop/rspec/top_level_describe' +require 'rubocop/cop/rspec/base' +require 'rubocop/cop/rspec/mixin/top_level_group' module RuboCop module Cop module RSpec - class TopLevelDescribePath < RuboCop::Cop::Cop - include RuboCop::RSpec::TopLevelDescribe + class TopLevelDescribePath < RuboCop::Cop::RSpec::Base + include RuboCop::Cop::RSpec::TopLevelGroup MESSAGE = 'A file with a top-level `describe` must end in _spec.rb.' - SHARED_EXAMPLES = %i[shared_examples shared_examples_for].freeze - def on_top_level_describe(node, args) + def on_top_level_example_group(node) return if acceptable_file_path?(processed_source.buffer.name) - return if shared_example?(node) - add_offense(node, message: MESSAGE) + add_offense(node.send_node, message: MESSAGE) end private @@ -23,12 +22,6 @@ module RuboCop def acceptable_file_path?(path) File.fnmatch?('*_spec.rb', path) || File.fnmatch?('*/frontend/fixtures/*', path) || File.fnmatch?('*/docs_screenshots/*_docs.rb', path) end - - def shared_example?(node) - node.ancestors.any? do |node| - node.respond_to?(:method_name) && SHARED_EXAMPLES.include?(node.method_name) - end - end end end end diff --git a/rubocop/cop/rspec/web_mock_enable.rb b/rubocop/cop/rspec/web_mock_enable.rb index 0bef16a16b0..395375e5fc1 100644 --- a/rubocop/cop/rspec/web_mock_enable.rb +++ b/rubocop/cop/rspec/web_mock_enable.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'rubocop-rspec' + module RuboCop module Cop module RSpec |