diff --git a/TODO.md b/TODO.md index 064a968..d91efa6 100644 --- a/TODO.md +++ b/TODO.md @@ -7,6 +7,7 @@ - [x] DynamoFile - [x] PythonFile - [x] PythonNode +- [ ] Options - [ ] run() ## CI/CD diff --git a/tests/support.py b/tests/support.py index 0cb0e57..bb6e72c 100644 --- a/tests/support.py +++ b/tests/support.py @@ -3,15 +3,17 @@ import dyn2py INPUT_DIR = "tests/input_files" OUTPUT_DIR = "tests/output_files" +TEMP_DIR = "tests/temp_files" -def cleanup_output_dir(): - output_dir = pathlib.Path(OUTPUT_DIR) - if output_dir.exists(): - for f in output_dir.iterdir(): - f.unlink() - else: - output_dir.mkdir() +def cleanup_dirs(): + for p in [OUTPUT_DIR, TEMP_DIR]: + the_dir = pathlib.Path(p) + if the_dir.exists(): + for f in the_dir.iterdir(): + f.unlink() + else: + the_dir.mkdir() def extract_single_node_dyn(modify_py: bool = False): @@ -23,7 +25,7 @@ def extract_single_node_dyn(modify_py: bool = False): modify_py (bool, optional): Also do some changes on the exported file. Defaults to False. """ - cleanup_output_dir() + cleanup_dirs() # Extract py: options = dyn2py.Options(python_folder=OUTPUT_DIR) diff --git a/tests/test_CommandLine.py b/tests/test_CommandLine.py index 2b75e7e..525808b 100644 --- a/tests/test_CommandLine.py +++ b/tests/test_CommandLine.py @@ -36,8 +36,12 @@ class TestCommandLine(unittest.TestCase): self.assertFalse(p.stderr) dyn_sources = [ - {"filename": "python_nodes.dyn", "py_file_count": 6}, - {"filename": "single_node.dyn", "py_file_count": 1} + {"filename": "python_nodes.dyn", "output_file_count": 6}, + {"filename": "single_node.dyn", "output_file_count": 1} + ] + + py_sources = [ + {"filename": "single_node_mod.py"}, ] dyn_sources_error = ["dynamo1file.dyn", @@ -85,7 +89,7 @@ class TestCommandLine(unittest.TestCase): test_dicts[0]["filenames"] = [ test_dicts[0]["filename"]] - if i == 0: + if i == 0 and len(source_dict) > 1: # Create a multi file version on the first file: d = {} for key in source_dict: @@ -100,7 +104,7 @@ class TestCommandLine(unittest.TestCase): for test_dict in test_dicts: if pfolder_option: - file_dir = INPUT_DIR + file_dir = TEMP_DIR pfolder_arg = f"{pfolder_option} {OUTPUT_DIR}" else: file_dir = OUTPUT_DIR @@ -122,7 +126,7 @@ class TestCommandLine(unittest.TestCase): def test_dyn_error(self): for s in self.dyn_sources_error: - cleanup_output_dir() + cleanup_dirs() if pathlib.Path(f"{INPUT_DIR}/{s}").exists(): shutil.copy(f"{INPUT_DIR}/{s}", @@ -140,12 +144,19 @@ class TestCommandLine(unittest.TestCase): dyn_tests = self.generate_test_args(self.dyn_sources) for s in dyn_tests: - cleanup_output_dir() + cleanup_dirs() + # if no pythonfolder, everything should be in output ddir if not s["pfolder_arg"]: - for filename in s["filenames"]: - shutil.copy(f"{INPUT_DIR}/{filename}", - f"{OUTPUT_DIR}/{filename}") + source_dir = OUTPUT_DIR + else: + source_dir = TEMP_DIR + + # copy source files: + for filename in s["filenames"]: + shutil.copy(f"{INPUT_DIR}/{filename}", + f"{source_dir}/{filename}") + # Open files normally file_open = self.run_command( @@ -155,7 +166,7 @@ class TestCommandLine(unittest.TestCase): self.assertFalse( bool(file_open["stderr"]), msg=file_open["stderr"]) self.assertEqual( - len(file_open["python_file_mtimes"]), s["py_file_count"]) + len(file_open["python_file_mtimes"]), s["output_file_count"]) # Test no overwrite file_no_overwrite = self.run_command( @@ -164,7 +175,7 @@ class TestCommandLine(unittest.TestCase): # Should give error, because they already exist: self.assertTrue(bool(file_no_overwrite["stderr"])) self.assertEqual( - len(file_no_overwrite["python_file_mtimes"]), s["py_file_count"]) + len(file_no_overwrite["python_file_mtimes"]), s["output_file_count"]) # The modify time shouldn't change as they were not overwritten: for p in file_no_overwrite["python_file_mtimes"]: @@ -178,9 +189,9 @@ class TestCommandLine(unittest.TestCase): # Should not have an error: self.assertFalse(bool(file_force["stderr"])) self.assertEqual( - len(file_force["python_file_mtimes"]), s["py_file_count"]) + len(file_force["python_file_mtimes"]), s["output_file_count"]) - #Modify time should be higher as they were replaced + # Modify time should be higher as they were replaced for p in file_force["python_file_mtimes"]: self.assertTrue( file_force["python_file_mtimes"][p] > file_open["python_file_mtimes"][p] @@ -193,20 +204,52 @@ class TestCommandLine(unittest.TestCase): self.assertFalse(bool(file_backup["stderr"])) self.assertEqual( - len(file_backup["python_file_mtimes"]), s["py_file_count"] * 2, + len(file_backup["python_file_mtimes"] + ), s["output_file_count"] * 2, msg=f"" - ) + ) for p in file_force["python_file_mtimes"]: self.assertTrue( file_backup["python_file_mtimes"][p] > file_force["python_file_mtimes"][p] ) + # def test_py(self): + # py_tests = self.generate_test_args(self.py_sources) + + # # TODO add more python files! + # self.assertEqual(len(py_tests), 1) + + # for s in py_tests: + # cleanup_dirs() + + # extract_single_node_dyn(modify_py=True) + + + # # if pythonfolder, python should be in the temp folder: + # if s["pfolder_arg"]: + # for filename in s["filenames"]: + # shutil.move(f"{OUTPUT_DIR}/{filename}", + # f"{TEMP_DIR}/{filename}") + + # # Open files normally + # file_open = self.run_command( + # [s["pfolder_arg"], s['filepath']]) + + + # # Open without error: + # self.assertFalse( + # bool(file_open["stderr"]), msg=file_open["stderr"]) + # self.assertEqual( + # len(file_open["python_file_mtimes"]), s["output_file_count"]) + + + def test_single_dyn_dryrun(self): for s in self.dyn_sources: for arg in ["-n", "--dry-run"]: - cleanup_output_dir() + cleanup_dirs() shutil.copy(f"{INPUT_DIR}/{s['filename']}", f"{OUTPUT_DIR}/{s['filename']}") diff --git a/tests/test_DynamoFile.py b/tests/test_DynamoFile.py index 0157bd3..3bda1ee 100644 --- a/tests/test_DynamoFile.py +++ b/tests/test_DynamoFile.py @@ -43,7 +43,7 @@ class TestDynamoFile(unittest.TestCase): dyn.get_python_node_by_id("wrongid") def test_extract_python(self): - cleanup_output_dir() + cleanup_dirs() dyn2py.PythonFile.open_files.clear() opt = dyn2py.Options(python_folder=OUTPUT_DIR) @@ -69,7 +69,7 @@ class TestDynamoFile(unittest.TestCase): dyn2py.DynamoFile.get_open_file_by_uuid("76de5c79-17c5-4c74-9f90-ad99a213d339")) def test_get_related_python_files(self): - cleanup_output_dir() + cleanup_dirs() opt = dyn2py.Options(python_folder=OUTPUT_DIR) dyn1 = dyn2py.DynamoFile(f"{INPUT_DIR}/python_nodes.dyn") @@ -91,7 +91,7 @@ class TestDynamoFile(unittest.TestCase): self.assertFalse(no_python_files) def test_write_same(self): - cleanup_output_dir() + cleanup_dirs() shutil.copy(f"{INPUT_DIR}/python_nodes.dyn", f"{OUTPUT_DIR}/python_nodes.dyn") diff --git a/tests/test_File.py b/tests/test_File.py index c2f791e..34aeb52 100644 --- a/tests/test_File.py +++ b/tests/test_File.py @@ -8,8 +8,6 @@ from tests.support import * class TestFile(unittest.TestCase): - # Write methods should be tested in subclasses! - def test_init(self): paths = [ f"{INPUT_DIR}/python_nodes.dyn", @@ -71,7 +69,7 @@ class TestFile(unittest.TestCase): nonexisting_file = dyn2py.File(f"{INPUT_DIR}/new_file.py") # Extract a python file so it is always newer than the others: - cleanup_output_dir() + cleanup_dirs() opt = dyn2py.Options(python_folder=OUTPUT_DIR) older_file.extract_python(options=opt) # type: ignore for f in dyn2py.PythonFile.open_files: @@ -103,3 +101,15 @@ class TestFile(unittest.TestCase): self.assertEqual(file.is_dynamo_file(), f == "dyn") self.assertEqual(file.is_python_file(), f == "py") + + def test_write(self): + + # new empty file: + empty_filepath = pathlib.Path(f"{OUTPUT_DIR}/empty.txt") + empty_filepath.touch() + + empty_file = dyn2py.File(f"{OUTPUT_DIR}/empty.txt") + + self.assertTrue(empty_file.exists) + with self.assertRaises(TypeError): + empty_file.write() \ No newline at end of file