Sha256: 7e4fb4b38f34db50a864fdb6c9f3371508ec0cd22956fae74dba938e7e36576b

Contents?: true

Size: 1.76 KB

Versions: 5

Compression:

Stored size: 1.76 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 < Base
        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)
          node.parent&.begin_type? ? node.parent.root? : node.root?
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/no_mixing_multisite_and_standard_specs.rb