8000 fix(core): make sure context manager exits by alexanderankin · Pull Request #876 · testcontainers/testcontainers-python · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install Python dependencies
run: poetry install -- 8000 all-extras
- name: Run twine check
run: poetry build && poetry run twine check dist/*.tar.gz
run: rm -f LICENSE.txt && poetry build && poetry run twine check dist/*.tar.gz
- name: Set up Docker
uses: docker/setup-docker-action@v4
- name: Run tests
Expand Down
9 changes: 7 additions & 2 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from dataclasses import asdict, dataclass, field, fields, is_dataclass
from functools import cached_property
from json import loads
Expand Down Expand Up @@ -223,8 +224,12 @@ def __post_init__(self) -> None:
self.env_file = [self.env_file]

def __enter__(self) -> "DockerCompose":
self.start()
return self
try:
self.start()
return self
except: # noqa: E722, RUF100
self.__exit__(*sys.exc_info())
raise

def __exit__(
self, exc_type: Optional[type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
Expand Down
7 changes: 6 additions & 1 deletion core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import sys
from os import PathLike
from socket import socket
from types import TracebackType
Expand Down Expand Up @@ -215,7 +216,11 @@ def stop(self, force: bool = True, delete_volume: bool = True) -> None:
self.get_docker_client().client.close()

def __enter__(self) -> Self:
return self.start()
try:
return self.start()
except: # noqa: E722, RUF100
self.__exit__(*sys.exc_info())
raise

def __exit__(
self, exc_type: Optional[type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ maintainers = [
]
readme = "README.md"
keywords = ["testing", "logging", "docker", "test automation"]
license = "Apache-2.0"
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Intended Audience :: Information Technology",
Expand Down
0