Skip to content

Commit

Permalink
pytester: Testdir.makefiles: support sequences (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored Nov 2, 2020
1 parent d407c80 commit a99b678
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/_pytest/pytester/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def maketxtfile(self, *args, **kwargs):

def makefiles(
self,
files: Mapping[str, str],
files: "Union[Mapping[str, str], Sequence[Tuple[str, str]]]",
*,
base_path: Optional[str] = None,
dedent: bool = True,
Expand All @@ -750,8 +750,9 @@ def makefiles(
This is a more straight-forward API than the other helpers (e.g.
:func:`makepyfile`).
:param Mapping[str,str] files:
Mapping of filenames to file contents.
:param Mapping[str,str]|Sequence[Tuple[str,str]] files:
Mapping of filenames to file contents, or a sequence with
filename/contents pairs.
Absolute paths are handled, but have to be inside of :attr:`tmpdir`.
:param base_path:
Expand All @@ -774,18 +775,24 @@ def makefiles(
base_path = os.path.abspath(base_path)
else:
base_path = os.getcwd()
validated_files = []
for k, v in files.items():
validated_files = [] # type: List[Tuple[Path, str]]
if isinstance(files, Mapping):
items = list(files.items()) # type: Sequence[Tuple[str, str]]
else:
items = files
for k, v in reversed(items):
abspath = Path(os.path.normpath(os.path.join(base_path, k)))
assert abspath.is_absolute(), abspath
if clobber:
if not (abspath.is_file() or abspath.is_symlink()):
if abspath.exists() and not (abspath.is_file() or abspath.is_symlink()):
raise ValueError(
"path is not a file/symlink, not clobbering: {!r}".format(
str(abspath)
)
)
else:
if any(x[0] == abspath for x in validated_files):
raise ValueError("duplicate file path: {!r}".format(str(abspath)))
if abspath.exists():
raise ValueError("path exists already: {!r}".format(str(abspath)))
if abspath.is_symlink():
Expand All @@ -799,7 +806,7 @@ def makefiles(
validated_files.append((abspath, v))

paths = []
for fpath, content in validated_files:
for fpath, content in reversed(validated_files):
path = tmpdir_path.joinpath(fpath)
if not path.parent.is_dir():
path.parent.mkdir(parents=True)
Expand All @@ -810,7 +817,7 @@ def makefiles(
if clobber:
if path.is_symlink():
path.unlink()
else:
elif path.exists():
assert path.is_file(), path
else:
assert not path.exists(), path
Expand Down
18 changes: 18 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,3 +1415,21 @@ def test_makefiles_dir(self, testdir: Testdir) -> None:
assert str(
excinfo.value
) == "path is not a file/symlink, not clobbering: {!r}".format(str(directory))

def test_makefiles_sequence(self, testdir: "Testdir") -> None:
files = testdir.makefiles((("1", "foo"), ("2", "bar")))
assert tuple(x.name for x in files) == ("1", "2")
assert tuple(x.read_text() for x in files) == ("foo", "bar")

def test_makefiles_sequence_duplicate(self, testdir: "Testdir") -> None:
with pytest.raises(ValueError) as excinfo:
testdir.makefiles((("1", "foo"), ("2", "bar"), ("1", "foo2")))
assert str(excinfo.value) == "duplicate file path: {!r}".format(
str(testdir.tmpdir.join("1"))
)

files = testdir.makefiles(
(("1", "foo"), ("2", "bar"), ("1", "foo2")), clobber=True
)
assert tuple(x.name for x in files) == ("1", "2", "1")
assert tuple(x.read_text() for x in files) == ("foo2", "bar", "foo2")

0 comments on commit a99b678

Please sign in to comment.