diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-21 12:10:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-09-21 12:10:54 +0300 |
commit | 7e29b8f3fcc32a6adcf735a5a9069de5d9d814d6 (patch) | |
tree | db9cd61369d239bf092628a8796395374f0c4468 /rubocop | |
parent | fccfc5332f11e87433ada819c2467713f2dbb8f3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/qa/fabricate_usage.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/rubocop/cop/qa/fabricate_usage.rb b/rubocop/cop/qa/fabricate_usage.rb new file mode 100644 index 00000000000..1787df0e7df --- /dev/null +++ b/rubocop/cop/qa/fabricate_usage.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +require_relative '../../qa_helpers' + +module RuboCop + module Cop + module QA + # This cop checks for the usages of API fabrications and suggests using + # the corresponding QA factories instead. + # @example + # Good: + # create(:project) + # Bad: + # Resource::Project.fabricate_via_api! + # @example + # Good: + # create(:group) + # Bad: + # Resource::Group.fabricate_via_api! + # @example + # Good: + # create(:user, username: 'username', password: 'password') + # Bad: + # Resource::User.fabricate_via_api! do |user| + # user.username = 'username' + # user.password = 'password' + # end + class FabricateUsage < RuboCop::Cop::Base + MESSAGE = "Prefer create(:%{factory}[, ...]) here." + RESOURCES_TO_CHECK = { + 'Resource::Project' => :project, + 'Resource::Group' => :group, + 'Resource::Issue' => :issue, + 'Resource::User' => :user, + 'Resource::Pipeline' => :pipeline, + 'Resource::Job' => :job, + 'Resource::File' => :file, + 'Resource::GroupAccessToken' => :group_access_token, + 'Resource::ProjectAccessToken' => :project_access_token, + 'Resource::GroupLabel' => :group_label, + 'Resource::ProjectLabel' => :project_label, + 'Resource::GroupRunner' => :group_runner, + 'Resource::ProjectRunner' => :project_runner, + 'Resource::GroupMilestone' => :group_milestone, + 'Resource::ProjectMilestone' => :project_milestone, + 'Resource::Snippet' => :snippet, + 'Resource::ProjectSnippet' => :project_snippet + }.freeze + + RESTRICT_ON_SEND = %i[fabricate_via_api!].freeze + + def_node_matcher :const_receiver, <<~PATTERN + (send $const ...) + PATTERN + + def on_send(node) + factory = RESOURCES_TO_CHECK[const_receiver(node)&.const_name] + return unless factory + + add_offense(node, message: format(MESSAGE, factory: factory)) + end + end + end + end +end |