Commit c55ebb84 authored by Desirée Chevalier's avatar Desirée Chevalier Committed by Rémy Coutable

Add e2e testcase link format cop

parent a898ae32
# frozen_string_literal: true
require_relative '../../qa_helpers'
module RuboCop
module Cop
module QA
# This cop checks for correct format of testcase links across e2e specs
#
# @example
#
# # bad
# it 'some test', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/557'
# it 'another test, testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/quality/test_cases/2455'
#
# # good
# it 'some test', testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/348312'
# it 'another test, testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/348236'
class TestcaseLinkFormat < RuboCop::Cop::Cop
include QAHelpers
TESTCASE_FORMAT = %r{https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/\d+}.freeze
MESSAGE = "Testcase link format incorrect. Please link a test case from the GitLab project. See: https://docs.gitlab.com/ee/development/testing_guide/end_to_end/best_practices.html#link-a-test-to-its-test-case."
def_node_matcher :testcase_link_format, <<~PATTERN
(block
(send nil? ...
...
(hash
(pair
(sym :testcase)
(str $_))...)...)...)
PATTERN
def on_block(node)
return unless in_qa_file?(node)
testcase_link_format(node) do |link|
add_offense(node, message: MESSAGE % link) unless TESTCASE_FORMAT =~ link
end
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../rubocop/cop/qa/testcase_link_format'
RSpec.describe RuboCop::Cop::QA::TestcaseLinkFormat do
let(:source_file) { 'qa/page.rb' }
let(:msg) { 'Testcase link format incorrect. Please link a test case from the GitLab project. See: https://docs.gitlab.com/ee/development/testing_guide/end_to_end/best_practices.html#link-a-test-to-its-test-case.' }
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 testcase link for an issue" do
node = "it 'another test', testcase: 'https://gitlab.com/gitlab-org/gitlab/-/issues/557' do"
expect_offense(<<-RUBY, node: node, msg: msg)
%{node}
^{node} %{msg}
end
RUBY
end
it "registers an offense for a testcase link for the wrong project" do
node = "it 'another test', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/quality/test_cases/2455' do"
expect_offense(<<-RUBY, node: node, msg: msg)
%{node}
^{node} %{msg}
end
RUBY
end
it "doesnt offend if testcase link is correct" do
expect_no_offenses(<<-RUBY)
it 'some test', testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/348312' do
end
RUBY
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment