Skip to content

Commit

Permalink
Merge pull request #1670 from rubocop/fix/1669
Browse files Browse the repository at this point in the history
Fix `RSpec/LetBeforeExamples` autocorrect incompatible with `RSpec/ScatteredLet` autocorrect
  • Loading branch information
bquorning committed Jul 17, 2023
2 parents 003e208 + 31c7ca3 commit 1da31d1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add new `RSpec/ReceiveMessages` cop. ([@ydah])
- Add `AllowedIdentifiers` and `AllowedPatterns` configuration option to `RSpec/IndexedLet`. ([@ydah])
- Fix `RSpec/NamedSubject` when block has no body. ([@splattael])
- Fix `RSpec/LetBeforeExamples` autocorrect incompatible with `RSpec/ScatteredLet` autocorrect. ([@ydah])

## 2.22.0 (2023-05-06)

Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/rspec/let_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class LetBeforeExamples < Base
}
PATTERN

def self.autocorrect_incompatible_with
[RSpec::ScatteredLet]
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless example_group_with_body?(node)

Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,49 @@
RUBY
end
end

context 'when corrects `RSpec/LetBeforeExamples` with ' \
'`RSpec/ScatteredLet`' do
before do
RuboCop::ConfigLoader
.default_configuration
.for_all_cops['SuggestExtensions'] = false

create_file('spec/example.rb', <<~RUBY)
RSpec.describe 'Foo' do
let(:params) { {} }
specify do
expect(true).to be true
end
let(:attributes) { %i[first_name last_name] }
end
RUBY
end

it 'rubocop terminates with a success' do
expect(cli.run(['-A', '--only',
'RSpec/LetBeforeExamples,' \
'RSpec/ScatteredLet'])).to eq(0)
end

it 'autocorrects be compatible with each other' do
cli.run(['-A', '--only',
'RSpec/LetBeforeExamples,' \
'RSpec/ScatteredLet'])

expect(File.read('spec/example.rb')).to eq(<<~RUBY)
RSpec.describe 'Foo' do
let(:params) { {} }
let(:attributes) { %i[first_name last_name] }
specify do
expect(true).to be true
end
end
RUBY
end
end
end

0 comments on commit 1da31d1

Please sign in to comment.