Sha256: db8f0a6a74e179fc921fb624ef283a3045c3872a6420b485eb51ae31974eea57

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require 'rubocop-rspec'
require_relative '../base'

module Rubocop
  module Cop
    module RSpec
      module FactoryBot
        # Check for create_list FactoryBot declarations higher than configured MaxAmount.
        #
        # @example MaxAmount: 20
        #   We do not allow more than 20 items to be created.
        #
        #   # bad
        #   create_list(:merge_request, 1000, state: :opened)
        #
        #   # good
        #   create_list(:merge_request, 15, state: :opened)
        #
        # @example
        #   We do not allow more than 10 items to be created (default)
        #   # bad
        #   create_list(:merge_request, 1000, state: :opened)
        #
        #   # good
        #   create_list(:merge_request, 10, state: :opened)
        #
        class ExcessiveCreateList < Base
          MESSAGE = 'Avoid using `create_list` with more than %{max_amount} items.'

          # @!method create_list?(node)
          def_node_matcher :create_list?, <<~PATTERN
            (send nil? :create_list (sym ...) $(int _) ...)
          PATTERN

          RESTRICT_ON_SEND = %i[create_list].freeze

          def on_send(node)
            number_node = create_list?(node)
            return unless number_node

            max_amount = cop_config['MaxAmount']
            return if number_node.value <= max_amount

            add_offense(number_node, message: format(MESSAGE, max_amount: max_amount))
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gitlab-styles-11.0.0 lib/rubocop/cop/rspec/factory_bot/excessive_create_list.rb
gitlab-styles-10.1.0 lib/rubocop/cop/rspec/factory_bot/excessive_create_list.rb