Sha256: 6ffbf80ec1a0fb3ee1cc6028f9906a9c28b27687f83a1b2ec1493f4fe18b29a3

Contents?: true

Size: 1.76 KB

Versions: 15

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for setup scattered across multiple hooks in an example group.
      #
      # Unify `before`, `after`, and `around` hooks when possible.
      #
      # @example
      #   # bad
      #   describe Foo do
      #     before { setup1 }
      #     before { setup2 }
      #   end
      #
      #   # good
      #   describe Foo do
      #     before do
      #       setup1
      #       setup2
      #     end
      #   end
      #
      class ScatteredSetup < Base
        MSG = 'Do not define multiple `%<hook_name>s` hooks in the same ' \
              'example group (also defined on %<lines>s).'

        def on_block(node)
          return unless example_group?(node)

          repeated_hooks(node).each do |occurrences|
            lines = occurrences.map(&:first_line)

            occurrences.each do |occurrence|
              lines_except_current = lines - [occurrence.first_line]
              message = format(MSG, hook_name: occurrences.first.method_name,
                               lines: lines_msg(lines_except_current))
              add_offense(occurrence, message: message)
            end
          end
        end

        def repeated_hooks(node)
          hooks = RuboCop::RSpec::ExampleGroup.new(node)
            .hooks
            .select(&:knowable_scope?)
            .group_by { |hook| [hook.name, hook.scope, hook.metadata] }
            .values
            .reject(&:one?)

          hooks.map do |hook|
            hook.map(&:to_node)
          end
        end

        def lines_msg(numbers)
          if numbers.size == 1
            "line #{numbers.first}"
          else
            "lines #{numbers.join(', ')}"
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/scattered_setup.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.9.0/lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.11.1 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.11.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.10.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.9.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.8.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.7.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.6.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.5.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.4.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.3.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.2.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.1.0 lib/rubocop/cop/rspec/scattered_setup.rb
rubocop-rspec-2.0.1 lib/rubocop/cop/rspec/scattered_setup.rb