Add a custom RSpec/SingleLineHook cop
This cop adds an offense when `before`, `after`, or `around` are used as single-line blocks.
This commit is contained in:
parent
557cbba7df
commit
69ad827e82
|
@ -1064,6 +1064,13 @@ RSpec/NotToNot:
|
||||||
RSpec/RepeatedDescription:
|
RSpec/RepeatedDescription:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
# Ensure RSpec hook blocks are always multi-line.
|
||||||
|
RSpec/SingleLineHook:
|
||||||
|
Enabled: false
|
||||||
|
Exclude:
|
||||||
|
- 'spec/factories/*'
|
||||||
|
- 'spec/requests/api/v3/*'
|
||||||
|
|
||||||
# Checks for stubbed test subjects.
|
# Checks for stubbed test subjects.
|
||||||
RSpec/SubjectStub:
|
RSpec/SubjectStub:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
module RuboCop
|
||||||
|
module Cop
|
||||||
|
module RSpec
|
||||||
|
# This cop checks for single-line hook blocks
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
#
|
||||||
|
# # bad
|
||||||
|
# before { do_something }
|
||||||
|
# after(:each) { undo_something }
|
||||||
|
#
|
||||||
|
# # good
|
||||||
|
# before do
|
||||||
|
# do_something
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# after(:each) do
|
||||||
|
# undo_something
|
||||||
|
# end
|
||||||
|
class SingleLineHook < RuboCop::Cop::RSpec::Cop
|
||||||
|
MESSAGE = "Don't use single-line hook blocks.".freeze
|
||||||
|
|
||||||
|
def_node_search :rspec_hook?, <<~PATTERN
|
||||||
|
(send nil {:after :around :before} ...)
|
||||||
|
PATTERN
|
||||||
|
|
||||||
|
def on_block(node)
|
||||||
|
return unless rspec_hook?(node)
|
||||||
|
return unless node.single_line?
|
||||||
|
|
||||||
|
add_offense(node, :expression, MESSAGE)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -15,3 +15,4 @@ require_relative 'cop/migration/remove_index'
|
||||||
require_relative 'cop/migration/reversible_add_column_with_default'
|
require_relative 'cop/migration/reversible_add_column_with_default'
|
||||||
require_relative 'cop/migration/timestamps'
|
require_relative 'cop/migration/timestamps'
|
||||||
require_relative 'cop/migration/update_column_in_batches'
|
require_relative 'cop/migration/update_column_in_batches'
|
||||||
|
require_relative 'cop/rspec/single_line_hook'
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
require 'rubocop'
|
||||||
|
require 'rubocop/rspec/support'
|
||||||
|
require 'rubocop-rspec'
|
||||||
|
|
||||||
|
require_relative '../../../../rubocop/cop/rspec/single_line_hook'
|
||||||
|
|
||||||
|
describe RuboCop::Cop::RSpec::SingleLineHook do
|
||||||
|
include CopHelper
|
||||||
|
|
||||||
|
subject(:cop) { described_class.new }
|
||||||
|
|
||||||
|
# Override `CopHelper#inspect_source` to always appear to be in a spec file,
|
||||||
|
# so that our RSpec-only cop actually runs
|
||||||
|
def inspect_source(*args)
|
||||||
|
super(*args, 'foo_spec.rb')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'registers an offense for a single-line `before` block' do
|
||||||
|
inspect_source(cop, 'before { do_something }')
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(1)
|
||||||
|
expect(cop.offenses.map(&:line)).to eq([1])
|
||||||
|
expect(cop.highlights).to eq(['before { do_something }'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'registers an offense for a single-line `after` block' do
|
||||||
|
inspect_source(cop, 'after(:each) { undo_something }')
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(1)
|
||||||
|
expect(cop.offenses.map(&:line)).to eq([1])
|
||||||
|
expect(cop.highlights).to eq(['after(:each) { undo_something }'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'registers an offense for a single-line `around` block' do
|
||||||
|
inspect_source(cop, 'around { |ex| do_something_else }')
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(1)
|
||||||
|
expect(cop.offenses.map(&:line)).to eq([1])
|
||||||
|
expect(cop.highlights).to eq(['around { |ex| do_something_else }'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'ignores a multi-line `before` block' do
|
||||||
|
inspect_source(cop, ['before do',
|
||||||
|
' do_something',
|
||||||
|
'end'])
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'ignores a multi-line `after` block' do
|
||||||
|
inspect_source(cop, ['after(:each) do',
|
||||||
|
' undo_something',
|
||||||
|
'end'])
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'ignores a multi-line `around` block' do
|
||||||
|
inspect_source(cop, ['around do |ex|',
|
||||||
|
' do_something_else',
|
||||||
|
'end'])
|
||||||
|
|
||||||
|
expect(cop.offenses.size).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue