Skip to content

Commit e6ed27f

Browse files
committed
feat(main): Add support to working with env files
1 parent 925329d commit e6ed27f

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

core/testcontainers/core/container.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import contextlib
2+
from os import PathLike
23
from platform import system
34
from socket import socket
4-
from typing import TYPE_CHECKING, Optional
5+
from typing import TYPE_CHECKING, Optional, Union
56

67
import docker.errors
78
from docker import version
89
from docker.types import EndpointConfig
10+
from dotenv import dotenv_values
911
from typing_extensions import Self
1012

1113
from testcontainers.core.config import testcontainers_config as c
@@ -57,6 +59,12 @@ def with_env(self, key: str, value: str) -> Self:
5759
self.env[key] = value
5860
return self
5961

62+
def with_env_file(self, env_file: Union[str, PathLike]) -> Self:
63+
env_values = dotenv_values(env_file)
64+
for key, value in env_values.items():
65+
self.with_env(key, value)
66+
return self
67+
6068
def with_bind_ports(self, container: int, host: Optional[int] = None) -> Self:
6169
self.ports[container] = host
6270
return self

core/tests/test_core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,29 @@ def test_docker_image_with_custom_dockerfile_path(dockerfile_path: Optional[Path
9292
with DockerContainer(str(image)) as container:
9393
assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch"
9494
assert container.get_logs() == (("Hello world!\n").encode(), b""), "Container logs mismatch"
95+
96+
97+
def test_docker_container_with_env_file():
98+
"""Test that environment variables can be loaded from a file"""
99+
with tempfile.TemporaryDirectory() as temp_directory:
100+
env_file_path = Path(temp_directory) / "env_file"
101+
with open(env_file_path, "w") as f:
102+
f.write(
103+
"""
104+
TEST_ENV_VAR=hello
105+
NUMBER=123
106+
DOMAIN=example.org
107+
ADMIN_EMAIL=admin@${DOMAIN}
108+
ROOT_URL=${DOMAIN}/app
109+
"""
110+
)
111+
container = DockerContainer("alpine").with_command("tail -f /dev/null") # Keep the container running
112+
container.with_env_file(env_file_path) # Load the environment variables from the file
113+
with container:
114+
output = container.exec("env").output.decode("utf-8").strip()
115+
assert "TEST_ENV_VAR=hello" in output
116+
assert "NUMBER=123" in output
117+
assert "DOMAIN=example.org" in output
118+
assert "[email protected]" in output
119+
assert "ROOT_URL=example.org/app" in output
120+
print(output)

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ docker = "*" # ">=4.0"
8383
urllib3 = "*" # "<2.0"
8484
wrapt = "*" # "^1.16.0"
8585
typing-extensions = "*"
86+
python-dotenv = "*"
8687

8788
# community modules
8889
python-arango = { version = "^7.8", optional = true }

0 commit comments

Comments
 (0)