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
9c5347bd23
commit
eebc63c216
|
@ -71,7 +71,7 @@ def recursive_interpolate(obj, interpolator):
|
|||
|
||||
|
||||
class TemplateWithDefaults(Template):
|
||||
idpattern = r'[_a-z][_a-z0-9]*(?::?-[^}]+)?'
|
||||
idpattern = r'[_a-z][_a-z0-9]*(?::?-[^}]*)?'
|
||||
|
||||
# Modified from python2.7/string.py
|
||||
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:-}"
|
|
@ -2875,6 +2875,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)
|
||||
def test_unset_variable_produces_warning(self):
|
||||
os.environ.pop('FOO', None)
|
||||
|
|
Loading…
Reference in New Issue