Skip to content

Commit

Permalink
Allow undefined environment variables in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 21, 2018
1 parent b0879e6 commit bbe5d9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion astrality/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,15 @@ def insert_environment_values(
env_variable_pattern = re.compile(r'\$\{(\w+)\}')

def expand_environment_variable(match: Match[str]) -> str:
return env_dict[match.groups()[0]]
env_variable = match.groups()[0]
try:
return env_dict[env_variable]
except KeyError:
logging.error(
f'Could not insert environment variable {env_variable}. '
'It is not defined. Leaving it as is in the configuration.'
)
return '${' + env_variable + '}'

return env_variable_pattern.sub(
expand_environment_variable,
Expand Down
8 changes: 8 additions & 0 deletions astrality/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ def test_insert_environment_variables():
expected = 'value2'
assert insert_environment_values(upper_case_conficting_key) == expected

def test_inserting_environment_variable_that_does_not_exist(caplog):
config_line= 'key=value-${DOES_NOT_EXIST}'
assert insert_environment_values(config_line) == config_line

assert caplog.record_tuples[0][2] == \
'Could not insert environment variable DOES_NOT_EXIST. ' \
'It is not defined. Leaving it as is in the configuration.'

def test_insert_context_section():
context = compiler.context({'context/section1': {'key_one': 'value_one'}})
assert context['section1']['key_one'] == 'value_one'
Expand Down

0 comments on commit bbe5d9e

Please sign in to comment.