Sha256: 27539000fcde67e8a47146159cf8a67f14085a22c71a660cf656bc5892e9aca2

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      # Use `type: :multisite` only on a top-level `describe`.
      # Mixing multisite and standard specs can lead to errors,
      # e.g. when using `fab!` helper.
      #
      # @example
      #   # bad
      #   describe "x" do
      #   end
      #
      #   describe "x", type: :multisite do
      #   end
      #
      #   # good
      #   # x_spec.rb
      #   describe "x" do
      #   end
      #
      #   # x_multisite_spec.rb
      #   describe "x", type: :multisite do
      #   end
      class NoMixingMultisiteAndStandardSpecs < Cop
        MSG = "Do not mix multisite and standard specs. Consider moving multisite describes to a separate file."

        def initialize(config = nil, options = nil)
          super
          @describes = nil
        end

        def on_block(node)
          return if !top_level?(node)
          return if !describe?(node.children.first)

          type = !!multisite_describe?(node.children.first)

          if !@describes.nil? && @describes != type
            add_offense(node, message: MSG)
          else
            @describes = type
          end
        end

        private

        def_node_matcher :describe?, <<~MATCHER
          (send
            {nil? (const nil? :RSpec)}
            {:describe :context :it}
            ...
          )
        MATCHER

        def_node_matcher :multisite_describe?, <<~MATCHER
          (send
            {nil? (const nil? :RSpec)}
            {:describe :context :it}
            _
            (hash (pair (sym :type) (sym :multisite)) ...)
          )
        MATCHER

        def top_level?(node)
          if node.parent&.begin_type?
            node.parent.root?
          else
            node.root?
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-discourse-3.1.0 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.0.3 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.0.2 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.0.1 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.0 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-2.6.0 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-2.5.0 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb