From ec2c6016af3884c462d2eacd21b37453edcf8964 Mon Sep 17 00:00:00 2001 From: Erwan Thomas Date: Mon, 26 Oct 2020 13:00:09 +0100 Subject: [PATCH] Do load .env.local in Rails' test environment This basically reverts https://github.com/bkeepers/dotenv/pull/280. The above-mentioned PR says: > When using .env.local for tests, the tests are not transparent and > can't be executed for example on a CI server. > As the documentation says, the .env.local is here for local > overwrites. They should not overwrite a test specific variable. Tests > should be run on every machine in every environment and shouldn't > require any specification of environment variables. While that description matches the expected behavior of Dotenv, the implementation goes too far (1) and even contradicts other bits of Dotenv's current behavior (2). About (1), a [quote][jakedouglas-comment] from the Github user named jakedouglas: >> Tests should be run on every machine in every environment and >> shouldn't require any specification of environment variables. > > At least one very common example of this idea not holding water is > that most Rails applications require a database, and the URL of that > database may vary between different developer machines, and between > developer machines and a CI service. Given this PR, if a developer > happens to have a database setup that doesn't match the > application's defaults (common), they need to specify the database > URL in both .env.local and .env.test.local in order to do local > development comprehensively. About (2), anything that .env.local could do to the test environment variables before it became ignored for that environment can now be done with .env.test.local, often at the cost of tediously duplicating variables from the currently-ignored .env.local. This contradicts the behavior intended by PR #280 and consistency would dictate that .env.test.local be removed altogether. Only then the test variables would truly be guaranteed to always stay the same as far as Dotenv is concerned. Since (1) shows that local development might require local test variables for legitimate reasons, removing .env.test.local is not a valid option and it would make sense to instead restore loading .env.local for the test environment. [jakedouglas-comment]: https://github.com/bkeepers/dotenv/pull/280#issuecomment-536307113 --- README.md | 2 +- lib/dotenv/rails.rb | 2 +- spec/dotenv/rails_spec.rb | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f783220b..59411f41 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,7 @@ If you use this gem to handle env vars for multiple Rails environments (developm | 1st (highest) | `.env.development.local` | Development | Yes! | Local overrides of environment-specific settings. | | 1st | `.env.test.local` | Test | Yes! | Local overrides of environment-specific settings. | | 1st | `.env.production.local` | Production | Yes! | Local overrides of environment-specific settings. | -| 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments _except_ `test`. | +| 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments. | | 3rd | `.env.development` | Development | No. | Shared environment-specific settings | | 3rd | `.env.test` | Test | No. | Shared environment-specific settings | | 3rd | `.env.production` | Production | No. | Shared environment-specific settings | diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index c799ffb4..ebb1e43b 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -66,7 +66,7 @@ def self.load def dotenv_files [ root.join(".env.#{Rails.env}.local"), - (root.join(".env.local") unless Rails.env.test?), + root.join(".env.local"), root.join(".env.#{Rails.env}"), root.join(".env") ].compact diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index 110e2496..2ebcf46e 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -50,17 +50,18 @@ def add(*items) expect(Spring.watcher.items).to include(path) end - it "does not load .env.local in test rails environment" do + it "loads .env.local in test rails environment" do expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql( [ Rails.root.join(".env.test.local"), + Rails.root.join(".env.local"), Rails.root.join(".env.test"), Rails.root.join(".env") ] ) end - it "does load .env.local in development environment" do + it "loads .env.local in development environment" do Rails.env = "development" expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql( [ @@ -72,8 +73,8 @@ def add(*items) ) end - it "loads .env.test before .env" do - expect(ENV["DOTENV"]).to eql("test") + it "loads .env.local before .env" do + expect(ENV["DOTENV"]).to eql("local") end context "when Rails.root is nil" do @@ -91,17 +92,18 @@ def add(*items) context "overload" do before { Dotenv::Railtie.overload } - it "does not load .env.local in test rails environment" do + it "loads .env.local in test rails environment" do expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql( [ Rails.root.join(".env.test.local"), + Rails.root.join(".env.local"), Rails.root.join(".env.test"), Rails.root.join(".env") ] ) end - it "does load .env.local in development environment" do + it "loads .env.local in development environment" do Rails.env = "development" expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql( [ @@ -113,7 +115,7 @@ def add(*items) ) end - it "overloads .env.test with .env" do + it "overloads .env.local with .env" do expect(ENV["DOTENV"]).to eql("true") end