Skip to content

Commit

Permalink
Merge pull request #280 from siegy22/no-local-when-test
Browse files Browse the repository at this point in the history
don't load .env.local in rails' test environment
  • Loading branch information
bkeepers authored Jan 27, 2017
2 parents f1f4324 + c1ebe6c commit 0f5e6fa
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
9 changes: 8 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

[Unreleased changes](https://github.com/bkeepers/dotenv/compare/v2.1.2...master)

## Unreleased

* [dotenv-rails] Allow to have a local file per environment, e.g. `.env.development.local` ([281](https://github.com/bkeepers/dotenv/pull/281))
* [dotenv-rails] No longer load `.env.local` in rails' test environment ([#280](https://github.com/bkeepers/dotenv/pull/280))

[Full Changelog](https://github.com/bkeepers/dotenv/compare/v2.1.1...v2.1.2)

## 2.1.2

* Fix parser to allow leading whitespace before variables ([#276](https://github.com/bkeepers/dotenv/pull/276))
* Fix bug with `require "dotenv/rails-now"` in older versions of rails ([#269](https://github.com/bkeepers/dotenv/pull/269))
* [dotenv-rails] Fix bug with `require "dotenv/rails-now"` in older versions of rails ([#269](https://github.com/bkeepers/dotenv/pull/269))

[Full Changelog](https://github.com/bkeepers/dotenv/compare/v2.1.1...v2.1.2)

Expand Down
18 changes: 12 additions & 6 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ class Railtie < Rails::Railtie
# This will get called during the `before_configuration` callback, but you
# can manually call `Dotenv::Railtie.load` if you needed it sooner.
def load
Dotenv.load(
root.join(".env.#{Rails.env}.local"),
root.join(".env.local"),
root.join(".env.#{Rails.env}"),
root.join(".env")
)
Dotenv.load(*dotenv_files)
end

# Internal: `Rails.root` is nil in Rails 4.1 before the application is
Expand All @@ -57,5 +52,16 @@ def self.load
end

config.before_configuration { load }

private

def dotenv_files
[
root.join(".env.#{Rails.env}.local"),
(root.join(".env.local") unless Rails.env.test?),
root.join(".env.#{Rails.env}"),
root.join(".env")
].compact
end
end
end
29 changes: 20 additions & 9 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "spec_helper"
ENV["RAILS_ENV"] = "test"
require "rails"
require "dotenv/rails"

Expand All @@ -18,6 +17,7 @@ def add(*items)
end

before do
Rails.env = "test"
allow(Rails).to receive(:root)
.and_return Pathname.new(File.expand_path("../../fixtures", __FILE__))
Rails.application = double(:application)
Expand Down Expand Up @@ -50,19 +50,30 @@ def add(*items)
expect(Spring.watcher.items).to include(path)
end

it "loads .env, .env.local, and .env.#{Rails.env}" do
expect(Spring.watcher.items).to eql(
it "does not load .env.local in test rails environment" do
expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql(
[
Rails.root.join(".env.test.local").to_s,
Rails.root.join(".env.local").to_s,
Rails.root.join(".env.test").to_s,
Rails.root.join(".env").to_s
Rails.root.join(".env.test.local"),
Rails.root.join(".env.test"),
Rails.root.join(".env")
]
)
end

it "loads .env.local before .env" do
expect(ENV["DOTENV"]).to eql("local")
it "does load .env.local in development environment" do
Rails.env = "development"
expect(Dotenv::Railtie.instance.send(:dotenv_files)).to eql(
[
Rails.root.join(".env.development.local"),
Rails.root.join(".env.local"),
Rails.root.join(".env.development"),
Rails.root.join(".env")
]
)
end

it "loads .env.test before .env" do
expect(ENV["DOTENV"]).to eql("test")
end

context "when Rails.root is nil" do
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM_RAILS_ENV=.env.development
DOTENV=dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM_RAILS_ENV=.env.test
FROM_LOCAL=true
DOTENV=local
DOTENV=development-local
1 change: 1 addition & 0 deletions spec/fixtures/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FROM_RAILS_ENV=.env.test
DOTENV=test

0 comments on commit 0f5e6fa

Please sign in to comment.