Skip to content

Commit 3157524

Browse files
authored
test: Move create_file() helper and add script.temporary_file() (#13712)
This will be useful for requirements/constraints files.
1 parent 090e226 commit 3157524

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

tests/lib/__init__.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pip._internal.models.target_python import TargetPython
3636
from pip._internal.network.session import PipSession
3737

38+
from tests.lib.filesystem import create_file
3839
from tests.lib.venv import VirtualEnvironment
3940
from tests.lib.wheel import make_wheel
4041

@@ -51,18 +52,6 @@
5152
_FilesState = dict[str, Union[FoundDir, FoundFile]]
5253

5354

54-
def create_file(path: str, contents: str | None = None) -> None:
55-
"""Create a file on the path, with the given contents"""
56-
from pip._internal.utils.misc import ensure_dir
57-
58-
ensure_dir(os.path.dirname(path))
59-
with open(path, "w") as f:
60-
if contents is not None:
61-
f.write(contents)
62-
else:
63-
f.write("\n")
64-
65-
6655
def make_test_search_scope(
6756
find_links: list[str] | None = None,
6857
index_urls: list[str] | None = None,
@@ -769,6 +758,12 @@ def assert_installed_editable(self, dist_name: str) -> None:
769758
and x.get("editable_project_location")
770759
)
771760

761+
def temporary_file(self, filename: str, contents: str) -> pathlib.Path:
762+
"""Create a temporary file with the given filename and contents."""
763+
path = self.scratch_path.joinpath(filename)
764+
create_file(path, contents)
765+
return path
766+
772767

773768
# FIXME ScriptTest does something similar, but only within a single
774769
# ProcResult; this generalizes it so states can be compared across

tests/lib/filesystem.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@ def chmod(path: str | Path, mode: int) -> Iterator[None]:
3131
yield
3232
finally:
3333
os.chmod(path, old_mode)
34+
35+
36+
def create_file(path: str | Path, contents: str | None = None) -> None:
37+
"""Create a file on the path, with the given contents"""
38+
_path = Path(path)
39+
_path.parent.mkdir(exist_ok=True, parents=True)
40+
with _path.open("w") as f:
41+
if contents is not None:
42+
f.write(contents)
43+
else:
44+
f.write("\n")

tests/unit/test_req_uninstall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
uninstallation_paths,
2020
)
2121

22-
from tests.lib import create_file
22+
from tests.lib.filesystem import create_file
2323

2424

2525
# Pretend all files are local, so UninstallPathSet accepts files in the tmpdir,

0 commit comments

Comments
 (0)