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
19 changes: 7 additions & 12 deletions tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from pip._internal.models.target_python import TargetPython
from pip._internal.network.session import PipSession

from tests.lib.filesystem import create_file
from tests.lib.venv import VirtualEnvironment
from tests.lib.wheel import make_wheel

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


def create_file(path: str, contents: str | None = None) -> None:
"""Create a file on the path, with the given contents"""
from pip._internal.utils.misc import ensure_dir

ensure_dir(os.path.dirname(path))
with open(path, "w") as f:
if contents is not None:
f.write(contents)
else:
f.write("\n")


def make_test_search_scope(
find_links: list[str] | None = None,
index_urls: list[str] | None = None,
Expand Down Expand Up @@ -759,6 +748,12 @@ def assert_installed_editable(self, dist_name: str) -> None:
and x.get("editable_project_location")
)

def temporary_file(self, filename: str, contents: str) -> pathlib.Path:
"""Create a temporary file with the given filename and contents."""
path = self.scratch_path.joinpath(filename)
create_file(path, contents)
return path


# FIXME ScriptTest does something similar, but only within a single
# ProcResult; this generalizes it so states can be compared across
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ def chmod(path: str | Path, mode: int) -> Iterator[None]:
yield
finally:
os.chmod(path, old_mode)


def create_file(path: str | Path, contents: str | None = None) -> None:
"""Create a file on the path, with the given contents"""
_path = Path(path)
_path.parent.mkdir(exist_ok=True, parents=True)
with _path.open("w") as f:
if contents is not None:
f.write(contents)
else:
f.write("\n")
2 changes: 1 addition & 1 deletion tests/unit/test_req_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
uninstallation_paths,
)

from tests.lib import create_file
from tests.lib.filesystem import create_file


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