Class: RuboCop::Cop::Env::Assign

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/env/assign.rb

Overview

Rubocop Cop for Checking ENV Checks for any assignments to the ENV in tests.

When assigning to ENV you assign to a global state. This can affect other tests when you do not reassign the original value back to the ENV. This also can affect other tests when you are running tests in separate threads. This can lead to flaky and hard to find bugs in your test suite. It is much safer to mock the return value of the index getter to return the correct value.

Examples:

# bad
ENV["OPTS"] = "testopts"

# good
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("OPTS").and_return("testopts")

Constant Summary collapse

MSG =
'Mock the return value of an ENV variable instead of assigning to ENV directly.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
33
34
# File 'lib/rubocop/cop/env/assign.rb', line 30

def on_send(node)
  return unless env_assign?(node)

  add_offense(node)
end