File: feature_flags.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (72 lines) | stat: -rw-r--r-- 2,449 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require_relative '../../qa_helpers'

module RuboCop
  module Cop
    module QA
      # This cop checks for the usages of Runtime::Feature in QA specs and enforces
      # the presence and format of the `feature_flag` metadata.
      # @example
      #   # good
      #   describe 'some test', feature_flag: { name: :flag } do
      #
      #   # bad
      #   describe 'some test', :feature_flag do
      #
      #   # good
      #   describe 'some test', feature_flag: { name: :flag } do
      #     before do
      #       Runtime::Feature.enable(:flag)
      #
      #   # bad
      #   describe 'some test' do
      #     before do
      #       Runtime::Feature.enable(:flag)
      class FeatureFlags < RuboCop::Cop::Base
        APPLY_MESSAGE = "Apply the `feature_flag: { name: :flag }` metadata to the test to use `%{feature}` in " \
          "end-to-end tests."
        BLOCK_MESSAGE = "Feature flags must specify a name. Use a block with `feature_flag: { name: :flag }` instead."
        CONSTS = %w[Runtime::Feature QA::Runtime::Feature].freeze

        RSPEC_METHODS = %i[describe it context].freeze
        FEATURE_METHODS = %i[enable disable set enabled?].freeze
        RESTRICT_ON_SEND = RSPEC_METHODS + FEATURE_METHODS

        def_node_matcher :const_receiver, <<~PATTERN
          (send $const ...)
        PATTERN

        def_node_matcher :feature_flag_metatag?, <<~PATTERN
          (hash <(pair (sym :feature_flag) (hash <(pair (sym :name) _) ...>)) ...>)
        PATTERN

        def on_send(node)
          if FEATURE_METHODS.include?(node.method_name)
            return unless CONSTS.include?(const_receiver(node)&.const_name)
            return if has_required_metadata?(node)

            add_offense(node, message: format(APPLY_MESSAGE, feature: const_receiver(node).const_name))
          end

          return unless RSPEC_METHODS.include?(node.method_name)

          return unless has_feature_flag_metadata?(node)
          return if feature_flag_metatag?(node)

          add_offense(node, message: format(BLOCK_MESSAGE))
        end

        private

        def has_feature_flag_metadata?(node)
          node.arguments.any? { |arg| arg.sym_type? && arg.value == :feature_flag }
        end

        def has_required_metadata?(node)
          node.each_ancestor(:block).any? { |block| feature_flag_metatag?(block.send_node.last_argument) }
        end
      end
    end
  end
end