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

Lucky aware loading #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions spec/lucky_env_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require "./spec_helper"

# The default environments are configured to look for Lucky-specific environment
# files. We redefine them here for testing purposes.
LuckyEnv.add_env :development, env_file: "./spec/support/.env"
LuckyEnv.add_env :production, env_file: "./spec/support/.env.production"
LuckyEnv.add_env :test, env_file: "./spec/support/.env.test"

describe LuckyEnv do
original_env = ENV.to_h

Expand All @@ -25,6 +31,36 @@ describe LuckyEnv do
ENV["LUCKY_ENV"].should eq "test"
ENV["DEV_PORT"].should eq "3500"
end

context "when no file path is provided" do
it "reads the environment file based on the current environment" do
ENV["LUCKY_ENV"] = "development"
LuckyEnv.load
ENV["ENV_FILE"]?.should eq(".env")

ENV["LUCKY_ENV"] = "production"
LuckyEnv.load
ENV["ENV_FILE"]?.should eq(".env.production")

ENV["LUCKY_ENV"] = "test"
LuckyEnv.load
ENV["ENV_FILE"]?.should eq(".env.test")
end

context "when environment does not exist" do
it "raises an error" do
ENV["LUCKY_ENV"] = "staging"
expected_msg = <<-MSG
Unknown environment staging. Have you forgotten to add it?
LuckyEnv.add_env :staging, env_file: File.expand_path(".env.staging")
MSG
expect_raises(LuckyEnv::UnknownEnvironmentError, expected_msg) do
LuckyEnv.load
end
end
end
end
end

describe "load?" do
Expand All @@ -40,6 +76,29 @@ describe LuckyEnv do
data["LUCKY_ENV"].should eq "test"
ENV["LUCKY_ENV"].should eq "test"
end

context "when no file path is provided" do
it "reads the environment file based on the current environment" do
ENV["LUCKY_ENV"] = "development"
LuckyEnv.load?
ENV["ENV_FILE"]?.should eq(".env")

ENV["LUCKY_ENV"] = "production"
LuckyEnv.load?
ENV["ENV_FILE"]?.should eq(".env.production")

ENV["LUCKY_ENV"] = "test"
LuckyEnv.load?
ENV["ENV_FILE"]?.should eq(".env.test")
end

context "when environment does not exist" do
it "returns nil" do
ENV["LUCKY_ENV"] = "staging"
LuckyEnv.load?.should be_nil
end
end
end
end

describe ".development?" do
Expand Down
1 change: 1 addition & 0 deletions spec/support/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DEV_PORT=3500
SECRET_KEY_BASE=j5I0VrpzT1Of7dhCA=
ASSET_HOST="https://luckyframework.org"
ENV_WITH_SPACE= start_end
ENV_FILE=".env"
2 changes: 2 additions & 0 deletions spec/support/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LUCKY_ENV=production
ENV_FILE=.env.production
1 change: 1 addition & 0 deletions spec/support/.env.test
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
LUCKY_ENV=test
ENV_FILE=.env.test
1 change: 0 additions & 1 deletion spec/support/production_env

This file was deleted.

45 changes: 40 additions & 5 deletions src/lucky_env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ require "./lucky_env/string_modifier"
require "./lucky_env/*"

module LuckyEnv
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
ENVIRONMENTS = {} of String => String

macro add_env(name)
macro add_env(name, env_file)
LuckyEnv::ENVIRONMENTS[{{ name.id.stringify }}] = {{ env_file }}

# Check if the current `environment` is `{{ name.id.stringify }}`.
def LuckyEnv.{{ name.id }}?
environment == {{ name.id.stringify }}
end
end

# Load the results of parsing the env file (see `add_env`) associated with the
# current Lucky `environment` into `ENV`.
#
# If the environment is unknown, it raises an `UnknownEnvironmentError`.
# If the associated env file does not exist, it raises a `MissingFileError`.
def self.load : Hash(String, String)
if ENVIRONMENTS.has_key?(environment)
load(ENVIRONMENTS[environment])
else
raise UnknownEnvironmentError.new <<-ERROR
Unknown environment #{environment}. Have you forgotten to add it?
LuckyEnv.add_env :#{environment}, env_file: File.expand_path(".env.#{environment}")
ERROR
end
end

# Parses the `file_path`, and loads the results in to `ENV`
# raises `LuckyEnv::MissingFileError` if the file is missing
def self.load(file_path : String) : Hash(String, String)
Expand All @@ -23,22 +44,36 @@ module LuckyEnv
data
end

# Load the results of parsing the env file (see `add_env`) associated with the
# current Lucky `environment` into `ENV`.
#
# If the environment is unknown or the associated env file does not exist, it
# returns `Nil`.
def self.load? : Hash(String, String)?
load?(ENVIRONMENTS[environment]) if ENVIRONMENTS.has_key?(environment)
end

# Returns `nil` if the file is missing
def self.load?(file_path : String) : Hash(String, String)?
if File.exists?(file_path) || File.symlink?(file_path)
load(file_path)
end
end

# Check if a [LuckyTask](https://github.com/luckyframework/lucky_task) is
# currently running.
def self.task?
ENV["LUCKY_TASK"] == "true" || ENV["LUCKY_TASK"] == "1"
end

# Returns the current Lucky environment (`ENV["LUCKY_ENV"]`).
#
# If `ENV["LUCKY_ENV"]` is not set, it defaults to `"development"`.
def self.environment
ENV.fetch("LUCKY_ENV", "development")
end

add_env :development
add_env :production
add_env :test
add_env :development, env_file: File.expand_path(".env")
add_env :production, env_file: File.expand_path(".env.production")
add_env :test, env_file: File.expand_path(".env.test")
end
3 changes: 3 additions & 0 deletions src/lucky_env/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ module LuckyEnv

class MissingFileError < Exception
end

class UnknownEnvironmentError < Exception
end
end