Commit 07c450fa authored by Bob Van Landuyt's avatar Bob Van Landuyt

Add cop for modifying Sidekiq middleware in specs

This should prevent us from leaking sidekiq middleware across
specs. By default they should all be enabled.

For the specs that test 1 middleware specifically, we can use a helper.
parent 50c77d67
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# This cop checks for `Sidekiq::Testing.server_middleware`
# usage in specs.
#
# @example
#
# # bad
# Sidekiq::Testing.server_middleware do |chain|
# chain.add(MyMiddlewareUnderTest)
# end
#
#
# # good
# with_custom_sidekiq_middleware do |chain|
# chain.add(MyMiddlewareUnderTest)
# end
#
#
class ModifySidekiqMiddleware < RuboCop::Cop::Cop
MSG = <<~MSG
Don't modify global sidekiq middleware, use the `#with_sidekiq_server_middleware`
helper instead
MSG
def_node_search :modifies_sidekiq_middleware?, <<~PATTERN
(send
(const
(const nil? :Sidekiq) :Testing) :server_middleware)
PATTERN
def on_send(node)
return unless modifies_sidekiq_middleware?(node)
add_offense(node, location: :expression)
end
def autocorrect(node)
-> (corrector) do
corrector.replace(node.loc.expression,
'with_sidekiq_server_middleware')
end
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../support/helpers/expect_offense'
require_relative '../../../../rubocop/cop/rspec/modify_sidekiq_middleware'
describe RuboCop::Cop::RSpec::ModifySidekiqMiddleware do
include CopHelper
include ExpectOffense
subject(:cop) { described_class.new }
let(:source) do
<<~SRC
Sidekiq::Testing.server_middleware do |chain|
chain.add(MyCustomMiddleware)
end
SRC
end
let(:corrected) do
<<~SRC
with_sidekiq_server_middleware do |chain|
chain.add(MyCustomMiddleware)
end
SRC
end
it 'registers an offence' do
inspect_source(source)
expect(cop.offenses.size).to eq(1)
end
it 'can autocorrect the source' do
expect(autocorrect_source(source)).to eq(corrected)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment