Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] Rails/FreezeTime gives invalid suggestion when travel_to is passed a Ruby expression #848

Closed
christos opened this issue Oct 31, 2022 · 0 comments · Fixed by #851
Labels
bug Something isn't working

Comments

@christos
Copy link

christos commented Oct 31, 2022

This looks like a partial regression of #769

It happens when the arguments passed to travel_to is a Ruby expression more complex than simply a Time/Date instantiation.

  • Time.new(2019, 4, 3, 12, 30).in_time_zone triggers the bug
  • Time.new(2019, 4, 3, 12, 30) doesn't trigger the bug
  • Time.new.in_time_zone(2019, 4, 3, 12, 30) doesn't trigger the bug is not valid syntax

Expected behaviour

RuboCop Rails should not have suggested to replace travel_to a_ruby_expression with freeze_time

Actual behavior

RuboCop Rails suggested to autocorrect a travel_to with a ruby expression with freeze_time

Steps to reproduce the problem

In a Rails 7 project, add the following code to a file spec/rails_time_freeze_spec.rb

# frozen_string_literal: true

travel_to Time.new(2019, 4, 3, 12, 30).in_time_zone do
  expect("travel_to").to_not eq("freeze_time")
end

Run bin/rubocop spec/rails_time_freeze_spec.rb

Output is

Inspecting 1 file
C

Offenses:

spec/rails_time_freeze_spec.rb:3:1: C: [Correctable] Rails/FreezeTime: Use freeze_time instead of travel_to.
travel_to Time.new(2019, 4, 3, 12, 30).in_time_zone do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

RuboCop version

$ [bundle exec] rubocop -V
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.2) [arm64-darwin21]
  - rubocop-performance 1.14.3
  - rubocop-rails 2.17.2
  - rubocop-rspec 2.14.2
koic added a commit to koic/rubocop-rails that referenced this issue Nov 1, 2022
Fixes rubocop#848.

This PR fixes a false positive for `Rails/FreezeTime`
when using `travel_to` with an argument of `Time.new(...).in_time_zone`.
@koic koic added the bug Something isn't working label Nov 1, 2022
@koic koic closed this as completed in #851 Nov 5, 2022
koic added a commit that referenced this issue Nov 5, 2022
…_time

[Fix #848] Fix a false positive for `Rails/FreezeTime`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants