autorestore.rb 801 B

1234567891011121314151617181920212223242526272829
  1. # Automatically restore `ENV` to its original state after
  2. if defined?(RSpec.configure)
  3. RSpec.configure do |config|
  4. # Save ENV before the suite starts
  5. config.before(:suite) { Dotenv.save }
  6. # Restore ENV after each example
  7. config.after { Dotenv.restore }
  8. end
  9. end
  10. if defined?(ActiveSupport)
  11. ActiveSupport.on_load(:active_support_test_case) do
  12. ActiveSupport::TestCase.class_eval do
  13. # Save ENV before each test
  14. setup { Dotenv.save }
  15. # Restore ENV after each test
  16. teardown do
  17. Dotenv.restore
  18. rescue ThreadError => e
  19. # Restore will fail if running tests in parallel.
  20. warn e.message
  21. warn "Set `config.dotenv.autorestore = false` in `config/initializers/test.rb`" if defined?(Dotenv::Rails)
  22. end
  23. end
  24. end
  25. end