Skip to content

Commit ec965af

Browse files
committed
-
1 parent 0e342a6 commit ec965af

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

source_py3/python_toolbox/temp_file_tools.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@context_management.ContextManagerType
18-
def create_temp_folder(suffix='', prefix=tempfile.template):
18+
def create_temp_folder(*, prefix=tempfile.template, suffix='', dir=None):
1919
'''
2020
Context manager that creates a temporary folder and deletes it after usage.
2121
@@ -35,9 +35,12 @@ def create_temp_folder(suffix='', prefix=tempfile.template):
3535
# The suite is finished, now it's all cleaned:
3636
assert not temp_folder.exists()
3737
38-
Use the `suffix` and `prefix` string arguments to dictate a suffix and/or a
39-
prefix to the temporary folder's name in the filesystem.
38+
Use the `prefix` and `suffix` string arguments to dictate a prefix and/or a
39+
suffix to the temporary folder's name in the filesystem.
4040
'''
41-
temp_folder = pathlib.Path(tempfile.mkdtemp(suffix=suffix, prefix=prefix))
42-
yield temp_folder
43-
shutil.rmtree(str(temp_folder))
41+
temp_folder = pathlib.Path(tempfile.mkdtemp(prefix=prefix, suffix=suffix,
42+
dir=dir))
43+
try:
44+
yield temp_folder
45+
finally:
46+
shutil.rmtree(str(temp_folder))

source_py3/test_python_toolbox/test_temp_file_tools/test_create_temp_folder.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,14 @@ def test_without_pathlib():
8585
assert not os.path.isdir(file_path)
8686

8787

88+
def test_prefix_suffix():
89+
with create_temp_folder(prefix='hocus', suffix='pocus') as tf1:
90+
assert tf1.name.startswith('hocus')
91+
assert tf1.name.endswith('pocus')
8892

93+
def test_dir():
94+
with create_temp_folder() as tf1:
95+
with create_temp_folder(dir=str(tf1)) as tf2:
96+
assert isinstance(tf2, pathlib.Path)
97+
assert str(tf2).startswith(str(tf1))
98+

0 commit comments

Comments
 (0)