From d81689c91c904583ce123372d6dc0ae9a0f828f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A9sir=C3=A9e=20Chevalier?= Date: Thu, 4 Nov 2021 08:44:42 +0000 Subject: [PATCH] Add cop for duplicate testcase links --- rubocop/cop/qa/duplicate_testcase_link.rb | 52 +++++++++++++++++++ .../cop/qa/duplicate_testcase_link_spec.rb | 36 +++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 rubocop/cop/qa/duplicate_testcase_link.rb create mode 100644 spec/rubocop/cop/qa/duplicate_testcase_link_spec.rb diff --git a/rubocop/cop/qa/duplicate_testcase_link.rb b/rubocop/cop/qa/duplicate_testcase_link.rb new file mode 100644 index 000000000000..f30768c7d80f --- /dev/null +++ b/rubocop/cop/qa/duplicate_testcase_link.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require_relative '../../qa_helpers' + +module RuboCop + module Cop + module QA + # This cop checks for duplicate testcase links across e2e specs + # + # @example + # + # # bad + # it 'some test', testcase: '(...)/quality/test_cases/1892' + # it 'another test, testcase: '(...)/quality/test_cases/1892' + # + # # good + # it 'some test', testcase: '(...)/quality/test_cases/1892' + # it 'another test, testcase: '(...)/quality/test_cases/1894' + class DuplicateTestcaseLink < RuboCop::Cop::Cop + include QAHelpers + + MESSAGE = "Don't reuse the same testcase link in different tests. Replace one of `%s`." + + @testcase_set = Set.new + + def_node_matcher :duplicate_testcase_link, <<~PATTERN + (block + (send nil? ... + ... + (hash + (pair + (sym :testcase) + (str $_))...)...)...) + PATTERN + + def on_block(node) + return unless in_qa_file?(node) + + duplicate_testcase_link(node) do |link| + break unless self.class.duplicate?(link) + + add_offense(node, message: MESSAGE % link) + end + end + + def self.duplicate?(link) + !@testcase_set.add?(link) + end + end + end + end +end diff --git a/spec/rubocop/cop/qa/duplicate_testcase_link_spec.rb b/spec/rubocop/cop/qa/duplicate_testcase_link_spec.rb new file mode 100644 index 000000000000..fb424da90e8a --- /dev/null +++ b/spec/rubocop/cop/qa/duplicate_testcase_link_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require_relative '../../../../rubocop/cop/qa/duplicate_testcase_link' + +RSpec.describe RuboCop::Cop::QA::DuplicateTestcaseLink do + 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 a duplicate testcase link" do + expect_offense(<<-RUBY) + it 'some test', testcase: '/quality/test_cases/1892' do + end + it 'another test', testcase: '/quality/test_cases/1892' do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't reuse the same testcase link in different tests. Replace one of `/quality/test_cases/1892`. + end + RUBY + end + + it "doesnt offend if testcase link is unique" do + expect_no_offenses(<<-RUBY) + it 'some test', testcase: '/quality/test_cases/1893' do + end + it 'another test', testcase: '/quality/test_cases/1894' do + end + RUBY + end + end +end