Last active
January 1, 2016 07:39
-
-
Save alainravet/8112925 to your computer and use it in GitHub Desktop.
mini DSL to validate environment variables presence and value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage : | |
# EnvVarsChecker.new do | |
# env_includes 'USER' | |
# env_includes 'USER', in: -> { `echo $USER`.chomp} | |
# env_includes 'USER', in: %w(deployer deploy rails) | |
# end | |
class EnvVarsChecker | |
class Error < RuntimeError ; end | |
def initialize(&block) | |
instance_eval(&block) | |
end | |
def env_includes(var_name, options={}) | |
detect_unset_variable!(var_name) | |
detect_invalid_value!(var_name, options[:in]) if options[:in] | |
end | |
private | |
def detect_unset_variable!(var_name) | |
raise( Error, "ENV['#{var_name}'] not found") unless ENV[var_name] | |
end | |
def detect_invalid_value!(var_name, possible_values) | |
possible_values = possible_values.call if possible_values.is_a?(Proc) | |
return if possible_values.include?(ENV[var_name]) | |
raise( Error, "ENV['#{var_name}'] invalid value (must be one of #{possible_values.inspect})") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'env_vars_checker' | |
def assert_failure_detected(expected_message) | |
begin | |
yield | |
raise "***TEST FAILURE : an EnvVarsChecker::Error should have been raised" | |
rescue EnvVarsChecker::Error => e | |
raise "***TEST FAILURE : invalid message (#{e.message})" unless e.message == expected_message | |
end | |
end | |
#------------------------------------------------------------ | |
# Success : var is set and value is valid | |
EnvVarsChecker.new do | |
env_includes 'USER' | |
env_includes 'USER', in: -> { `echo $USER`.chomp} | |
env_includes 'USER', in: Array(`echo $USER`.chomp) | |
end | |
#------------------------------------------------------------ | |
# Failure : variable is not set | |
assert_failure_detected("ENV['an-unknown-variable'] not found") do | |
EnvVarsChecker.new do | |
env_includes 'an-unknown-variable' | |
end | |
end | |
# Failure : variable is set with invalid value | |
assert_failure_detected(%{ENV['USER'] invalid value (must be one of ["foobarbuz"])}) do | |
EnvVarsChecker.new do | |
env_includes 'USER', in: ["foobarbuz"] | |
end | |
end | |
# Failure : variable is set with invalid value (lambda) | |
assert_failure_detected(%{ENV['USER'] invalid value (must be one of ["foobarbuz"])}) do | |
EnvVarsChecker.new do | |
env_includes 'USER', in: (-> { ["foobarbuz"] }) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment