From 83c5f4f649aabb9f6849f9b67b37693bbd72248b Mon Sep 17 00:00:00 2001 From: samwaseda Date: Fri, 16 Aug 2024 07:09:15 +0000 Subject: [PATCH] correct tests --- pyiron_snippets/files.py | 7 +++++-- tests/unit/test_files.py | 29 ++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pyiron_snippets/files.py b/pyiron_snippets/files.py index bc8b94c..c0aa51d 100644 --- a/pyiron_snippets/files.py +++ b/pyiron_snippets/files.py @@ -98,6 +98,8 @@ def __new__(cls, file_name: str, directory: DirectoryObject = None): if directory is None: full_path = Path(file_name) else: + if isinstance(directory, str): + directory = DirectoryObject(directory) full_path = directory.joinpath(file_name) instance = super().__new__(cls, full_path) return instance @@ -111,7 +113,7 @@ def read(self, mode="r"): return f.read() def is_file(self): - return self.exists() and self.is_file() + return self.exists() and super().is_file() def delete(self): self.unlink() @@ -131,8 +133,9 @@ def copy( if directory is None: directory = self.parent + elif isinstance(directory, str): + directory = DirectoryObject(directory) new_file = directory.joinpath(new_file_name) shutil.copy(str(self), str(new_file)) return FileObject(new_file_name, DirectoryObject(directory)) - diff --git a/tests/unit/test_files.py b/tests/unit/test_files.py index fc39e57..c0e2c50 100644 --- a/tests/unit/test_files.py +++ b/tests/unit/test_files.py @@ -4,7 +4,6 @@ from pyiron_snippets.files import DirectoryObject, FileObject - class TestFiles(unittest.TestCase): def setUp(self): self.directory = DirectoryObject("test") @@ -31,11 +30,6 @@ def test_file_instantiation(self): msg="File path not the same as directory path" ) - if system() == "Windows": - self.assertRaises(ValueError, FileObject, "C:\\test.txt", "test") - else: - self.assertRaises(ValueError, FileObject, "/test.txt", "test") - def test_directory_exists(self): self.assertTrue(self.directory.exists() and self.directory.is_dir()) @@ -125,7 +119,28 @@ def test_remove(self): def test_copy(self): f = FileObject("test_copy.txt", self.directory) f.write("sam wrote this wonderful thing") - new_file_1 + new_file_1 = f.copy("another_test") + self.assertEqual(new_file_1.read(), "sam wrote this wonderful thing") + new_file_2 = f.copy("another_test", ".") + with open("another_test", "r") as file: + txt = file.read() + self.assertEqual(txt, "sam wrote this wonderful thing") + new_file_2.delete() # needed because current directory + new_file_3 = f.copy(str(f.parent / "another_test"), ".") + self.assertEqual(new_file_1, new_file_3) + new_file_4 = f.copy(directory=".") + with open("test_copy.txt", "r") as file: + txt = file.read() + self.assertEqual(txt, "sam wrote this wonderful thing") + new_file_4.delete() # needed because current directory + with self.assertRaises(ValueError): + f.copy() + + def test_str(self): + f = FileObject("test_copy.txt", self.directory) + expected_path = str(self.directory / "test_copy.txt") + self.assertEqual(str(f), expected_path) + if __name__ == '__main__': unittest.main()