diff --git a/podman_compose.py b/podman_compose.py index a0ea0587..cd2f3031 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2302,15 +2302,16 @@ def _parse_compose_file(self) -> None: # env-file is relative to the CWD dotenv_dict = {} - if args.env_file: + if args.env_file is None or len(args.env_file) == 0: # Load .env from the Compose file's directory to preserve # behavior prior to 1.1.0 and to match with Docker Compose (v2). - if ".env" == args.env_file: - project_dotenv_file = os.path.realpath(os.path.join(dirname, ".env")) - if os.path.exists(project_dotenv_file): - dotenv_dict.update(dotenv_to_dict(project_dotenv_file)) - dotenv_path = os.path.realpath(args.env_file) - dotenv_dict.update(dotenv_to_dict(dotenv_path)) + project_dotenv_file = os.path.realpath(os.path.join(dirname, ".env")) + if os.path.exists(project_dotenv_file): + dotenv_dict.update(dotenv_to_dict(project_dotenv_file)) + else: + for env_file in args.env_file: + dotenv_path = os.path.realpath(env_file) + dotenv_dict.update(dotenv_to_dict(dotenv_path)) os.environ.update({ key: value # type: ignore[misc] @@ -2689,8 +2690,8 @@ def _init_global_parser(parser: argparse.ArgumentParser) -> None: "--env-file", help="Specify an alternate environment file", metavar="env_file", - type=str, - default=".env", + action="append", + default=[], ) parser.add_argument( "-f", diff --git a/tests/integration/env_file_tests/project/container-compose.yaml b/tests/integration/env_file_tests/project/container-compose.yaml index bd6a6bf3..1f48e9a3 100644 --- a/tests/integration/env_file_tests/project/container-compose.yaml +++ b/tests/integration/env_file_tests/project/container-compose.yaml @@ -7,3 +7,4 @@ services: - /tmp environment: ZZVAR1: $ZZVAR1 + ZZVAR3: $ZZVAR3 diff --git a/tests/integration/env_file_tests/test_podman_compose_env_file.py b/tests/integration/env_file_tests/test_podman_compose_env_file.py index 95038de9..542f2f0e 100644 --- a/tests/integration/env_file_tests/test_podman_compose_env_file.py +++ b/tests/integration/env_file_tests/test_podman_compose_env_file.py @@ -35,7 +35,40 @@ def test_path_env_file_inline(self) -> None: "--no-color", ]) # takes only value ZZVAR1 as container-compose.yaml file requires - self.assertEqual(output, b"ZZVAR1=podman-rocks-123\n") + self.assertEqual(output, b"ZZVAR1=podman-rocks-123\nZZVAR3=podman-rocks-125\n") + finally: + self.run_subprocess_assert_returncode([ + podman_compose_path(), + "-f", + path_compose_file, + "down", + ]) + + def test_path_env_file_inline_many(self) -> None: + # Test taking env variable value directly from env-file when its path is inline path + base_path = compose_base_path() + path_compose_file = os.path.join(base_path, "project/container-compose.yaml") + try: + self.run_subprocess_assert_returncode([ + podman_compose_path(), + "-f", + path_compose_file, + "--env-file", + os.path.join(base_path, "env-files/project-1.env"), + "--env-file", + os.path.join(base_path, "env-files/project-2.env"), + "up", + ]) + output, _ = self.run_subprocess_assert_returncode([ + podman_compose_path(), + "-f", + path_compose_file, + "logs", + "--no-log-prefix", + "--no-color", + ]) + # takes only value ZZVAR1 as container-compose.yaml file requires + self.assertEqual(output, b"ZZVAR1=podman-rocks-223\nZZVAR3=podman-rocks-125\n") finally: self.run_subprocess_assert_returncode([ podman_compose_path(), @@ -207,7 +240,7 @@ def test_var_value_inline_overrides_env_file_path_inline(self) -> None: "--no-color", ]) # takes only value ZZVAR1 as container-compose.yaml file requires - self.assertEqual(output, b"ZZVAR1=podman-rocks-321\n") + self.assertEqual(output, b"ZZVAR1=podman-rocks-321\nZZVAR3=podman-rocks-125\n") finally: self.run_subprocess_assert_returncode([ podman_compose_path(),