From 2aa042b9c745b5ecf9a1306544aca8b5b15b41ba Mon Sep 17 00:00:00 2001 From: indra Date: Thu, 16 Jan 2025 10:36:34 +1100 Subject: [PATCH] expand service environment_variables before adding to subs_dict Also modifies an existing integration test to expect an empty string as `docker-compose` warns that `ZZVAR3` is not set and defaults it to an empty string per the acutal output here. ```yaml $ docker-compose -f container-compose.load-.env-in-project.yaml config WARN[0000] The "ZZVAR3" variable is not set. Defaulting to a blank string. name: project services: app: command: - /bin/busybox - sh - -c - env | grep ZZ environment: ZZVAR1: This value is loaded but should be overwritten ZZVAR2: This value is loaded from .env in project/ directory ZZVAR3: "" ... ``` Signed-off-by: indra --- podman_compose.py | 1 - tests/integration/test_podman_compose_env_file.py | 2 +- tests/unit/test_rec_subs.py | 10 ++++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index e569d23e..d57bb08c 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -270,7 +270,6 @@ def rec_subs(value, subs_dict): svc_envs = {k: v for k, v in value['environment'].items() if k not in subs_dict} # we need to add `svc_envs` to the `subs_dict` so that it can evaluate the # service environment that reference to another service environment. - subs_dict.update(svc_envs) svc_envs = rec_subs(svc_envs, subs_dict) subs_dict.update(svc_envs) diff --git a/tests/integration/test_podman_compose_env_file.py b/tests/integration/test_podman_compose_env_file.py index e9f63415..5e0dbac8 100644 --- a/tests/integration/test_podman_compose_env_file.py +++ b/tests/integration/test_podman_compose_env_file.py @@ -233,7 +233,7 @@ def test_taking_env_variables_from_env_files_from_different_directories(self): [ 'ZZVAR1=This value is loaded but should be overwritten\r', 'ZZVAR2=This value is loaded from .env in project/ directory\r', - 'ZZVAR3=$ZZVAR3\r', + 'ZZVAR3=\r', '', ], ) diff --git a/tests/unit/test_rec_subs.py b/tests/unit/test_rec_subs.py index 4e83e17b..8d0401ab 100644 --- a/tests/unit/test_rec_subs.py +++ b/tests/unit/test_rec_subs.py @@ -26,6 +26,16 @@ class TestRecSubs(unittest.TestCase): {"environment": {"non_var": "$$v1", "vx": "$non_var"}, "image": "abc:$non_var"}, {"environment": {"non_var": "$v1", "vx": "$v1"}, "image": "abc:$v1"}, ), + ( + "service environment with unpopulated ${VARIABLE:-default} format", + {"environment": {"v100": "${v100:-low priority}", "actual-v100": "$v100"}}, + {"environment": {"v100": "low priority", "actual-v100": "low priority"}}, + ), + ( + "service environment with populated ${VARIABLE:-default} format", + {"environment": {"v1": "${v1:-low priority}", "actual-v1": "$v1"}}, + {"environment": {"v1": "high priority", "actual-v1": "high priority"}}, + ), # list ( "Values in list are substituted",