Skip to content

Commit

Permalink
fix: Don't use FreezeTime cop in Rails <5.2
Browse files Browse the repository at this point in the history
`freeze_time` wasn't added until Rails 5.2.
  • Loading branch information
DRBragg committed Jan 20, 2023
1 parent f77da83 commit 0a96ee6
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix `Rails/FreezeTime` running against Rails < 5.2 apps. ([@DRBragg][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rails/freeze_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module Rails
#
class FreezeTime < Base
extend AutoCorrector
extend TargetRailsVersion

minimum_target_rails_version 5.2

MSG = 'Use `freeze_time` instead of `travel_to`.'
NOW_METHODS = %i[now new current].freeze
Expand Down
249 changes: 144 additions & 105 deletions spec/rubocop/cop/rails/freeze_time_spec.rb
Original file line number Diff line number Diff line change
@@ -1,121 +1,160 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Rails::FreezeTime, :config do
it 'registers an offense when using `travel_to` with an argument of the current time' do
expect_offense(<<~RUBY)
travel_to(Time.now)
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.new)
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(DateTime.now)
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.current)
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.zone.now)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.now.in_time_zone)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.current.to_time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::Time.now)
^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::DateTime.now)
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::Time.zone.now)
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
RUBY

expect_correction(<<~RUBY)
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
RUBY
end
context 'Rails 5.1', :rails51 do
it 'does not register an offense when using `travel_to` with an argument of the current time' do
expect_no_offenses('travel_to(Time.now)')
expect_no_offenses('travel_to(Time.new)')
expect_no_offenses('travel_to(DateTime.now)')
expect_no_offenses('travel_to(Time.current)')
expect_no_offenses('travel_to(Time.zone.now)')
expect_no_offenses('travel_to(Time.now.in_time_zone)')
expect_no_offenses('travel_to(Time.current.to_time)')
expect_no_offenses('travel_to(::Time.now)')
expect_no_offenses('travel_to(::DateTime.now)')
expect_no_offenses('travel_to(::Time.zone.now)')
end

it 'registers an offense when using `travel_to` with an argument of the current time and `do-end` block' do
expect_offense(<<~RUBY)
travel_to(Time.now) do
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
do_something
end
RUBY

expect_correction(<<~RUBY)
freeze_time do
do_something
end
RUBY
end
it 'does not register an offense when using `travel_to` with an argument of the current time and `do-end` block' do
expect_no_offenses(<<~RUBY)
travel_to(Time.now) do
do_something
end
RUBY
end

it 'registers an offense when using `travel_to` with an argument of the current time and `{}` block' do
expect_offense(<<~RUBY)
travel_to(Time.now) { do_something }
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
RUBY
it 'does not register an offense when using `travel_to` with an argument of the current time and `{}` block' do
expect_no_offenses(<<~RUBY)
travel_to(Time.now) { do_something }
RUBY
end

expect_correction(<<~RUBY)
freeze_time { do_something }
RUBY
it 'does not register an offense when using `travel_to` with an argument of the current time and proc argument' do
expect_no_offenses(<<~RUBY)
around do |example|
travel_to(Time.current, &example)
end
RUBY
end
end

it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
expect_offense(<<~RUBY)
around do |example|
travel_to(Time.current, &example)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
end
RUBY

expect_correction(<<~RUBY)
around do |example|
freeze_time(&example)
end
RUBY
end
context 'Rails 5.2', :rails52 do
it 'registers an offense when using `travel_to` with an argument of the current time' do
expect_offense(<<~RUBY)
travel_to(Time.now)
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.new)
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(DateTime.now)
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.current)
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.zone.now)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.now.in_time_zone)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(Time.current.to_time)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::Time.now)
^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::DateTime.now)
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
travel_to(::Time.zone.now)
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
RUBY

it 'does not register an offense when using `freeze_time`' do
expect_no_offenses(<<~RUBY)
freeze_time
RUBY
end
expect_correction(<<~RUBY)
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
freeze_time
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of the not current time' do
expect_no_offenses(<<~RUBY)
travel_to(Time.current.yesterday)
travel_to(Time.zone.tomorrow)
travel_to(DateTime.next_day)
travel_to(Time.zone.yesterday.in_time_zone)
RUBY
end
it 'registers an offense when using `travel_to` with an argument of the current time and `do-end` block' do
expect_offense(<<~RUBY)
travel_to(Time.now) do
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
do_something
end
RUBY

it 'does not register an offense when using `travel_to` with an argument of `current` method without receiver' do
expect_no_offenses(<<~RUBY)
travel_to(current)
RUBY
end
expect_correction(<<~RUBY)
freeze_time do
do_something
end
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of `DateTime.new` with arguments' do
expect_no_offenses(<<~RUBY)
travel_to(DateTime.new(2022, 5, 3, 12, 0, 0))
RUBY
end
it 'registers an offense when using `travel_to` with an argument of the current time and `{}` block' do
expect_offense(<<~RUBY)
travel_to(Time.now) { do_something }
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
RUBY

it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
expect_no_offenses(<<~RUBY)
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
RUBY
end
expect_correction(<<~RUBY)
freeze_time { do_something }
RUBY
end

it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
expect_offense(<<~RUBY)
around do |example|
travel_to(Time.current, &example)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
end
RUBY

expect_correction(<<~RUBY)
around do |example|
freeze_time(&example)
end
RUBY
end

it 'does not register an offense when using `freeze_time`' do
expect_no_offenses(<<~RUBY)
freeze_time
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of the not current time' do
expect_no_offenses(<<~RUBY)
travel_to(Time.current.yesterday)
travel_to(Time.zone.tomorrow)
travel_to(DateTime.next_day)
travel_to(Time.zone.yesterday.in_time_zone)
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of `current` method without receiver' do
expect_no_offenses(<<~RUBY)
travel_to(current)
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of `DateTime.new` with arguments' do
expect_no_offenses(<<~RUBY)
travel_to(DateTime.new(2022, 5, 3, 12, 0, 0))
RUBY
end

it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
expect_no_offenses(<<~RUBY)
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
RUBY
end

it 'does not register an offense when using `travel_to` without argument' do
expect_no_offenses(<<~RUBY)
travel_to
RUBY
it 'does not register an offense when using `travel_to` without argument' do
expect_no_offenses(<<~RUBY)
travel_to
RUBY
end
end
end

0 comments on commit 0a96ee6

Please sign in to comment.