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
|
# frozen_string_literal: true
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
# This cop checks for the usage of TestProf methods in migration specs.
#
# @example
#
# # bad
# let_it_be(:user) { table(:users).create(username: 'test') }
# let_it_be_with_reload(:user) { table(:users).create(username: 'test') }
# let_it_be_with_refind(:user) { table(:users).create(username: 'test') }
#
# before_all do
# do_something
# end
#
# # good
# let(:user) { table(:users).create(username: 'test') }
# let!(:user) { table(:users).create(username: 'test') }
#
# before(:all) do
# do_something
# end
#
# before do
# do_something
# end
class AvoidTestProf < RuboCop::Cop::Base
MESSAGE = "Prefer %{alternatives} over `%{method}` in migration specs. " \
'See ' \
'https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#testprof-in-migration-specs'
LET_ALTERNATIVES = %w[`let` `let!`].freeze
ALTERNATIVES = {
let_it_be: LET_ALTERNATIVES,
let_it_be_with_reload: LET_ALTERNATIVES,
let_it_be_with_refind: LET_ALTERNATIVES,
before_all: %w[`before` `before(:all)`]
}.freeze
FORBIDDEN_METHODS = ALTERNATIVES.keys.map(&:inspect).join(' ')
RESTRICT_ON_SEND = ALTERNATIVES.keys
def_node_matcher :forbidden_method_usage, <<~PATTERN
(send nil? ${#{FORBIDDEN_METHODS}} ...)
PATTERN
def on_send(node)
method = forbidden_method_usage(node)
return unless method
alternatives = ALTERNATIVES.fetch(method).join(' or ')
add_offense(
node,
message: format(MESSAGE, method: method, alternatives: alternatives)
)
end
end
end
end
end
|