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

Fix multiple interpolation #45

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v0.8 (unreleased)
====

Test with Python 3.9 and Python 3.10.
Fix interpolation of multiple variables in one setting value.


v0.7
Expand Down
2 changes: 1 addition & 1 deletion configstore/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_setting(self, key, default=_no_default, *, asbool=False):
return ret

def interpolate(self, string):
res = re.sub(r"\$\{([_a-zA-Z0-9]+)\}", self._replace, string, count=1)
res = re.sub(r"\$\{([_a-zA-Z0-9]+)\}", self._replace, string)
return res

def _replace(self, matchobj):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def test_store_get_setting_interpolate_value():
assert value == '42-staging-secrets!'


def test_store_get_setting_interpolate_missing():
store = Store([DictBackend({'service_host': 'cool-db-server:6000'})])

with pytest.raises(SettingNotFoundException):
store.get_setting('service_url')


def test_store_get_setting_interpolate_default():
store = Store([DictBackend({'service_host': 'cool-db-server:6000'})])

Expand All @@ -82,6 +89,21 @@ def test_store_get_setting_interpolate_default():
assert value == 'https://cool-db-server:6000/db'


def test_store_get_setting_interpolate_special_character_boundary():
store = Store([DictBackend(dict(
service_url='https://${user}:${dbpass}@${host}:${port}/${db}',
user='bob',
dbpass='secret',
host='cool-db-server',
port='6000',
db='db1',
))])

value = store.get_setting('service_url')

assert value == 'https://bob:secret@cool-db-server:6000/db1'


def test_store_add_backend():
store = Store([])

Expand Down
9 changes: 7 additions & 2 deletions tests/test_z_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ def test_dict_defaults(monkeypatch):

def test_interpolation(monkeypatch, tmp_path):
monkeypatch.setenv('ENVIRONMENT', 'STAGING')
monkeypatch.setenv('REDIS_URL', 'https://redis:4012')
monkeypatch.setenv('REDIS_HOST', 'redis')
monkeypatch.setenv('REDIS_PORT', '4012')
path = tmp_path / 'config.env'
path.write_text(DOTENV_CONTENTS)
path.write_text('APP_NAME=supertest-${ENVIRONMENT}-1')
path.write_text(
'APP_NAME=supertest-${ENVIRONMENT}-1\n'
# make sure more than one variable is possible
'REDIS_URL=https://${REDIS_HOST}:${REDIS_PORT}\n'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not good, this test did not fail 🧐

)

store = configstore.Store([
configstore.EnvVarBackend(),
Expand Down