From c36fb0d91577fcdf6086895e8b84d08aad32f64f Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Fri, 26 Feb 2016 09:42:01 -0500 Subject: [PATCH 1/2] Fix for rspec rake tasks initializing in development --- lib/dotenv/rails.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index 0f2ae9c6..71b21400 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -1,5 +1,17 @@ require "dotenv" +# Fix for rspec rake tasks loading in development +# +# Dotenv loads environment variables when the Rails application is initialized. +# When running `rake`, the Rails application is initialized in development. +# Rails includes some hacks to set `RAILS_ENV=test` when running `rake test`, +# but rspec does not include the same hacks. +# +# See https://github.com/bkeepers/dotenv/issues/219 +if defined?(Rake.application) && Rake.application.top_level_tasks.grep(/^spec(:|$)/).any? + Rails.env = ENV['RAILS_ENV'] ||= 'test' +end + Dotenv.instrumenter = ActiveSupport::Notifications # Watch all loaded env files with Spring From 34e2ac78ecdae594c27558896bcde2a1ccfba526 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Fri, 26 Feb 2016 10:17:33 -0500 Subject: [PATCH 2/2] Fix rubocop warnings --- lib/dotenv/rails.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index 71b21400..90ac1e46 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -8,8 +8,9 @@ # but rspec does not include the same hacks. # # See https://github.com/bkeepers/dotenv/issues/219 -if defined?(Rake.application) && Rake.application.top_level_tasks.grep(/^spec(:|$)/).any? - Rails.env = ENV['RAILS_ENV'] ||= 'test' +if defined?(Rake.application) + is_running_specs = Rake.application.top_level_tasks.grep(/^spec(:|$)/).any? + Rails.env = ENV["RAILS_ENV"] ||= "test" if is_running_specs end Dotenv.instrumenter = ActiveSupport::Notifications