Skip to content

Commit 73fe04a

Browse files
authored
Merge pull request rubocop#918 from DRBragg/set_minimum_target_rails_version_for_freeze_time
Don't use FreezeTime cop in Rails <5.2
2 parents f77da83 + 186e6b4 commit 73fe04a

File tree

3 files changed

+143
-105
lines changed

3 files changed

+143
-105
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#918](https://github.com/rubocop/rubocop-rails/pull/918): Fix `Rails/FreezeTime` running against Rails < 5.2 apps. ([@DRBragg][])

lib/rubocop/cop/rails/freeze_time.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module Rails
2626
#
2727
class FreezeTime < Base
2828
extend AutoCorrector
29+
extend TargetRailsVersion
30+
31+
minimum_target_rails_version 5.2
2932

3033
MSG = 'Use `freeze_time` instead of `travel_to`.'
3134
NOW_METHODS = %i[now new current].freeze
Lines changed: 139 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,155 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::FreezeTime, :config do
4-
it 'registers an offense when using `travel_to` with an argument of the current time' do
5-
expect_offense(<<~RUBY)
6-
travel_to(Time.now)
7-
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
8-
travel_to(Time.new)
9-
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
10-
travel_to(DateTime.now)
11-
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
12-
travel_to(Time.current)
13-
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
14-
travel_to(Time.zone.now)
15-
^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
16-
travel_to(Time.now.in_time_zone)
17-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
18-
travel_to(Time.current.to_time)
19-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
20-
travel_to(::Time.now)
21-
^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
22-
travel_to(::DateTime.now)
23-
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
24-
travel_to(::Time.zone.now)
25-
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
26-
RUBY
27-
28-
expect_correction(<<~RUBY)
29-
freeze_time
30-
freeze_time
31-
freeze_time
32-
freeze_time
33-
freeze_time
34-
freeze_time
35-
freeze_time
36-
freeze_time
37-
freeze_time
38-
freeze_time
39-
RUBY
40-
end
4+
context 'Rails 5.1', :rails51 do
5+
it 'does not register an offense when using `travel_to` with an argument of the current time' do
6+
expect_no_offenses('travel_to(Time.now)')
7+
expect_no_offenses('travel_to(Time.new)')
8+
expect_no_offenses('travel_to(DateTime.now)')
9+
expect_no_offenses('travel_to(Time.current)')
10+
expect_no_offenses('travel_to(Time.zone.now)')
11+
end
4112

42-
it 'registers an offense when using `travel_to` with an argument of the current time and `do-end` block' do
43-
expect_offense(<<~RUBY)
44-
travel_to(Time.now) do
45-
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
46-
do_something
47-
end
48-
RUBY
49-
50-
expect_correction(<<~RUBY)
51-
freeze_time do
52-
do_something
53-
end
54-
RUBY
55-
end
13+
it 'does not register an offense when using `travel_to` with an argument of the current time and `do-end` block' do
14+
expect_no_offenses(<<~RUBY)
15+
travel_to(Time.now) do
16+
do_something
17+
end
18+
RUBY
19+
end
5620

57-
it 'registers an offense when using `travel_to` with an argument of the current time and `{}` block' do
58-
expect_offense(<<~RUBY)
59-
travel_to(Time.now) { do_something }
60-
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
61-
RUBY
21+
it 'does not register an offense when using `travel_to` with an argument of the current time and `{}` block' do
22+
expect_no_offenses(<<~RUBY)
23+
travel_to(Time.now) { do_something }
24+
RUBY
25+
end
6226

63-
expect_correction(<<~RUBY)
64-
freeze_time { do_something }
65-
RUBY
27+
it 'does not register an offense when using `travel_to` with an argument of the current time and proc argument' do
28+
expect_no_offenses(<<~RUBY)
29+
around do |example|
30+
travel_to(Time.current, &example)
31+
end
32+
RUBY
33+
end
6634
end
6735

68-
it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
69-
expect_offense(<<~RUBY)
70-
around do |example|
71-
travel_to(Time.current, &example)
72-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
73-
end
74-
RUBY
75-
76-
expect_correction(<<~RUBY)
77-
around do |example|
78-
freeze_time(&example)
79-
end
80-
RUBY
81-
end
36+
context 'Rails 5.2', :rails52 do
37+
it 'registers an offense when using `travel_to` with an argument of the current time' do
38+
expect_offense(<<~RUBY)
39+
travel_to(Time.now)
40+
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
41+
travel_to(Time.new)
42+
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
43+
travel_to(DateTime.now)
44+
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
45+
travel_to(Time.current)
46+
^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
47+
travel_to(Time.zone.now)
48+
^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
49+
travel_to(Time.now.in_time_zone)
50+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
51+
travel_to(Time.current.to_time)
52+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
53+
travel_to(::Time.now)
54+
^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
55+
travel_to(::DateTime.now)
56+
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
57+
travel_to(::Time.zone.now)
58+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
59+
RUBY
8260

