Module: ProtectEnvVars

Defined in:
spec/spec_helper.rb

Overview

A module that provides functionality for protecting environment variables during tests.

This module ensures that environment variable changes made during test execution are automatically restored to their original values after the test completes. It is designed to prevent side effects between tests that modify environment variables, maintaining a clean testing environment.

Class Method Summary collapse

Class Method Details

.apply ⇒ Proc

The apply method creates a lambda that protects environment variables during test execution.

Returns:

  • (Proc) —

    a lambda that wraps test execution with environment variable preservation



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'spec/spec_helper.rb', line 90

def self.apply
  -> example do
    if example.[:protect_env]
      begin
        stored_env = ENV.to_h
        example.run
      ensure
        ENV.replace(stored_env)
      end
    else
      example.run
    end
  end
end