Skip to content

Commit 5ff3f3b

Browse files
committed
Re-use _path.build for building files.
1 parent 3112653 commit 5ff3f3b

File tree

2 files changed

+9
-30
lines changed

2 files changed

+9
-30
lines changed

tests/_path.py

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def _(content: str, path):
7878
path.write_text(content, encoding='utf-8')
7979

8080

81+
@create.register
82+
def _(content: str, path):
83+
path.write_text(content, encoding='utf-8')
84+
85+
8186
class Recording:
8287
"""
8388
A TreeMaker object that records everything that would be written.

tests/fixtures.py

+4-30
Original file line numberDiff line numberDiff line change
@@ -328,38 +328,12 @@ def setUp(self):
328328
build_files(EggInfoFile.files, prefix=self.site_dir)
329329

330330

331-
def build_files(file_defs, prefix=pathlib.Path()):
332-
"""Build a set of files/directories, as described by the
331+
# dedent all text strings before writing
332+
orig = _path.create.registry[str]
333+
_path.create.register(str, lambda content, path: orig(DALS(content), path))
333334

334-
file_defs dictionary. Each key/value pair in the dictionary is
335-
interpreted as a filename/contents pair. If the contents value is a
336-
dictionary, a directory is created, and the dictionary interpreted
337-
as the files within it, recursively.
338335

339-
For example:
340-
341-
{"README.txt": "A README file",
342-
"foo": {
343-
"__init__.py": "",
344-
"bar": {
345-
"__init__.py": "",
346-
},
347-
"baz.py": "# Some code",
348-
}
349-
}
350-
"""
351-
for name, contents in file_defs.items():
352-
full_name = prefix / name
353-
if isinstance(contents, dict):
354-
full_name.mkdir()
355-
build_files(contents, prefix=full_name)
356-
else:
357-
if isinstance(contents, bytes):
358-
with full_name.open('wb') as f:
359-
f.write(contents)
360-
else:
361-
with full_name.open('w', encoding='utf-8') as f:
362-
f.write(DALS(contents))
336+
build_files = _path.build
363337

364338

365339
def build_record(file_defs):

0 commit comments

Comments
 (0)