Skip to content

Commit

Permalink
Use windows home dir as temp dir location
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Zhu <[email protected]>
  • Loading branch information
peterzhuamazon committed Aug 31, 2023
1 parent 7232a94 commit facdec7
Show file tree
Hide file tree
Showing 2 changed files with 20 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)

Check warning on line 45 in src/system/temporary_directory.py

View check run for this annotation

Codecov / codecov/patch

src/system/temporary_directory.py#L44-L45

Added lines #L44 - L45 were not covered by tests
else:
self.name = tempfile.mkdtemp()

if chdir:
self.curdir = os.getcwd()
os.chdir(self.name)
Expand Down
12 changes: 12 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,13 @@ 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":
print('windows')
windows_home_dir = os.path.expanduser('~')
self.assertTrue(str(work_dir.path).startswith(windows_home_dir))
else:
print('others')
self.assertTrue(str(work_dir.path).startswith(tempfile.gettempdir()))

0 comments on commit facdec7

Please sign in to comment.