File: autorestore.rb

package info (click to toggle)
ruby-dotenv 3.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156 kB
  • sloc: ruby: 539; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Automatically restore `ENV` to its original state after

if defined?(RSpec.configure)
  RSpec.configure do |config|
    # Save ENV before the suite starts
    config.before(:suite) { Dotenv.save }

    # Restore ENV after each example
    config.after { Dotenv.restore }
  end
end

if defined?(ActiveSupport)
  ActiveSupport.on_load(:active_support_test_case) do
    ActiveSupport::TestCase.class_eval do
      # Save ENV before each test
      setup { Dotenv.save }

      # Restore ENV after each test
      teardown do
        Dotenv.restore
      rescue ThreadError => e
        # Restore will fail if running tests in parallel.
        warn e.message
        warn "Set `config.dotenv.autorestore = false` in `config/initializers/test.rb`" if defined?(Dotenv::Rails)
      end
    end
  end
end