mirror of https://github.com/docker/compose.git
Allow empty default values in variable interpolation (fixes #5185)
Signed-off-by: Guillermo Arribas <garribas@gmail.com>
This commit is contained in:
parent
ee6a293ae0
commit
8cd46cd54d
|
@ -71,7 +71,7 @@ def recursive_interpolate(obj, interpolator):
|
||||||
|
|
||||||
|
|
||||||
class TemplateWithDefaults(Template):
|
class TemplateWithDefaults(Template):
|
||||||
idpattern = r'[_a-z][_a-z0-9]*(?::?-[^}]+)?'
|
idpattern = r'[_a-z][_a-z0-9]*(?::?-[^}]*)?'
|
||||||
|
|
||||||
# Modified from python2.7/string.py
|
# Modified from python2.7/string.py
|
||||||
def substitute(self, mapping):
|
def substitute(self, mapping):
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
version: "2.1"
|
||||||
|
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
# set value with default, default must be ignored
|
||||||
|
image: ${IMAGE:-alpine}
|
||||||
|
|
||||||
|
# unset value with default value
|
||||||
|
ports:
|
||||||
|
- "${HOST_PORT:-80}:8000"
|
||||||
|
|
||||||
|
# unset value with empty default
|
||||||
|
hostname: "host-${UNSET_VALUE:-}"
|
|
@ -2894,6 +2894,28 @@ class InterpolationTest(unittest.TestCase):
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ)
|
||||||
|
def test_config_file_with_environment_variable_with_defaults(self):
|
||||||
|
project_dir = 'tests/fixtures/environment-interpolation-with-defaults'
|
||||||
|
os.environ.update(
|
||||||
|
IMAGE="busybox",
|
||||||
|
)
|
||||||
|
|
||||||
|
service_dicts = config.load(
|
||||||
|
config.find(
|
||||||
|
project_dir, None, Environment.from_env_file(project_dir)
|
||||||
|
)
|
||||||
|
).services
|
||||||
|
|
||||||
|
self.assertEqual(service_dicts, [
|
||||||
|
{
|
||||||
|
'name': 'web',
|
||||||
|
'image': 'busybox',
|
||||||
|
'ports': types.ServicePort.parse('80:8000'),
|
||||||
|
'hostname': 'host-',
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
@mock.patch.dict(os.environ)
|
@mock.patch.dict(os.environ)
|
||||||
def test_unset_variable_produces_warning(self):
|
def test_unset_variable_produces_warning(self):
|
||||||
os.environ.pop('FOO', None)
|
os.environ.pop('FOO', None)
|
||||||
|
|
Loading…
Reference in New Issue