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

Rails/FreezeTime cop erroring out #762

Closed
tomdracz opened this issue Sep 9, 2022 · 2 comments · Fixed by #765
Closed

Rails/FreezeTime cop erroring out #762

tomdracz opened this issue Sep 9, 2022 · 2 comments · Fixed by #765
Labels
bug Something isn't working

Comments

@tomdracz
Copy link

tomdracz commented Sep 9, 2022

Rails/FreezeTime cop errors out when processing a spec file as below:

RSpec.describe DatetimeHelper, type: :helper do
  describe '#date_in_zone' do
    subject(:result) { date_in_zone(Time.current) }

    around do |example|
      travel_to(current) do
        Time.use_zone(timezone) do
          example.run
        end
      end
    end

    context 'Europe/London timezone' do
      let(:timezone) { 'Europe/London' }
      let(:current) { Time.zone.local(2021, 3, 4, 9) }

      it { expect(result).to eq('Thursday 4th March') }
    end
  end
end

Running with -d flag, it seems to break with the error:

undefined method `send_type?' for nil:NilClass

          node.send_type? ? zoned_time_now?(node) : time_now?(node)

The problem seems to be with how the current argument is passed to travel_to - if changed to some time passed directly like:

travel_to(Time.zone.local(2021, 3, 4, 9)) do

No errors occur


RuboCop version

$ [bundle exec] rubocop -V
1.36.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.1.1) [arm64-darwin21]
  - rubocop-rails 2.16.0
  - rubocop-rspec 2.12.1
@koic koic added the bug Something isn't working label Sep 9, 2022
koic added a commit to koic/rubocop-rails that referenced this issue Sep 9, 2022
Fixes rubocop#762.

This PR fixes an error for `Rails/FreezeTime` when using `travel_to` with
an argument of `current` method without receiver.
@bf4
Copy link

bf4 commented Sep 9, 2022

lazy bug report, following #714

An error occurred while Rails/FreezeTime cop was inspecting spec/policies/newsletter_policy_spec.rb:58:14.
undefined method `send_type?' for nil:NilClass
.bundle/vendor/ruby/3.0.0/gems/rubocop-rails-2.16.0/lib/rubocop/cop/rails/freeze_time.rb:57:in `current_time?'
.bundle/vendor/ruby/3.0.0/gems/rubocop-rails-2.16.0/lib/rubocop/cop/rails/freeze_time.rb:47:in `on_send'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:136:in `public_send'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:134:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:70:in `on_send'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:153:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
.bundle/vendor/ruby/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:20:in `walk'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/team.rb:77:in `investigate'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:311:in `inspect_file'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:255:in `block in do_inspection_loop'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:289:in `block in iterate_until_no_changes'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:282:in `loop'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:282:in `iterate_until_no_changes'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:251:in `do_inspection_loop'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:134:in `block in file_offenses'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:159:in `file_offense_cache'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:133:in `file_offenses'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:124:in `process_file'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:105:in `block in each_inspected_file'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `each'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `reduce'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `each_inspected_file'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:90:in `inspect_files'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:47:in `run'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command.rb:11:in `run'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/environment.rb:18:in `run'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:72:in `run_command'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:79:in `execute_runners'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:48:in `run'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/exe/rubocop:19:in `block in <top (required)>'
ruby-3.0.3/lib/ruby/3.0.0/benchmark.rb:308:in `realtime'
.bundle/vendor/ruby/3.0.0/gems/rubocop-1.35.0/exe/rubocop:19:in `<top (required)>'
.bundle/vendor/ruby/3.0.0/bin/rubocop:25:in `load'
.bundle/vendor/ruby/3.0.0/bin/rubocop:25:in `<main>'
.rvm/rubies/ruby-3.0.3/bin/ruby_executable_hooks:22:in `eval'
.rvm/rubies/ruby-3.0.3/bin/ruby_executable_hooks:22:in `<main>'

An error occurred while Rails/FreezeTime cop was inspecting spec/policies/newsletter_policy_spec.rb:70:12.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.35.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.0.3 x86_64-darwin21)
Finished in 0.8817499997094274 seconds

code there is

    let!(:now) do
      Time.utc(2021, 4, 16, 10, 5)
    end

          it "does not permit" do
            travel_to now do
              expect(policy).not_to permit(user, record)
            end
          end

@koic
Copy link
Member

koic commented Sep 9, 2022

@bf4 Thank you for the feedback. The error will be fixed by #765.

@koic koic closed this as completed in #765 Sep 10, 2022
koic added a commit that referenced this issue Sep 10, 2022
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.

3 participants