From 2942920eedac2afa2d0e6862abdccc7cf194f9a6 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 5 Sep 2023 14:20:09 -0400 Subject: [PATCH] Use windows home dir as temp dir location (#3951) Signed-off-by: Peter Zhu --- src/system/temporary_directory.py | 9 ++++++++- tests/tests_system/test_temporary_directory.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/system/temporary_directory.py b/src/system/temporary_directory.py index 5b7cac247b..a39d56b797 100644 --- a/src/system/temporary_directory.py +++ b/src/system/temporary_directory.py @@ -14,6 +14,8 @@ from types import FunctionType from typing import Any +from system.os import current_platform + def g__handleRemoveReadonly(func: FunctionType, path: str, exc: Any) -> Any: excvalue = exc[1] @@ -38,7 +40,12 @@ def g__handleRemoveReadonly(func: FunctionType, path: str, exc: Any) -> Any: class TemporaryDirectory: def __init__(self, keep: bool = False, chdir: bool = False) -> None: self.keep = keep - self.name = tempfile.mkdtemp() + if current_platform() == "windows": + windows_home_dir = os.path.expanduser('~') + self.name = tempfile.mkdtemp(dir=windows_home_dir) + else: + self.name = tempfile.mkdtemp() + if chdir: self.curdir = os.getcwd() os.chdir(self.name) diff --git a/tests/tests_system/test_temporary_directory.py b/tests/tests_system/test_temporary_directory.py index 6c3f2461c3..9151c955eb 100644 --- a/tests/tests_system/test_temporary_directory.py +++ b/tests/tests_system/test_temporary_directory.py @@ -7,9 +7,11 @@ import os import stat +import tempfile import unittest from pathlib import Path +from system.os import current_platform from system.temporary_directory import TemporaryDirectory @@ -51,3 +53,11 @@ def test_path(self) -> None: with TemporaryDirectory() as work_dir: self.assertIsInstance(work_dir.path, Path) self.assertTrue(work_dir.path.exists()) + + def test_path_windows(self) -> None: + with TemporaryDirectory() as work_dir: + if current_platform() == "windows": + windows_home_dir = os.path.expanduser('~') + self.assertTrue(str(work_dir.path).startswith(windows_home_dir)) + else: + self.assertTrue(str(work_dir.path).startswith(tempfile.gettempdir()))