Skip to content

Commit

Permalink
Use windows home dir as temp dir location (#3951)
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Zhu <[email protected]>
  • Loading branch information
peterzhuamazon committed Sep 5, 2023
1 parent 2296613 commit 2942920
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/system/temporary_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/tests_system/test_temporary_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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()))

0 comments on commit 2942920

Please sign in to comment.