Coverage for cogapp/makefiles.py: 11.11%
22 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-06 06:21 -0500
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-06 06:21 -0500
1"""Dictionary-to-filetree functions, to create test files for testing."""
3import os.path
5from .whiteutils import reindent_block
8def make_files(d, basedir="."):
9 """Create files from the dictionary `d` in the directory named by `basedir`."""
10 for name, contents in d.items():
11 child = os.path.join(basedir, name)
12 if isinstance(contents, (bytes, str)):
13 mode = "w"
14 if isinstance(contents, bytes):
15 mode += "b"
16 with open(child, mode) as f:
17 f.write(reindent_block(contents))
18 else:
19 if not os.path.exists(child):
20 os.mkdir(child)
21 make_files(contents, child)
24def remove_files(d, basedir="."):
25 """Remove the files created by `makeFiles`.
27 Directories are removed if they are empty.
29 """
30 for name, contents in d.items():
31 child = os.path.join(basedir, name)
32 if isinstance(contents, (bytes, str)):
33 os.remove(child)
34 else:
35 remove_files(contents, child)
36 if not os.listdir(child):
37 os.rmdir(child)