Skip to content

Commit ffdd261

Browse files
committed
testhelper/fshelper.py: extend get_tmp_file()
Allow us to create a temporary file of a certain size by passing the filesize paramter. Signed-off-by: Sachin Prabhu <[email protected]>
1 parent 6a3f2e1 commit ffdd261

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

testhelper/fshelper.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,23 @@ def get_tmp_mount_point(tmp_root: Path = Path(tempfile.gettempdir())) -> Path:
2929
return Path(tempfile.mkdtemp(prefix="mnt_", dir=tmp_root))
3030

3131

32-
def get_tmp_file(tmp_root: Path = Path(tempfile.gettempdir())) -> Path:
32+
def get_tmp_file(
33+
tmp_root: Path = Path(tempfile.gettempdir()), size: int = 0
34+
) -> Path:
3335
"""
3436
Return a temporary file within the temporary directory
3537
3638
Parameters:
3739
tmp_root: Directory in which to create temporary file.
40+
size: If provided, create a file of size bytes
3841
3942
Returns:
4043
tmp_file: Location of temporary file.
4144
"""
42-
(fd, file_name) = tempfile.mkstemp(dir=tmp_root)
43-
os.close(fd)
45+
fd, file_name = tempfile.mkstemp(dir=tmp_root)
46+
if size != 0:
47+
with os.fdopen(fd, "wb") as f:
48+
f.write(os.urandom(size))
4449
return Path(file_name)
4550

4651

0 commit comments

Comments
 (0)