summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2018-10-11 19:10:42 +0200
committerRémy Coutable <remy@rymai.me>2018-10-15 14:28:03 +0200
commit02c47f2f73ac2d5a33b9a6c971668a38397b4910 (patch)
treeb4e9120c8c2592529bbbee6683b926c483fddc3b /rubocop
parent63cd88c668130d22f4a3d708894d0283a5941319 (diff)
downloadgitlab-ce-02c47f2f73ac2d5a33b9a6c971668a38397b4910.tar.gz
Add a new QA::ElementWithPattern cop
This cop forbids the use of `element :foo, 'pattern'` and `element :bar, /pattern/` in QA files. Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/qa/element_with_pattern.rb39
-rw-r--r--rubocop/qa_helpers.rb11
-rw-r--r--rubocop/rubocop.rb1
3 files changed, 51 insertions, 0 deletions
diff --git a/rubocop/cop/qa/element_with_pattern.rb b/rubocop/cop/qa/element_with_pattern.rb
new file mode 100644
index 00000000000..9d80946f1ba
--- /dev/null
+++ b/rubocop/cop/qa/element_with_pattern.rb
@@ -0,0 +1,39 @@
+require_relative '../../qa_helpers'
+
+module RuboCop
+ module Cop
+ module QA
+ # This cop checks for the usage of factories in migration specs
+ #
+ # @example
+ #
+ # # bad
+ # let(:user) { create(:user) }
+ #
+ # # good
+ # let(:users) { table(:users) }
+ # let(:user) { users.create!(name: 'User 1', username: 'user1') }
+ class ElementWithPattern < RuboCop::Cop::Cop
+ include QAHelpers
+
+ MESSAGE = "Don't use a pattern for element, create a corresponding `%s` instead.".freeze
+
+ def on_send(node)
+ return unless in_qa_file?(node)
+ return unless method_name(node).to_s == 'element'
+
+ element_name, pattern = node.arguments
+ return unless pattern
+
+ add_offense(node, location: pattern.source_range, message: MESSAGE % "qa-#{element_name.value.to_s.tr('_', '-')}")
+ end
+
+ private
+
+ def method_name(node)
+ node.children[1]
+ end
+ end
+ end
+ end
+end
diff --git a/rubocop/qa_helpers.rb b/rubocop/qa_helpers.rb
new file mode 100644
index 00000000000..f4adf7f4e9f
--- /dev/null
+++ b/rubocop/qa_helpers.rb
@@ -0,0 +1,11 @@
+module RuboCop
+ # Module containing helper methods for writing QA cops.
+ module QAHelpers
+ # Returns true if the given node originated from the qa/ directory.
+ def in_qa_file?(node)
+ path = node.location.expression.source_buffer.name
+
+ path.start_with?(File.join(Dir.pwd, 'qa'))
+ end
+ end
+end
diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb
index 76d6037706e..6c9b8753c1a 100644
--- a/rubocop/rubocop.rb
+++ b/rubocop/rubocop.rb
@@ -29,6 +29,7 @@ require_relative 'cop/migration/update_large_table'
require_relative 'cop/project_path_helper'
require_relative 'cop/rspec/env_assignment'
require_relative 'cop/rspec/factories_in_migration_specs'
+require_relative 'cop/qa/element_with_pattern'
require_relative 'cop/sidekiq_options_queue'
require_relative 'cop/destroy_all'
require_relative 'cop/ruby_interpolation_in_translation'