summaryrefslogtreecommitdiff
path: root/rubocop/cop/qa/duplicate_testcase_link.rb
blob: 82549707a83d6269a1157d25cf2b63dfb78bb280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

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
        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)
          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