Skip to content

Commit

Permalink
add michales fix and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gagelarsen committed Jul 7, 2020
1 parent 5750553 commit 4baa113
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions _package/tests/unit_tests/filesystem_pyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ def test_temp_filename(self):
extension = os.path.splitext(f)[1]
self.assertEqual('.suffix_test', extension)

# With XMS_PYTHON_APP_TEMP_DIRECTORY environment variable
d = tempfile.mkdtemp()
os.environ['XMS_PYTHON_APP_TEMP_DIRECTORY'] = d
f = filesystem.temp_filename()
os.environ.pop('XMS_PYTHON_APP_TEMP_DIRECTORY')
self.assertEqual(os.path.normpath(os.path.dirname(f)), os.path.normpath(d))

shutil.rmtree(d)
2 changes: 1 addition & 1 deletion _package/xms/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from . import filesystem # NOQA: F401
from . import misc # NOQA: F401

__version__ = '3.2.1
__version__ = '3.2.1'
9 changes: 6 additions & 3 deletions _package/xms/core/filesystem/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def file_prefix(filepath):


def temp_filename(dir='', suffix=''):
"""Returns a temporary filename.
"""Returns a temporary filename, in the XMS temp directory by default.
Args:
dir: If provided, filename will be in the directory. Otherwise it will be in the
Expand All @@ -191,12 +191,15 @@ def temp_filename(dir='', suffix=''):
Returns:
See description.
"""
if dir: # If we call the next line with dir == '', it seems to use the working directory.
file = tempfile.NamedTemporaryFile(mode='wt', suffix=suffix, dir=dir, delete=True)
else:
file = tempfile.NamedTemporaryFile(mode='wt', suffix=suffix, delete=True)
xms_temp = os.environ.get('XMS_PYTHON_APP_TEMP_DIRECTORY', 'unknown')
if xms_temp != 'unknown':
file = tempfile.NamedTemporaryFile(mode='wt', suffix=suffix, dir=xms_temp, delete=True)
else:
file = tempfile.NamedTemporaryFile(mode='wt', suffix=suffix, delete=True)

filename = file.name
file.close()
Expand Down

0 comments on commit 4baa113

Please sign in to comment.