summaryrefslogtreecommitdiff
path: root/rubocop/cop/qa
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-08-19 09:08:42 +0000
commitb76ae638462ab0f673e5915986070518dd3f9ad3 (patch)
treebdab0533383b52873be0ec0eb4d3c66598ff8b91 /rubocop/cop/qa
parent434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff)
downloadgitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'rubocop/cop/qa')
-rw-r--r--rubocop/cop/qa/selector_usage.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/rubocop/cop/qa/selector_usage.rb b/rubocop/cop/qa/selector_usage.rb
new file mode 100644
index 00000000000..568b1c30851
--- /dev/null
+++ b/rubocop/cop/qa/selector_usage.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+require_relative '../../qa_helpers'
+require_relative '../../code_reuse_helpers'
+
+module RuboCop
+ module Cop
+ module QA
+ # This cop checks for the usage of data-qa-selectors or .qa-* classes in non-QA files
+ #
+ # @example
+ # # bad
+ # find('[data-qa-selector="the_selector"]')
+ # find('.qa-selector')
+ #
+ # # good
+ # find('[data-testid="the_selector"]')
+ # find('#selector')
+ class SelectorUsage < RuboCop::Cop::Cop
+ include QAHelpers
+ include CodeReuseHelpers
+
+ SELECTORS = /\.qa-\w+|data-qa-\w+/.freeze
+ MESSAGE = %(Do not use `%s` as this is reserved for the end-to-end specs. Use a different selector or a data-testid instead.)
+
+ def on_str(node)
+ return if in_qa_file?(node)
+ return unless in_spec?(node)
+
+ add_offense(node, message: MESSAGE % node.value) if SELECTORS =~ node.value
+ rescue StandardError
+ # catch all errors and ignore them.
+ # without this catch-all rescue, rubocop will fail
+ # because of non-UTF-8 characters in some Strings
+ end
+ end
+ end
+ end
+end