| 757 | self._monkeypatch.chdir(self.path) |
| 758 | |
| 759 | def _makefile( |
| 760 | self, |
| 761 | ext: str, |
| 762 | lines: Sequence[Any | bytes], |
| 763 | files: Mapping[str, _FileContent], |
| 764 | encoding: str = "utf-8", |
| 765 | ) -> Path: |
| 766 | items = list(files.items()) |
| 767 | |
| 768 | if ext is None: |
| 769 | raise TypeError("ext must not be None") |
| 770 | |
| 771 | if ext and not ext.startswith("."): |
| 772 | raise ValueError( |
| 773 | f"pytester.makefile expects a file extension, try .{ext} instead of {ext}" |
| 774 | ) |
| 775 | |
| 776 | def to_text(s: Any | bytes) -> str: |
| 777 | return s.decode(encoding) if isinstance(s, bytes) else str(s) |
| 778 | |
| 779 | if lines: |
| 780 | source = "\n".join(to_text(x) for x in lines) |
| 781 | basename = self._name |
| 782 | items.insert(0, (basename, source)) |
| 783 | |
| 784 | ret = None |
| 785 | for basename, value in items: |
| 786 | p = self.path.joinpath(basename).with_suffix(ext) |
| 787 | p.parent.mkdir(parents=True, exist_ok=True) |
| 788 | source_ = Source(value) |
| 789 | source = "\n".join(to_text(line) for line in source_.lines) |
| 790 | p.write_text(source.strip(), encoding=encoding) |
| 791 | if ret is None: |
| 792 | ret = p |
| 793 | assert ret is not None |
| 794 | return ret |
| 795 | |
| 796 | def makefile(self, ext: str, *args: str, **kwargs: str) -> Path: |
| 797 | r"""Create new text file(s) in the test directory. |