debian-mirror-gitlab/rubocop/cop/rspec/timecop_travel.rb

42 lines
1.1 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
2022-11-25 23:54:43 +05:30
require 'rubocop-rspec'
2021-01-03 14:25:43 +05:30
module RuboCop
module Cop
module RSpec
# This cop checks for `Timecop.travel` usage in specs.
#
# @example
#
# # bad
# Timecop.travel(1.day.ago) { create(:issue) }
#
# # good
# travel_to(1.day.ago) { create(:issue) }
#
2022-10-11 01:57:18 +05:30
class TimecopTravel < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-01-03 14:25:43 +05:30
include MatchRange
MESSAGE = 'Do not use `Timecop.travel`, use `travel_to` instead. ' \
'See https://gitlab.com/gitlab-org/gitlab/-/issues/214432 for more info.'
def_node_matcher :timecop_travel?, <<~PATTERN
(send (const nil? :Timecop) :travel _)
PATTERN
def on_send(node)
return unless timecop_travel?(node)
2022-10-11 01:57:18 +05:30
add_offense(node, message: MESSAGE) do |corrector|
2021-01-03 14:25:43 +05:30
each_match_range(node.source_range, /^(Timecop\.travel)/) do |match_range|
corrector.replace(match_range, 'travel_to')
end
end
end
end
end
end
end