Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-10-19 15:57:54 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-10-19 15:57:54 +0300
commit419c53ec62de6e97a517abd5fdd4cbde3a942a34 (patch)
tree1f43a548b46bca8a5fb8fe0c31cef1883d49c5b6 /rubocop/cop/qa/fabricate_usage.rb
parent1da20d9135b3ad9e75e65b028bffc921aaf8deb7 (diff)
Add latest changes from gitlab-org/gitlab@16-5-stable-eev16.5.0-rc42
Diffstat (limited to 'rubocop/cop/qa/fabricate_usage.rb')
-rw-r--r--rubocop/cop/qa/fabricate_usage.rb65
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