Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timecop_travel.rb « rspec « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03f978be3496549689540345d90a4521f29c4a1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require 'rubocop-rspec'

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) }
      #
      class TimecopTravel < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        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)

          add_offense(node, message: MESSAGE) do |corrector|
            each_match_range(node.source_range, /^(Timecop\.travel)/) do |match_range|
              corrector.replace(match_range, 'travel_to')
            end
          end
        end
      end
    end
  end
end