Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_should_raise_bad_configuration_error(self):
with pytest.raises(BadConfigurationError) as excinfo:
evaluate_env_var("${BASE_URL}")
assert (
str(excinfo.value)
== "'BASE_URL' environment variable not set or badly configured"
)
def test_should_raise_bad_configuration_error(self):
with pytest.raises(BadConfigurationError) as excinfo:
StringEvaluator._evaluate_env_var("${BASE_URL}")
assert (
str(excinfo.value)
== "'BASE_URL' environment variable not set or badly configured"
)
def evaluate(cls, sequence, vars):
try:
sequence = cls._evaluate_env_var(sequence)
except BadConfigurationError as e:
logger.error(e)
sys.exit()
sequence = cls._evaluate_custom_var(sequence, vars)
try:
return CodeEvaluator.evaluate(sequence, vars)
except InvalidPythonCodeError as e:
logger.error(e)
sys.exit()
def evaluate(self, sequence):
try:
return self.evaluate_env_var(sequence)
except BadConfigurationError as e:
logger.error(e)
sys.exit()
def __init__(self, env_var, *args):
super(BadConfigurationError, self).__init__(
f"{env_var} environment variable not set or badly configured", *args
)
def _evaluate_env_var(cls, sequence):
matches = cls.variable_pattern.finditer(sequence)
if not matches:
return sequence
for match in matches:
variable_name = match.group("variable")
if any(letter.islower() for letter in variable_name):
continue
try:
variable_value = os.environ[variable_name]
except KeyError as e:
raise BadConfigurationError(e)
sequence = cls.replace_var_with_value(
sequence, match.group(), variable_value
)
return sequence
def evaluate_env_var(self, sequence):
matches = self.variable_pattern.finditer(sequence)
if not matches:
return sequence
for match in matches:
variable_name = match.group("variable")
if any(letter.islower() for letter in variable_name):
continue
try:
variable_value = os.environ[variable_name]
except KeyError as e:
raise BadConfigurationError(e)
sequence = self.replace_var_with_value(
sequence, match.group(), variable_value
)
return sequence
def _evaluate_env_var(cls, sequence):
matches = cls.variable_pattern.finditer(sequence)
if not matches:
return sequence
for match in matches:
variable_name = match.group("variable")
if any(letter.islower() for letter in variable_name):
continue
try:
variable_value = os.environ[variable_name]
except KeyError as e:
raise BadConfigurationError(e)
sequence = cls.replace_var_with_value(
sequence, match.group(), variable_value
)
return sequence