From eebc63c216692bd681e2200b6ad30d5aa71fa9d2 Mon Sep 17 00:00:00 2001 From: Guillermo Arribas Date: Thu, 19 Oct 2017 22:19:05 -0300 Subject: [PATCH] Allow empty default values in variable interpolation (fixes #5185) Signed-off-by: Guillermo Arribas --- compose/config/interpolation.py | 2 +- .../docker-compose.yml | 13 +++++++++++ tests/unit/config/config_test.py | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/environment-interpolation-with-defaults/docker-compose.yml diff --git a/compose/config/interpolation.py b/compose/config/interpolation.py index b13ac591a..df9c988e7 100644 --- a/compose/config/interpolation.py +++ b/compose/config/interpolation.py @@ -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): diff --git a/tests/fixtures/environment-interpolation-with-defaults/docker-compose.yml b/tests/fixtures/environment-interpolation-with-defaults/docker-compose.yml new file mode 100644 index 000000000..42e7cbb6a --- /dev/null +++ b/tests/fixtures/environment-interpolation-with-defaults/docker-compose.yml @@ -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:-}" diff --git a/tests/unit/config/config_test.py b/tests/unit/config/config_test.py index 8f2266ed8..7a7cfacdd 100644 --- a/tests/unit/config/config_test.py +++ b/tests/unit/config/config_test.py @@ -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)