Skip to content

Commit 40ce897

Browse files
committed
Extract assured context manager.
For re-use by coherent.test to save ruff config. Ref coherent-oss/coherent.test#14
1 parent b69f1d1 commit 40ce897

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

bootstrap.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import contextlib
22
import pathlib
3+
from typing import Callable, ContextManager
34

45
from .compat.py311 import importlib
56

67

8+
def write_pyproject(target: pathlib.Path = pathlib.Path()) -> ContextManager[None]:
9+
return assured(
10+
target / 'pyproject.toml',
11+
importlib.resources.files().joinpath('system.toml').read_text,
12+
)
13+
14+
715
@contextlib.contextmanager
8-
def write_pyproject(target: pathlib.Path = pathlib.Path()):
9-
path = target / 'pyproject.toml'
10-
if path.exists():
16+
def assured(
17+
target: pathlib.Path, content_factory: Callable[[], str]
18+
) -> ContextManager[None]:
19+
"""
20+
Yield with target existing on the file system.
21+
22+
If target does not already exist, it is created with the contents
23+
as supplied by ``content_factory()`` and deleted on exit.
24+
"""
25+
if target.exists():
1126
yield
1227
return
13-
path.write_text(importlib.resources.files().joinpath('system.toml').read_text())
28+
target.write_text(content_factory())
1429
try:
1530
yield
1631
finally:
17-
path.unlink()
32+
target.unlink()

0 commit comments

Comments
 (0)