Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkennard-aquaveo committed Dec 6, 2023
1 parent 9cc9324 commit 2a969e2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
83 changes: 46 additions & 37 deletions _package/tests/unit_tests/filesystem_pyt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
__license__ = "All rights reserved"


def _make_bob_file(dir_: str | Path) -> str:
"""Makes a file in dir_ named 'bob.txt' with 'bob' in the file."""
temp_file_path = os.path.join(str(dir_), 'bob.txt')
with open(temp_file_path, 'w') as file:
file.write('bob')
return temp_file_path


class FilesystemTests(unittest.TestCase):
"""Tests functions in filesystem.py."""
def test_file_prefix(self):
Expand All @@ -44,7 +52,8 @@ def test_file_prefix(self):
]
for input, result in paths:
self.assertEqual(result, filesystem.file_prefix(input)) # Test using str
self.assertEqual(result, filesystem.file_prefix(Path(input))) # Test using pathlib
if input != '':
self.assertEqual(result, filesystem.file_prefix(Path(input))) # Test using pathlib

def test_temp_filename(self):
"""Tests filesystem.temp_filename()."""
Expand Down Expand Up @@ -180,24 +189,23 @@ def test_make_or_clear_dir(self):
assert not Path(path).is_dir()
make_or_clear_dir(path)
assert Path(path).is_dir()
temp_file = tempfile.NamedTemporaryFile(mode='wt', dir=path)
assert Path(temp_file.name).is_file()
file_path = _make_bob_file(path)
assert Path(file_path).is_file()
make_or_clear_dir(path)
assert not Path(temp_file.name).is_file()
assert not Path(file_path).is_file()
assert Path(path).is_dir()
Path(path).unlink()
shutil.rmtree(path)

# Test using pathlib
path = Path(d) / dir1 / dir2
assert not path.is_dir()
make_or_clear_dir(path)
assert path.is_dir()
temp_file = tempfile.NamedTemporaryFile(mode='wt', dir=path)
assert Path(temp_file.name).is_file()
file_path = _make_bob_file(path)
assert Path(file_path).is_file()
make_or_clear_dir(path)
assert not Path(temp_file.name).is_file()
assert not Path(file_path).is_file()
assert path.is_dir()
path.unlink()

shutil.rmtree(d)

Expand All @@ -217,42 +225,43 @@ def test_paths_are_equal(self):
def test_copyfile(self):
"""Tests filesystem.copyfile()."""
# Test using str
d = tempfile.mkdtemp()
temp_file = tempfile.NamedTemporaryFile(mode='wt', dir=d)
with Path(temp_file.name).open('w') as file:
file.write('bob')
temp_file_copy = os.path.join(d, 'copy')
copyfile(temp_file, temp_file_copy)
assert filecmp.cmp(temp_file, temp_file_copy)
shutil.rmtree(d)
temp_dir = tempfile.mkdtemp()
file_path = _make_bob_file(temp_dir)
file_path_copy = os.path.join(temp_dir, 'bob_copy.txt')
copyfile(file_path, file_path_copy)
assert filecmp.cmp(file_path, file_path_copy)
shutil.rmtree(temp_dir)

# Test using pathlib
d = tempfile.mkdtemp()
temp_file = tempfile.NamedTemporaryFile(mode='wt', dir=d)
with Path(temp_file.name).open('w') as file:
file.write('bob')
temp_file_copy = os.path.join(d, 'copy')
copyfile(Path(temp_file.name), Path(temp_file_copy))
assert filecmp.cmp(temp_file, temp_file_copy)
shutil.rmtree(d)
temp_dir = tempfile.mkdtemp()
file_path = _make_bob_file(temp_dir)
file_path_copy = os.path.join(temp_dir, 'bob_copy.txt')
copyfile(Path(file_path), Path(file_path_copy))
assert filecmp.cmp(file_path, file_path_copy)
shutil.rmtree(temp_dir)

def test_removefile(self):
"""Tests filesystem.removefile()."""
# Test using str
temp_file = tempfile.NamedTemporaryFile(mode='wt')
assert Path(temp_file.name).is_file()
removefile(temp_file)
assert Path(temp_file.name).is_file()
temp_dir = tempfile.mkdtemp()
file_path = _make_bob_file(temp_dir)
assert Path(file_path).is_file()
removefile(file_path)
assert not Path(file_path).is_file()

# Test using pathlib
temp_file = tempfile.NamedTemporaryFile(mode='wt')
assert Path(temp_file.name).is_file()
removefile(Path(temp_file.name))
assert Path(temp_file.name).is_file()
file_path = _make_bob_file(temp_dir)
assert Path(file_path).is_file()
removefile(Path(file_path))
assert not Path(file_path).is_file()

shutil.rmtree(temp_dir)

def test_is_somewhere_below_system_temp(self):
"""Tests filesystem.is_somewhere_below_system_temp()."""
temp_file = tempfile.NamedTemporaryFile(mode='wt')
self.assertTrue(True, is_somewhere_below_system_temp(temp_file))
self.assertTrue(True, is_somewhere_below_system_temp(Path(temp_file.name)))
Path(temp_file.name).unlink()
temp_dir = tempfile.mkdtemp()
file_path = _make_bob_file(temp_dir)
self.assertTrue(True, is_somewhere_below_system_temp(file_path))
self.assertTrue(True, is_somewhere_below_system_temp(Path(file_path)))
Path(file_path).unlink()
shutil.rmtree(temp_dir)
10 changes: 5 additions & 5 deletions _package/xms/core/filesystem/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def compute_relative_path(path: str | Path, file: str | Path) -> str:
try:
return os.path.relpath(file, path).replace('\\', '/')
except Exception:
return file.replace('\\', '/')
return str(file).replace('\\', '/')


def is_somewhere_below_system_temp(filename: str | Path) -> bool:
Expand Down Expand Up @@ -177,19 +177,19 @@ def file_prefix(filepath: str | Path) -> str:
return os.path.splitext(basename)[0]


def temp_filename(dir_: str | Path = None, suffix: str = '') -> str:
def temp_filename(dir: str | Path = None, suffix: str = '') -> str:
"""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
dir: If provided, filename will be in the directory. Otherwise it will be in the
system temp directory.
suffix: The suffix to use for the file. You must include a '.' if you want it to be an extension.
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)
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:
xms_temp = os.environ.get('XMS_PYTHON_APP_TEMP_DIRECTORY', 'unknown')
if xms_temp != 'unknown':
Expand Down

0 comments on commit 2a969e2

Please sign in to comment.