-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/MultipleTestTargetsPerSpecFile
cop
- Loading branch information
Showing
6 changed files
with
106 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
51 changes: 51 additions & 0 deletions
51
lib/rubocop/cop/rspec/multiple_test_targets_per_spec_file.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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks that spec files only include one test target object. | ||
# | ||
# @example | ||
# # bad | ||
# RSpec.describe User do | ||
# # ... | ||
# decribe User::Admin do | ||
# # ... | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# RSpec.describe User do | ||
# # ... | ||
# end | ||
# RSpec.describe Admin do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# RSpec.describe User do | ||
# # ... | ||
# end | ||
# | ||
class MultipleTestTargetsPerSpecFile < Base | ||
MSG = 'Spec files should only include one test target object.' | ||
|
||
# @!method describe_classes(node) | ||
def_node_search :describe_classes, <<~PATTERN | ||
(block $(send #rspec? #ExampleGroups.all (const ...) ...) ...) | ||
PATTERN | ||
|
||
def on_investigation_end | ||
return unless processed_source.ast | ||
|
||
describes = describe_classes(processed_source.ast) | ||
return if describes.count <= 1 | ||
|
||
describes.each do |node| | ||
add_offense(node) | ||
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
45 changes: 45 additions & 0 deletions
45
spec/rubocop/cop/rspec/multiple_test_targets_per_spec_file_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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::MultipleTestTargetsPerSpecFile, :config do | ||
it 'registers an offense when include a class and a nested class describe' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe User do | ||
^^^^^^^^^^^^^^^^^^^ Spec files should only include one test target object. | ||
# ... | ||
describe User::Admin do | ||
^^^^^^^^^^^^^^^^^^^^ Spec files should only include one test target object. | ||
# ... | ||
end | ||
describe User::Moderator do | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Spec files should only include one test target object. | ||
# ... | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when include multiple classes' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe User do | ||
^^^^^^^^^^^^^^^^^^^ Spec files should only include one test target object. | ||
# ... | ||
end | ||
RSpec.describe Admin do | ||
^^^^^^^^^^^^^^^^^^^^ Spec files should only include one test target object. | ||
# ... | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when include only class' do | ||
expect_no_offenses(<<~RUBY) | ||
RSpec.describe User do | ||
describe '#method' do | ||
# ... | ||
end | ||
end | ||
RUBY | ||
end | ||
end |