83-
it 'does not register an offense when using `freeze_time`' do
84-
expect_no_offenses(<<~RUBY)
85-
freeze_time
86-
RUBY
87-
end
61+
expect_correction(<<~RUBY)
62+
freeze_time
63+
freeze_time
64+
freeze_time
65+
freeze_time
66+
freeze_time
67+
freeze_time
68+
freeze_time
69+
freeze_time
70+
freeze_time
71+
freeze_time
72+
RUBY
73+
end
8874

89-
it 'does not register an offense when using `travel_to` with an argument of the not current time' do
90-
expect_no_offenses(<<~RUBY)
91-
travel_to(Time.current.yesterday)
92-
travel_to(Time.zone.tomorrow)
93-
travel_to(DateTime.next_day)
94-
travel_to(Time.zone.yesterday.in_time_zone)
95-
RUBY
96-
end
75+
it 'registers an offense when using `travel_to` with an argument of the current time and `do-end` block' do
76+
expect_offense(<<~RUBY)
77+
travel_to(Time.now) do
78+
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
79+
do_something
80+
end
81+
RUBY
9782

98-
it 'does not register an offense when using `travel_to` with an argument of `current` method without receiver' do
99-
expect_no_offenses(<<~RUBY)
100-
travel_to(current)
101-
RUBY
102-
end
83+
expect_correction(<<~RUBY)
84+
freeze_time do
85+
do_something
86+
end
87+
RUBY
88+
end
10389

104-
it 'does not register an offense when using `travel_to` with an argument of `DateTime.new` with arguments' do
105-
expect_no_offenses(<<~RUBY)
106-
travel_to(DateTime.new(2022, 5, 3, 12, 0, 0))
107-
RUBY
108-
end
90+
it 'registers an offense when using `travel_to` with an argument of the current time and `{}` block' do
91+
expect_offense(<<~RUBY)
92+
travel_to(Time.now) { do_something }
93+
^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
94+
RUBY
10995

110-
it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
111-
expect_no_offenses(<<~RUBY)
112-
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
113-
RUBY
114-
end
96+
expect_correction(<<~RUBY)
97+
freeze_time { do_something }
98+
RUBY
99+
end
100+
101+
it 'registers an offense when using `travel_to` with an argument of the current time and proc argument' do
102+
expect_offense(<<~RUBY)
103+
around do |example|
104+
travel_to(Time.current, &example)
105+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `freeze_time` instead of `travel_to`.
106+
end
107+
RUBY
108+
109+
expect_correction(<<~RUBY)
110+
around do |example|
111+
freeze_time(&example)
112+
end
113+
RUBY
114+
end
115+
116+
it 'does not register an offense when using `freeze_time`' do
117+
expect_no_offenses(<<~RUBY)
118+
freeze_time
119+
RUBY
120+
end
121+
122+
it 'does not register an offense when using `travel_to` with an argument of the not current time' do
123+
expect_no_offenses(<<~RUBY)
124+
travel_to(Time.current.yesterday)
125+
travel_to(Time.zone.tomorrow)
126+
travel_to(DateTime.next_day)
127+
travel_to(Time.zone.yesterday.in_time_zone)
128+
RUBY
129+
end
130+
131+
it 'does not register an offense when using `travel_to` with an argument of `current` method without receiver' do
132+
expect_no_offenses(<<~RUBY)
133+
travel_to(current)
134+
RUBY
135+
end
136+
137+
it 'does not register an offense when using `travel_to` with an argument of `DateTime.new` with arguments' do
138+
expect_no_offenses(<<~RUBY)
139+
travel_to(DateTime.new(2022, 5, 3, 12, 0, 0))
140+
RUBY
141+
end
142+
143+
it 'does not register an offense when using `travel_to` with an argument of `Time.new(...).in_time_zone`' do
144+
expect_no_offenses(<<~RUBY)
145+
travel_to(Time.new(2019, 4, 3, 12, 30).in_time_zone)
146+
RUBY
147+
end
115148

116-
it 'does not register an offense when using `travel_to` without argument' do
117-
expect_no_offenses(<<~RUBY)
118-
travel_to
119-
RUBY
149+
it 'does not register an offense when using `travel_to` without argument' do
150+
expect_no_offenses(<<~RUBY)
151+
travel_to
152+
RUBY
153+
end
120154
end
121155
end

0 commit comments

Comments
 (0)