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 env predicates #13

Merged
merged 3 commits into from
Jul 5, 2021
Merged
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
112 changes: 112 additions & 0 deletions spec/lucky_env_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,116 @@ describe LuckyEnv do
ENV["LUCKY_ENV"].should eq "test"
end
end

describe ".development?" do
context "when LUCKY_ENV is not set" do
it "returns true" do
with_env("LUCKY_ENV", nil) do
LuckyEnv.development?.should be_true
end
end
end

context "when LUCKY_ENV is set to 'development'" do
it "returns true" do
with_env("LUCKY_ENV", "development") do
LuckyEnv.development?.should be_true
end
end
end

context "when LUCKY_ENV is set to something else" do
it "returns false" do
with_env("LUCKY_ENV", "test") do
LuckyEnv.development?.should be_false
end
end
end
end

describe ".test?" do
context "when LUCKY_ENV is not set" do
it "returns false" do
with_env("LUCKY_ENV", nil) do
LuckyEnv.test?.should be_false
end
end
end

context "when LUCKY_ENV is set to 'test'" do
it "returns true" do
with_env("LUCKY_ENV", "test") do
LuckyEnv.test?.should be_true
end
end
end

context "when LUCKY_ENV is set to something else" do
it "returns false" do
with_env("LUCKY_ENV", "development") do
LuckyEnv.test?.should be_false
end
end
end
end

describe ".production?" do
context "when LUCKY_ENV is not set" do
it "returns true" do
with_env("LUCKY_ENV", nil) do
LuckyEnv.production?.should be_false
end
end
end

context "when LUCKY_ENV is set to 'production'" do
it "returns true" do
with_env("LUCKY_ENV", "production") do
LuckyEnv.production?.should be_true
end
end
end

context "when LUCKY_ENV is set to something else" do
it "returns false" do
with_env("LUCKY_ENV", "test") do
LuckyEnv.production?.should be_false
end
end
end
end

describe ".task?" do
context "when LUCKY_TASK is set to 'true'" do
it "returns true" do
with_env("LUCKY_TASK", "true") do
LuckyEnv.task?.should be_true
end
end
end

context "when LUCKY_TASK is set to '1'" do
it "returns true" do
with_env("LUCKY_TASK", "1") do
LuckyEnv.task?.should be_true
end
end
end

context "when LUCKY_TASK is set to something else" do
it "returns false" do
with_env("LUCKY_TASK", "false") do
LuckyEnv.task?.should be_false
end
end
end
end
end

private def with_env(key, value, &block)
original_value = ENV[key]?
ENV[key] = value
block.call
ensure
ENV[key] = original_value
Comment on lines +144 to +149
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Love this.

end
18 changes: 18 additions & 0 deletions src/lucky_env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ require "./lucky_env/*"
module LuckyEnv
VERSION = "0.1.2"

macro add_env(name)
def LuckyEnv.{{ name.id }}?
environment == {{ name.id.stringify }}
end
end

Comment on lines +8 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! Ok, this is awesome. This means in a generated app, we can have the config/env.cr file be some comments on how to use this to configure additional envs plus document using these methods 🥳 Good call ❤️

# 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,4 +29,16 @@ module LuckyEnv
load(file_path)
end
end

def self.task?
ENV["LUCKY_TASK"] == "true" || ENV["LUCKY_TASK"] == "1"
end

def self.environment
ENV.fetch("LUCKY_ENV", "development")
end

add_env :development
add_env :production
add_env :test
end