Skip to content

Commit dff8957

Browse files
authored
Merge pull request #891 from AlexWayfer/disable_RSpec/DescribeClass_for_string_constants
Ignore String constants by `RSpec/Describe`
2 parents 3a2088f + 21610c0 commit dff8957

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Before submitting the PR make sure the following are checked:
77
* [ ] Feature branch is up-to-date with `master` (if not - rebase it).
88
* [ ] Squashed related commits together.
99
* [ ] Added tests.
10+
* [ ] Updated documentation.
1011
* [ ] Added an entry to the [changelog](https://github.com/rubocop-hq/rubocop-rspec/blob/master/CHANGELOG.md) if the new code introduces user-observable changes.
1112
* [ ] The build (`bundle exec rake`) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).
1213

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Fix `RSpec/FilePath` detection when absolute path includes test subject. ([@eitoball][])
66
* Add new `Capybara/VisibilityMatcher` cop. ([@aried3r][])
7+
* Ignore String constants by `RSpec/Describe`. ([@AlexWayfer][])
78

89
## 1.38.1 (2020-02-15)
910

@@ -494,3 +495,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
494495
[@elebow]: https://github.com/elebow
495496
[@eitoball]: https://github.com/eitoball
496497
[@aried3r]: https://github.com/aried3r
498+
[@AlexWayfer]: https://github.com/AlexWayfer

lib/rubocop/cop/rspec/describe_class.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ module RSpec
1212
#
1313
# # good
1414
# describe TestedClass do
15+
# subject { described_class }
16+
# end
17+
#
18+
# describe 'TestedClass::VERSION' do
19+
# subject { Object.const_get(self.class.description) }
1520
# end
1621
#
1722
# describe "A feature example", type: :feature do
@@ -44,12 +49,20 @@ class DescribeClass < Cop
4449

4550
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern
4651

47-
def on_top_level_describe(node, args)
52+
def on_top_level_describe(node, (described_value, _))
4853
return if shared_group?(root_node)
4954
return if valid_describe?(node)
5055
return if describe_with_rails_metadata?(node)
56+
return if string_constant_describe?(described_value)
57+
58+
add_offense(described_value)
59+
end
60+
61+
private
5162

52-
add_offense(args.first)
63+
def string_constant_describe?(described_value)
64+
described_value.str_type? &&
65+
described_value.value =~ /^((::)?[A-Z]\w*)+$/
5366
end
5467
end
5568
end

manual/cops_rspec.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ end
333333

334334
# good
335335
describe TestedClass do
336+
subject { described_class }
337+
end
338+
339+
describe 'TestedClass::VERSION' do
340+
subject { Object.const_get(self.class.description) }
336341
end
337342

338343
describe "A feature example", type: :feature do

spec/rubocop/cop/rspec/describe_class_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,75 @@
5151
RUBY
5252
end
5353

54+
context 'when argument is a String literal' do
55+
it 'ignores class without namespace' do
56+
expect_no_offenses(<<-RUBY)
57+
describe 'Thing' do
58+
subject { Object.const_get(self.class.description) }
59+
end
60+
RUBY
61+
end
62+
63+
it 'ignores class with namespace' do
64+
expect_no_offenses(<<-RUBY)
65+
describe 'Some::Thing' do
66+
subject { Object.const_get(self.class.description) }
67+
end
68+
RUBY
69+
end
70+
71+
it 'ignores value constants' do
72+
expect_no_offenses(<<-RUBY)
73+
describe 'VERSION' do
74+
subject { Object.const_get(self.class.description) }
75+
end
76+
RUBY
77+
end
78+
79+
it 'ignores value constants with namespace' do
80+
expect_no_offenses(<<-RUBY)
81+
describe 'Some::VERSION' do
82+
subject { Object.const_get(self.class.description) }
83+
end
84+
RUBY
85+
end
86+
87+
it 'ignores top-level constants with `::` at start' do
88+
expect_no_offenses(<<-RUBY)
89+
describe '::Some::VERSION' do
90+
subject { Object.const_get(self.class.description) }
91+
end
92+
RUBY
93+
end
94+
95+
it 'checks `camelCase`' do
96+
expect_offense(<<-RUBY)
97+
describe 'activeRecord' do
98+
^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
99+
subject { Object.const_get(self.class.description) }
100+
end
101+
RUBY
102+
end
103+
104+
it 'checks numbers at start' do
105+
expect_offense(<<-RUBY)
106+
describe '2Thing' do
107+
^^^^^^^^ The first argument to describe should be the class or module being tested.
108+
subject { Object.const_get(self.class.description) }
109+
end
110+
RUBY
111+
end
112+
113+
it 'checks empty strings' do
114+
expect_offense(<<-RUBY)
115+
describe '' do
116+
^^ The first argument to describe should be the class or module being tested.
117+
subject { Object.const_get(self.class.description) }
118+
end
119+
RUBY
120+
end
121+
end
122+
54123
it 'ignores request specs' do
55124
expect_no_offenses(<<-RUBY)
56125
describe 'my new feature', type: :request do

0 commit comments

Comments
 (0)