Sha256: 4c6acefb6c49af0c58cc04ba823603a70eb5834533cde2d037c82005fe1783a8

Contents?: true

Size: 862 Bytes

Versions: 35

Compression:

Stored size: 862 Bytes

Contents

# frozen_string_literal: true

module Anyway
  module Testing
    module Helpers
      # Sets the ENV variables to the provided
      # values and restore outside the block
      #
      # Also resets Anyway.env before and after calling the block
      # to make sure that the values are not cached.
      #
      # NOTE: to remove the env value, pass `nil` as the value
      def with_env(data)
        was_values = []

        data.each do |key, val|
          was_values << [key, ENV[key]]
          next ENV.delete(key) if val.nil?
          ENV[key] = val
        end

        # clear cached env values
        Anyway.env.clear
        yield
      ensure
        was_values.each do |(key, val)|
          next ENV.delete(key) if val.nil?
          ENV[key] = val
        end

        # clear cache again
        Anyway.env.clear
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
anyway_config-2.6.4 lib/anyway/testing/helpers.rb
anyway_config-2.6.3 lib/anyway/testing/helpers.rb
anyway_config-2.6.2 lib/anyway/testing/helpers.rb
anyway_config-2.6.1 lib/anyway/testing/helpers.rb
anyway_config-2.6.0 lib/anyway/testing/helpers.rb
runger_config-3.0.1 lib/anyway/testing/helpers.rb
runger_config-3.0.0 lib/anyway/testing/helpers.rb
runger_config-2.7.0 lib/anyway/testing/helpers.rb
runger_config-2.6.1 lib/anyway/testing/helpers.rb
runger_config-2.6.0 lib/anyway/testing/helpers.rb
anyway_config-2.5.4 lib/anyway/testing/helpers.rb
anyway_config-2.5.3 lib/anyway/testing/helpers.rb
anyway_config-2.5.2 lib/anyway/testing/helpers.rb
anyway_config-2.5.1 lib/anyway/testing/helpers.rb
anyway_config-2.5.0 lib/anyway/testing/helpers.rb
anyway_config-2.4.2 lib/anyway/testing/helpers.rb
anyway_config-2.4.1 lib/anyway/testing/helpers.rb
anyway_config-2.4.0 lib/anyway/testing/helpers.rb
anyway_config-2.3.1 lib/anyway/testing/helpers.rb
anyway_config-2.3.0 lib/anyway/testing/helpers.rb