Coder Social home page Coder Social logo

Comments (8)

dideler avatar dideler commented on July 22, 2024 1

FYI this issue only occurs for me on my Rails project when I use dotenv-rails and Dotenv::Railtie.load when running rake. Does not occur when using dotenv with Dotenv.load.

Tried with dotenv-rails 2.0.2.

from dotenv.

bkeepers avatar bkeepers commented on July 22, 2024

The more I think about it, option 1 has the same problem as the current issue. Test helpers load config/environment.rb, which loads config/application.rb and then initializes. So there's not a good place for the spec helper to load the .env.test in the bootstrap process.

Option 4: Continue to load .env immediately, but wait to load .env.#{Rails.env} before initialization. This would allow most ENV vars to still be available in config/application.rb, but environment specific configs would not be available until just before config/environments/* and config/initializers/* are loaded.

from dotenv.

jonmagic avatar jonmagic commented on July 22, 2024

In the app we are currently working on all the environment specific vars are used in modules and classes in our lib folder, and each of those is required at the bottom of config/application.rb. We could move that require to an initializer in order to support option 4.

In our specific case option 3 also works (at least as our config/application.rb stands right now).

Option 2 sounds like it adds too much complication to be worth it.

from dotenv.

chamnap avatar chamnap commented on July 22, 2024

Another way to avoid passing RAILS_ENV=test is to use different keys for development, test, and production environment:

MYSQL2_DATABASE_DEVELOPMENT=
MYSQL2_USERNAME_DEVELOPMENT=
MYSQL2_PASSWORD_DEVELOPMENT=
MYSQL2_HOST_DEVELOPMENT=

MYSQL2_DATABASE_TEST=
MYSQL2_USERNAME_TEST=
MYSQL2_PASSWORD_TEST=
MYSQL2_HOST_TEST=

development:
adapter: mysql2
encoding: utf8
collation: utf8_unicode_ci
database: <%= ENV['MYSQL2_DATABASE_DEVELOPMENT'] %>
username: <%= ENV['MYSQL2_USERNAME_DEVELOPMENT'] %>
password: <%= ENV['MYSQL2_PASSWORD_DEVELOPMENT'] %>
host: <%= ENV['MYSQL2_HOST_DEVELOPMENT'] %>

test:
adapter: mysql2
encoding: utf8
collation: utf8_unicode_ci
database: <%= ENV['MYSQL2_DATABASE_TEST'] %>
username: <%= ENV['MYSQL2_USERNAME_TEST'] %>
password: <%= ENV['MYSQL2_PASSWORD_TEST'] %>
host: <%= ENV['MYSQL2_HOST_TEST'] %>

from dotenv.

replaid avatar replaid commented on July 22, 2024

I just ran into this trying to use dotenv with DATABASE_URL...ugh, so close and yet so far. I was excited.

I notice we're talking specifically about Rails rake tasks. Perhaps a solution could involve having dotenv-rails reach in and apply a new :dotenv_test task as a prerequisite for Rails test tasks?

from dotenv.

bradrobertson avatar bradrobertson commented on July 22, 2024

I was just burned by this also. I basically just have a Dotenv.overload '.env.test' if Rails.env.test? in an initializer right now. Not ideal but it fixed the issue

from dotenv.

 avatar commented on July 22, 2024

Still seeing this with dotenv-rails & dotenv 2.0.1. Specifically, rake spec is picking up config from .env.development and not .env.test.

Removing dotenv-rails and just using dotenv 2.0.1 by manually calling Dotenv.load in spec_helper doesnโ€™t load .env.development or .env.test.

from dotenv.

nickgnd avatar nickgnd commented on July 22, 2024

As already @dideler said, I solved using dotenv gem instead of dotenv-rails.

Simply load the environment in application.rb and override where you need it (ex. test_helper.rb:

# config/application.rb
# ...
Bundler.require(*Rails.groups)

# Load env variables
require 'dotenv'
Dotenv.load ".env.local", ".env.#{Rails.env}"

# ...
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'

require 'dotenv'
Dotenv.overload ".env.#{Rails.env}"

# ...

and for tasks which address test environment

# force test environment
ENV['RAILS_ENV'] = 'test'
require 'dotenv'
Dotenv.overload ".env.#{Rails.env}"

namespace :test do

  desc "Console in test environment"
  task :console do
    system("bin/rails c")
  end
end

tested with:

  • ruby 2.3.0
  • rails 5.0.0
  • dotenv 2.1.1

from dotenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.