Skip to content

Commit

Permalink
Dotenviable: Load env into another hash
Browse files Browse the repository at this point in the history
If `Dotenviable.load(env:)` is passed, load all config into `env`,
not just the port and rack_env.

Allows loading of alternative environments,
usually needed only for testing, but sometimes in other
situations (like loading development database urls
from a test environment).
  • Loading branch information
rgalanakis committed Apr 15, 2024
1 parent 4063cb7 commit a8b3292
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/appydays/dotenviable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
# it won't get used, because .env files don't stomp what is already in the environment
# (we don't want to use `overload`).
# So we have some trickery to overwrite only PORT.
#
# @param rack_env [nil,String] Value like 'development' or 'production' to use to load .env files.
# If not given, use +env['RACK_ENV']+ or +default_rack_env+.
# @param default_rack_env [String] If +env['RACK_ENV']+ is not set, use this value.
# @param env [Hash] Hash to read and mutate.
# Pass in a different hash to load environment variables into it instead of ENV.
# Useful for testing, or to get the config for another environment.
module Appydays::Dotenviable
def self.load(rack_env: nil, default_rack_env: "development", env: ENV)
original_port = env.delete("PORT")
Expand All @@ -33,7 +40,17 @@ def self.load(rack_env: nil, default_rack_env: "development", env: ENV)
".env.#{rack_env}",
".env",
]
orig_env = nil
if env.object_id != ENV.object_id
orig_env = ENV.to_h
ENV.replace(env)
end
Dotenv.load(*paths)
if orig_env
env.merge!(ENV)
ENV.replace(orig_env)
end

env["PORT"] ||= original_port
env["RACK_ENV"] ||= rack_env
end
Expand Down
14 changes: 14 additions & 0 deletions spec/appydays/dotenviable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@
described_class.load(env: env)
expect(env).to include("PORT" => "456")
end

it "can load into a separate hash" do
ENV["HASHTESTABC"] = "x"
File.write(".env.hashtest", "HASHTESTXYZ=a")
e = {}
described_class.load(rack_env: "hashtest", env: e)
expect(e).to include("HASHTESTXYZ" => "a")
expect(e).to_not include("HASHTESTABC")
expect(ENV.to_h).to_not include("HASHTESTXYZ")
expect(ENV.to_h).to include("HASHTESTABC" => "x")
ensure
File.delete(".env.hashtest")
ENV.delete("HASHTESTABC")
end
end

0 comments on commit a8b3292

Please sign in to comment.