Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for multiple env-files #614

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion python_on_whales/client_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions python_on_whales/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 52 additions & 1 deletion tests/python_on_whales/test_client_config.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Loading