2022-06-16 18:52:54 +02:00
|
|
|
import pytest
|
|
|
|
|
2022-06-16 20:35:24 +02:00
|
|
|
from borgmatic.config import environment as module
|
2022-06-16 18:52:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_env(monkeypatch):
|
2022-06-16 20:35:24 +02:00
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE', 'foo')
|
2022-06-16 18:52:54 +02:00
|
|
|
config = {'key': 'Hello $MY_CUSTOM_VALUE'}
|
|
|
|
module.resolve_env_variables(config)
|
|
|
|
assert config == {'key': 'Hello $MY_CUSTOM_VALUE'}
|
|
|
|
|
|
|
|
|
|
|
|
def test_env_braces(monkeypatch):
|
2022-06-16 20:35:24 +02:00
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE', 'foo')
|
2023-03-24 07:11:14 +01:00
|
|
|
config = {'key': 'Hello ${MY_CUSTOM_VALUE}'} # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
module.resolve_env_variables(config)
|
|
|
|
assert config == {'key': 'Hello foo'}
|
|
|
|
|
2022-06-23 19:41:04 +02:00
|
|
|
|
2022-06-17 09:50:56 +02:00
|
|
|
def test_env_multi(monkeypatch):
|
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE', 'foo')
|
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE2', 'bar')
|
2023-03-24 07:11:14 +01:00
|
|
|
config = {'key': 'Hello ${MY_CUSTOM_VALUE}${MY_CUSTOM_VALUE2}'} # noqa: FS003
|
2022-06-17 09:50:56 +02:00
|
|
|
module.resolve_env_variables(config)
|
|
|
|
assert config == {'key': 'Hello foobar'}
|
|
|
|
|
2022-06-23 19:41:04 +02:00
|
|
|
|
2022-06-17 09:50:56 +02:00
|
|
|
def test_env_escape(monkeypatch):
|
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE', 'foo')
|
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE2', 'bar')
|
2023-03-24 07:11:14 +01:00
|
|
|
config = {'key': r'Hello ${MY_CUSTOM_VALUE} \${MY_CUSTOM_VALUE}'} # noqa: FS003
|
2022-06-17 09:50:56 +02:00
|
|
|
module.resolve_env_variables(config)
|
2023-03-24 07:11:14 +01:00
|
|
|
assert config == {'key': r'Hello foo ${MY_CUSTOM_VALUE}'} # noqa: FS003
|
2022-06-17 09:50:56 +02:00
|
|
|
|
2022-06-16 18:52:54 +02:00
|
|
|
|
|
|
|
def test_env_default_value(monkeypatch):
|
2022-06-16 20:35:24 +02:00
|
|
|
monkeypatch.delenv('MY_CUSTOM_VALUE', raising=False)
|
2023-03-24 07:11:14 +01:00
|
|
|
config = {'key': 'Hello ${MY_CUSTOM_VALUE:-bar}'} # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
module.resolve_env_variables(config)
|
|
|
|
assert config == {'key': 'Hello bar'}
|
|
|
|
|
|
|
|
|
|
|
|
def test_env_unknown(monkeypatch):
|
2022-06-16 20:35:24 +02:00
|
|
|
monkeypatch.delenv('MY_CUSTOM_VALUE', raising=False)
|
2023-03-24 07:11:14 +01:00
|
|
|
config = {'key': 'Hello ${MY_CUSTOM_VALUE}'} # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.resolve_env_variables(config)
|
|
|
|
|
|
|
|
|
|
|
|
def test_env_full(monkeypatch):
|
2022-06-16 20:35:24 +02:00
|
|
|
monkeypatch.setenv('MY_CUSTOM_VALUE', 'foo')
|
|
|
|
monkeypatch.delenv('MY_CUSTOM_VALUE2', raising=False)
|
2022-06-16 18:52:54 +02:00
|
|
|
config = {
|
|
|
|
'key': 'Hello $MY_CUSTOM_VALUE is not resolved',
|
|
|
|
'dict': {
|
|
|
|
'key': 'value',
|
|
|
|
'anotherdict': {
|
2023-03-24 07:11:14 +01:00
|
|
|
'key': 'My ${MY_CUSTOM_VALUE} here', # noqa: FS003
|
|
|
|
'other': '${MY_CUSTOM_VALUE}', # noqa: FS003
|
|
|
|
'escaped': r'\${MY_CUSTOM_VALUE}', # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
'list': [
|
2023-03-24 07:11:14 +01:00
|
|
|
'/home/${MY_CUSTOM_VALUE}/.local', # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
'/var/log/',
|
2023-03-24 07:11:14 +01:00
|
|
|
'/home/${MY_CUSTOM_VALUE2:-bar}/.config', # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'list': [
|
2023-03-24 07:11:14 +01:00
|
|
|
'/home/${MY_CUSTOM_VALUE}/.local', # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
'/var/log/',
|
2023-03-24 07:11:14 +01:00
|
|
|
'/home/${MY_CUSTOM_VALUE2-bar}/.config', # noqa: FS003
|
2022-06-16 18:52:54 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
module.resolve_env_variables(config)
|
|
|
|
assert config == {
|
|
|
|
'key': 'Hello $MY_CUSTOM_VALUE is not resolved',
|
|
|
|
'dict': {
|
|
|
|
'key': 'value',
|
|
|
|
'anotherdict': {
|
|
|
|
'key': 'My foo here',
|
|
|
|
'other': 'foo',
|
2023-03-24 07:11:14 +01:00
|
|
|
'escaped': '${MY_CUSTOM_VALUE}', # noqa: FS003
|
2022-06-16 20:35:24 +02:00
|
|
|
'list': ['/home/foo/.local', '/var/log/', '/home/bar/.config'],
|
2022-06-16 18:52:54 +02:00
|
|
|
},
|
|
|
|
},
|
2022-06-16 20:35:24 +02:00
|
|
|
'list': ['/home/foo/.local', '/var/log/', '/home/bar/.config'],
|
2022-06-16 18:52:54 +02:00
|
|
|
}
|