Sha256: bd4387324333de03f0554cbaceee5d28eb160f2b087a56387121b8537570c9a5

Contents?: true

Size: 1.32 KB

Versions: 11

Compression:

Stored size: 1.32 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 "something" do
      #     describe "x", type: :multisite do
      #     end
      #
      #     it "does X", type: :multisite do
      #     end
      #   end
      #
      #   # good
      #   describe "something", type: :multisite do
      #     describe "x" do
      #     end
      #
      #     it "does X" do
      #     end
      #   end
      class OnlyTopLevelMultisiteSpecs < Cop
        MSG = "Use `type: :multisite` only on a top-level `describe`"

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

          add_offense(node, message: MSG)
        end

        private

        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

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-discourse-3.8.1 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.7.1 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.7.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.6.1 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.6.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.5.1 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.5.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.4.1 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.4.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.3.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb
rubocop-discourse-3.2.0 lib/rubocop/cop/discourse/only_top_level_multisite_specs.rb