diff --git a/changelog/fix_set_minimum_target_rails_version_for_freeze_time.md b/changelog/fix_set_minimum_target_rails_version_for_freeze_time.md new file mode 100644 index 0000000000..9b3a1d30cb --- /dev/null +++ b/changelog/fix_set_minimum_target_rails_version_for_freeze_time.md @@ -0,0 +1 @@ +* Fix `Rails/FreezeTime` running against Rails < 5.2 apps. ([@DRBragg][]) diff --git a/lib/rubocop/cop/rails/freeze_time.rb b/lib/rubocop/cop/rails/freeze_time.rb index fff5b37a4e..3a9bc863b6 100644 --- a/lib/rubocop/cop/rails/freeze_time.rb +++ b/lib/rubocop/cop/rails/freeze_time.rb @@ -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 diff --git a/spec/rubocop/cop/rails/freeze_time_spec.rb b/spec/rubocop/cop/rails/freeze_time_spec.rb index 22ae206e56..e084613302 100644 --- a/spec/rubocop/cop/rails/freeze_time_spec.rb +++ b/spec/rubocop/cop/rails/freeze_time_spec.rb @@ -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