Skip to content

Commit 10089f6

Browse files
fix(core): make sure context manager exits (#876)
maybe don't write your software in python
1 parent aa47435 commit 10089f6

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

.github/workflows/ci-core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Install Python dependencies
2525
run: poetry install --all-extras
2626
- name: Run twine check
27-
run: poetry build && poetry run twine check dist/*.tar.gz
27+
run: rm -f LICENSE.txt && poetry build && poetry run twine check dist/*.tar.gz
2828
- name: Set up Docker
2929
uses: docker/setup-docker-action@v4
3030
- name: Run tests

core/testcontainers/compose/compose.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from dataclasses import asdict, dataclass, field, fields, is_dataclass
23
from functools import cached_property
34
from json import loads
@@ -223,8 +224,12 @@ def __post_init__(self) -> None:
223224
self.env_file = [self.env_file]
224225

225226
def __enter__(self) -> "DockerCompose":
226-
self.start()
227-
return self
227+
try:
228+
self.start()
229+
return self
230+
except: # noqa: E722, RUF100
231+
self.__exit__(*sys.exc_info())
232+
raise
228233

229234
def __exit__(
230235
self, exc_type: Optional[type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]

core/testcontainers/core/container.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import sys
23
from os import PathLike
34
from socket import socket
45
from types import TracebackType
@@ -215,7 +216,11 @@ def stop(self, force: bool = True, delete_volume: bool = True) -> None:
215216
self.get_docker_client().client.close()
216217

217218
def __enter__(self) -> Self:
218-
return self.start()
219+
try:
220+
return self.start()
221+
except: # noqa: E722, RUF100
222+
self.__exit__(*sys.exc_info())
223+
raise
219224

220225
def __exit__(
221226
self, exc_type: Optional[type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ maintainers = [
1010
]
1111
readme = "README.md"
1212
keywords = ["testing", "logging", "docker", "test automation"]
13+
license = "Apache-2.0"
1314
classifiers = [
1415
"License :: OSI Approved :: Apache Software License",
1516
"Intended Audience :: Information Technology",

0 commit comments

Comments
 (0)