Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env is overriding .env.test #186

Closed
nisevi opened this issue May 20, 2015 · 2 comments
Closed

.env is overriding .env.test #186

nisevi opened this issue May 20, 2015 · 2 comments

Comments

@nisevi
Copy link

nisevi commented May 20, 2015

Hello. I would like to share with you something that is happening to me when I try to set an environment variable through .env.test file for testing purpose.

I have a file ".env" with the following code:

SOME_KEY_API="ENV_KEY"

and as I want to test some behavior I've created the file ".env.test" with the following code:

SOME_KEY_API="TEST_KEY"

and as it says the README file: "Variables defined in .env. will override any values set in .env or already defined in the environment."

so when check the environment while I'm testing, Rails.env is test, but when I check my ENV, SOME_KEY_API still is ENV_KEY. I was debugging the flow of the info through the gem and I was able to see that in the last iteration, in this part of the code:

"dotenv-2.0.1/lib/dotenv.rb"

# Internal: Helper to expand list of filenames.
#
# Returns a hash of all the loaded environment variables.
def with(*filenames, &block)
  filenames << ".env" if filenames.empty?

  filenames.reduce({}) do |hash, filename|
    hash.merge! block.call(File.expand_path(filename)) || {}
  end
end

"hash" has my SOME_KEY_API="TEST_KEY" and when the "merge" is applied is replacing "TEST_KEY" with "ENV_KEY". So finally for testing environment I never get the "TEST_KEY".

Could you check this behavior? and please tell me if you need more info about it.

@bkeepers
Copy link
Owner

I'm not able to duplicate your issue. Can you give more information about how your app is configured and how you're seeing the behavior?

Here's what I did:

$ rails new foobar
$ echo 'gem "dotenv-rails" >> Gemfile
$ bundle
$ echo "FOO=.env" > .env
$ echo "FOO=.env.test" > .env.test
$ RAILS_ENV=test rails runner "puts ENV['FOO']"

.env.test

Rails 4.2.0
dotenv 2.0.1

@bkeepers
Copy link
Owner

@nisevi let me know if you're still having problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants