2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
require 'rubocop-rspec'
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
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
|
|
|
|
#
|
|
|
|
#
|
2022-10-11 01:57:18 +05:30
|
|
|
class ModifySidekiqMiddleware < RuboCop::Cop::Base
|
|
|
|
extend RuboCop::Cop::AutoCorrector
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
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)
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
add_offense(node) do |corrector|
|
2020-04-22 19:07:51 +05:30
|
|
|
corrector.replace(node.loc.expression,
|
|
|
|
'with_sidekiq_server_middleware')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|