-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cop used to signal when we create a lot of factory objects with FactoryBot (configurable with the MaxAmount option)
- Loading branch information
1 parent
fe95441
commit 3eedcf3
Showing
9 changed files
with
159 additions
and
0 deletions.
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
53 changes: 53 additions & 0 deletions
53
lib/rubocop/cop/rspec/factory_bot/excessive_create_list.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,53 @@ | ||
# frozen_string_literal: true | ||
|
||
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 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) | ||
# | ||
class ExcessiveCreateList < RuboCop::Cop::RSpec::Base | ||
MESSAGE = | ||
'Avoid using `create_list` with more than %<max_amount>s 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 |
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
44 changes: 44 additions & 0 deletions
44
spec/rubocop/cop/rspec/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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Rubocop::Cop::RSpec::FactoryBot::ExcessiveCreateList do | ||
let(:cop_config) do | ||
{ 'MaxAmount' => max_amount } | ||
end | ||
|
||
context 'when MaxAmount is set to 50' do | ||
let(:max_amount) { 50 } | ||
|
||
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 50 items' do | ||
expect_no_offenses(<<~RUBY) | ||
create_list(:merge_requests, 30) | ||
RUBY | ||
end | ||
|
||
it 'ignores create_list for 50 items' do | ||
expect_no_offenses(<<~RUBY) | ||
create_list(:merge_requests, 50) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for create_list for more than 50 items' do | ||
expect_offense(<<~RUBY) | ||
create_list(:merge_requests, 51) | ||
^^ Avoid using `create_list` with more than 50 items. | ||
RUBY | ||
end | ||
end | ||
end |