diff options
author | Rémy Coutable <remy@rymai.me> | 2018-10-11 19:10:42 +0200 |
---|---|---|
committer | Rémy Coutable <remy@rymai.me> | 2018-10-15 14:28:03 +0200 |
commit | 02c47f2f73ac2d5a33b9a6c971668a38397b4910 (patch) | |
tree | b4e9120c8c2592529bbbee6683b926c483fddc3b /spec/rubocop | |
parent | 63cd88c668130d22f4a3d708894d0283a5941319 (diff) | |
download | gitlab-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 'spec/rubocop')
-rw-r--r-- | spec/rubocop/cop/qa/element_with_pattern_spec.rb | 50 | ||||
-rw-r--r-- | spec/rubocop/qa_helpers_spec.rb | 37 |
2 files changed, 87 insertions, 0 deletions
diff --git a/spec/rubocop/cop/qa/element_with_pattern_spec.rb b/spec/rubocop/cop/qa/element_with_pattern_spec.rb new file mode 100644 index 00000000000..c5beb40f9fd --- /dev/null +++ b/spec/rubocop/cop/qa/element_with_pattern_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../../rubocop/cop/qa/element_with_pattern' + +describe RuboCop::Cop::QA::ElementWithPattern do + include CopHelper + + let(:source_file) { 'qa/page.rb' } + + subject(:cop) { described_class.new } + + context 'in a QA file' do + before do + allow(cop).to receive(:in_qa_file?).and_return(true) + end + + it "registers an offense for elements with a pattern" do + expect_offense(<<-RUBY) + view 'app/views/shared/groups/_search_form.html.haml' do + element :groups_filter, 'search_field_tag :filter' + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't use a pattern for element, create a corresponding `qa-groups-filter` instead. + element :groups_filter_placeholder, /Search by name/ + ^^^^^^^^^^^^^^^^ Don't use a pattern for element, create a corresponding `qa-groups-filter-placeholder` instead. + end + RUBY + end + + it "does not register an offense for element without a pattern" do + expect_no_offenses(<<-RUBY) + view 'app/views/shared/groups/_search_form.html.haml' do + element :groups_filter + element :groups_filter_placeholder + end + RUBY + end + end + + context 'outside of a migration spec file' do + it "does not register an offense" do + expect_no_offenses(<<-RUBY) + describe 'foo' do + let(:user) { create(:user) } + end + RUBY + end + end +end diff --git a/spec/rubocop/qa_helpers_spec.rb b/spec/rubocop/qa_helpers_spec.rb new file mode 100644 index 00000000000..26e4c1ca6f0 --- /dev/null +++ b/spec/rubocop/qa_helpers_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rubocop' +require_relative '../../rubocop/qa_helpers' + +describe RuboCop::QAHelpers do + def parse_source(source, path = 'foo.rb') + buffer = Parser::Source::Buffer.new(path) + buffer.source = source + + builder = RuboCop::AST::Builder.new + parser = Parser::CurrentRuby.new(builder) + + parser.parse(buffer) + end + + let(:cop) do + Class.new do + include RuboCop::QAHelpers + end.new + end + + describe '#in_qa_file?' do + it 'returns true for a node in the qa/ directory' do + node = parse_source('10', Rails.root.join('qa', 'qa', 'page', 'dashboard', 'groups.rb')) + + expect(cop.in_qa_file?(node)).to eq(true) + end + + it 'returns false for a node outside the qa/ directory' do + node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb')) + + expect(cop.in_qa_file?(node)).to eq(false) + end + end +end |