diff --git a/python_on_whales/client_config.py b/python_on_whales/client_config.py index 6709f4ab..d6663e8a 100644 --- a/python_on_whales/client_config.py +++ b/python_on_whales/client_config.py @@ -74,6 +74,7 @@ class ClientConfig: compose_files: List[ValidPath] = field(default_factory=list) compose_profiles: List[str] = field(default_factory=list) compose_env_file: Optional[ValidPath] = None + compose_env_files: Iterable[ValidPath] = field(default_factory=list) compose_project_name: Optional[str] = None compose_project_directory: Optional[ValidPath] = None compose_compatibility: Optional[bool] = None @@ -159,7 +160,18 @@ def docker_compose_cmd(self) -> Command: base_cmd = self.docker_cmd + ["compose"] base_cmd.add_args_iterable_or_single("--file", self.compose_files) base_cmd.add_args_iterable_or_single("--profile", self.compose_profiles) - base_cmd.add_simple_arg("--env-file", self.compose_env_file) + if self.compose_env_files: + if self.compose_env_file: + warnings.warn( + "You can't set both `compose_env_file` and `compose_env_files`. Files used in `compose_env_files` will be used." + ) + base_cmd.add_args_iterable("--env-file", self.compose_env_files) + elif self.compose_env_file: + warnings.warn( + "`compose_env_file` is deprecated. Use `compose_env_files` instead." + ) + base_cmd.add_simple_arg("--env-file", self.compose_env_file) + base_cmd.add_simple_arg("--project-name", self.compose_project_name) base_cmd.add_simple_arg("--project-directory", self.compose_project_directory) base_cmd.add_flag("--compatibility", self.compose_compatibility) diff --git a/python_on_whales/docker_client.py b/python_on_whales/docker_client.py index 21dbe7b1..dfc2c3c9 100644 --- a/python_on_whales/docker_client.py +++ b/python_on_whales/docker_client.py @@ -180,6 +180,7 @@ def __init__( compose_files: List[ValidPath] = [], compose_profiles: List[str] = [], compose_env_file: Optional[ValidPath] = None, + compose_env_files: List[ValidPath] = [], compose_project_name: Optional[str] = None, compose_project_directory: Optional[ValidPath] = None, compose_compatibility: Optional[bool] = None, @@ -209,6 +210,7 @@ def __init__( compose_files=compose_files, compose_profiles=compose_profiles, compose_env_file=compose_env_file, + compose_env_files=compose_env_files, compose_project_name=compose_project_name, compose_project_directory=compose_project_directory, compose_compatibility=compose_compatibility, diff --git a/tests/python_on_whales/test_client_config.py b/tests/python_on_whales/test_client_config.py index 63128a1d..3b1e3651 100644 --- a/tests/python_on_whales/test_client_config.py +++ b/tests/python_on_whales/test_client_config.py @@ -1,10 +1,11 @@ import json from pathlib import Path +from typing import Sequence import pytest from python_on_whales import docker -from python_on_whales.client_config import ParsingError +from python_on_whales.client_config import ClientConfig, ParsingError fake_json_message = { "CreatedAt": "2020-10-08T18:32:55Z", @@ -36,3 +37,53 @@ def test_pretty_exception_message_and_report(mocker): raise IndexError assert Path(word).read_text() == json.dumps(fake_json_message, indent=2) + + +def test_compose_env_file(): + """Test that the deprecated `compose_env_file` gives a warning, and adds the `--env-file` argument to the compose command""" + with pytest.warns(UserWarning): + example_env_file_name = "example.env" + client_config = ClientConfig(compose_env_file=example_env_file_name) + assert client_config.docker_compose_cmd.count("--env-file") == 1 + # Since we are operating of a list of commands, we first find the index of the `--env-file` argument, then we check that the next argument is the file name. + index = client_config.docker_compose_cmd.index("--env-file") + assert client_config.docker_compose_cmd[index + 1] == example_env_file_name + + +@pytest.mark.parametrize( + "compose_env_files", + [ + ["example1.env", "example2.env"], + ["example1.env"], + ], + ids=["multiple_files", "single_file"], +) +def test_compose_env_files(compose_env_files: Sequence[str]): + """Test that using only `compose_env_files` adds all the files to the command line arguments""" + client_config = ClientConfig(compose_env_files=compose_env_files) + # Since we are operating of a list of commands, we first find the indices of the `--env-file` argument, then we check that the next argument is the file name. + indices = [ + index + for index, item in enumerate(client_config.docker_compose_cmd) + if item == "--env-file" + ] + assert len(indices) == len(compose_env_files) + for index, env_file in zip(indices, compose_env_files): + assert client_config.docker_compose_cmd[index + 1] == env_file + + +def test_compose_env_files_and_env_file(): + """Test that using both `compose_env_files` and `compose_env_file` gives a warning, and only uses `compose_env_files`""" + env_files_input = "example1.env" + env_file_input = "example2.env" + + with pytest.warns(UserWarning): + client_config = ClientConfig( + compose_env_files=[env_files_input], compose_env_file=env_file_input + ) + assert client_config.docker_compose_cmd.count("--env-file") == len( + [env_files_input] + ) + # Since we are operating of a list of commands, we first find the index of the `--env-file` argument, then we check that the next argument is the file name. + index = client_config.docker_compose_cmd.index("--env-file") + assert client_config.docker_compose_cmd[index + 1] == env_files_input