Sha256: d014b4ccd06049c31a3cc47946830c122563f737f3be5f6527f15c4d6de75e66

Contents?: true

Size: 1.33 KB

Versions: 19

Compression:

Stored size: 1.33 KB

Contents

require 'singleton'
require 'thread'

module RSpec
  module Support
    class Env
      include Singleton

      def self.setup
        instance.__send__(:setup)
      end

      def self.reset
        instance.__send__(:setup)
      end

      def self.env
        instance.to_h
      end

      def self.[](key)
        instance[key]
      end

      def self.[]=(key, value)
        instance[key] = value
      end

      def self.fetch_from_original(key)
        instance.__send__(:original).fetch(key)
      end

      def initialize
        @original = ENV.to_hash
        @mutex    = Mutex.new
        setup
      end

      def [](key)
        synchronize do
          env[key]
        end
      end

      def []=(key, value)
        synchronize do
          env[key] = value
        end
      end

      def to_h
        env.dup
      end

      private

      attr_reader :original, :env

      def setup
        synchronize do
          @env = {}
        end

        self["GEM_ROOT"] = original["GEM_ROOT"]
        self["GEM_HOME"] = original["GEM_HOME"]
        self["GEM_PATH"] = original["GEM_PATH"]
      end

      def synchronize(&blk)
        @mutex.synchronize(&blk)
      end
    end
  end
end

RSpec.configure do |config|
  config.before(:suite) do
    RSpec::Support::Env.setup
  end

  config.after do
    RSpec::Support::Env.reset
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
dry-web-web_pipe-0.2.0 spec/support/env.rb
dry-web-roda-0.13.1 spec/support/env.rb
dry-web-roda-0.13.0 spec/support/env.rb
dry-web-web_pipe-0.1.0 spec/support/env.rb
dry-web-roda-0.12.0 spec/support/env.rb
dry-web-roda-0.11.0 spec/support/env.rb
dry-web-roda-0.10.0 spec/support/env.rb
dry-web-roda-0.9.1 spec/support/env.rb
dry-web-roda-0.9.0 spec/support/env.rb
dry-web-roda-0.8.0 spec/support/env.rb
dry-web-roda-0.7.5 spec/support/env.rb
dry-web-roda-0.7.4 spec/support/env.rb
dry-web-roda-0.7.3 spec/support/env.rb
dry-web-roda-0.7.2 spec/support/env.rb
dry-web-roda-0.7.1 spec/support/env.rb
dry-web-roda-0.7.0 spec/support/env.rb
dry-web-roda-0.6.3 spec/support/env.rb
dry-web-roda-0.6.2 spec/support/env.rb
dry-web-roda-0.6.1 spec/support/env.rb