-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
FactoryBot/ExcessiveCreateList
cop
This PR is add excessive create list cop.
- Loading branch information
1 parent
0da664d
commit 3ed6552
Showing
8 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module FactoryBot | ||
# Check for excessive model creation in a list. | ||
# | ||
# @example MaxAmount: 10 (default) | ||
# # We do not allow more than 10 items to be created | ||
# | ||
# # bad | ||
# create_list(:merge_request, 1000, state: :opened) | ||
# | ||
# # good | ||
# create_list(:merge_request, 10, state: :opened) | ||
# | ||
# @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) | ||
# | ||
class ExcessiveCreateList < ::RuboCop::Cop::Base | ||
include ConfigurableExplicitOnly | ||
|
||
MESSAGE = | ||
'Avoid using `create_list` with more than %<max_amount>s items.' | ||
|
||
# @!method create_list?(node) | ||
def_node_matcher :create_list?, <<~PATTERN | ||
(send #factory_call? :create_list {sym str} $(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
spec/rubocop/cop/factory_bot/excessive_create_list_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::FactoryBot::ExcessiveCreateList do | ||
let(:cop_config) do | ||
{ 'MaxAmount' => max_amount } | ||
end | ||
|
||
let(:max_amount) { 10 } | ||
|
||
it 'ignores code that does not contain create_list' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(true).to be_truthy | ||
RUBY | ||
end | ||
|
||
it 'ignores create_list with non-integer value' do | ||
expect_no_offenses(<<~RUBY) | ||
create_list(:merge_requests, value) | ||
RUBY | ||
end | ||
|
||
it 'ignores create_list with less than 10 items' do | ||
expect_no_offenses(<<~RUBY) | ||
create_list(:merge_requests, 9) | ||
RUBY | ||
end | ||
|
||
it 'ignores create_list for 10 items' do | ||
expect_no_offenses(<<~RUBY) | ||
create_list(:merge_requests, 10) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for create_list for more than 10 items' do | ||
expect_offense(<<~RUBY) | ||
create_list(:merge_requests, 11) | ||
^^ Avoid using `create_list` with more than 10 items. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for FactoryBot.create_list' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.create_list(:merge_requests, 11) | ||
^^ Avoid using `create_list` with more than 10 items. | ||
RUBY | ||
end | ||
|
||
context 'when create_list has the factory name as a string' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
FactoryBot.create_list('warehouse/user', 11) | ||
^^ Avoid using `create_list` with more than 10 items. | ||
RUBY | ||
end | ||
end | ||
end |