From d2d27832f91a5fa0c899653d8017c41e4572b471 Mon Sep 17 00:00:00 2001 From: infeeeee Date: Fri, 24 Mar 2023 16:55:00 +0100 Subject: [PATCH] Platform independent tests --- dyn2py/files.py | 21 ++++++++++++--------- tests/test_DynamoFile.py | 2 +- tests/test_PythonFile.py | 2 +- tests/test_PythonNode.py | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/dyn2py/files.py b/dyn2py/files.py index 6c7cfd5..29ef868 100644 --- a/dyn2py/files.py +++ b/dyn2py/files.py @@ -316,8 +316,8 @@ class DynamoFile(File): self.python_nodes.remove(python_node_in_file) self.python_nodes.add(python_node) - # Update the dict: - node_dict["Code"] = python_node.code + # Update the dict, Dyn files are always CRLF: + node_dict["Code"] = "\r\n".join(python_node.code) self.modified = True @@ -370,8 +370,8 @@ class DynamoFile(File): class PythonFile(File): """A Python file, subclass of File()""" - code: str - """The python code as a string.""" + code: list[str] + """The python code.""" header_data: dict """Parsed dict from the header of a python file.""" text: str @@ -431,7 +431,7 @@ class PythonFile(File): header_string, HEADER_SEPARATOR, header_wrapper, - python_node.code + os.linesep.join(python_node.code) ]) self.code = python_node.code @@ -489,7 +489,7 @@ class PythonFile(File): raise PythonFileException("Error reading header!") self.header_data[line[0:sl]] = line[sl+1:] - self.code = os.linesep.join(python_lines[code_start_line:]) + self.code = python_lines[code_start_line:] self.open_files.add(self) logging.debug(f"Header data from python file: {self.header_data}") @@ -563,7 +563,7 @@ class PythonNode(): """The id of the node""" engine: str """The engine of the node, IronPython2 or CPython3""" - code: str + code: list[str] """The full code""" checksum: str """The checksum of the code, for checking changes""" @@ -599,7 +599,8 @@ class PythonNode(): else: self.engine = "IronPython2" - self.code = node_dict_from_dyn["Code"] + # It's read from a dynamo file, so separator is always CRLF + self.code = node_dict_from_dyn["Code"].split("\r\n") # Get the name of the node: self.name = next( @@ -618,6 +619,7 @@ class PythonNode(): "_".join(filename_parts) + ".py") self.filepath = dynamo_file.dirpath.joinpath(self.filename) + # Initialize from a python file: elif python_file and not node_dict_from_dyn and not dynamo_file: self.id = python_file.header_data["py_id"] self.engine = python_file.header_data["py_engine"] @@ -629,4 +631,5 @@ class PythonNode(): raise PythonNodeException # Calculate checksum: - self.checksum = hashlib.md5(self.code.encode()).hexdigest() + checksums = [hashlib.md5(l.encode()).hexdigest() for l in self.code] + self.checksum = hashlib.md5("".join(checksums).encode()).hexdigest() diff --git a/tests/test_DynamoFile.py b/tests/test_DynamoFile.py index 495948d..0088beb 100644 --- a/tests/test_DynamoFile.py +++ b/tests/test_DynamoFile.py @@ -37,7 +37,7 @@ class TestDynamoFile(unittest.TestCase): self.assertEqual(len(dyn.python_nodes), 6) self.assertTrue(py_node) self.assertIn(py_node, dyn.python_nodes) - self.assertEqual(py_node.checksum, "1f3d9e6153804fe1ed37571a9cda8e26") + self.assertEqual(py_node.checksum, "e830a6ae6b395bcfd4e5a40da48f3bfc") with self.assertRaises(dyn2py.PythonNodeNotFoundException): dyn.get_python_node_by_id("wrongid") diff --git a/tests/test_PythonFile.py b/tests/test_PythonFile.py index 4cd131b..89ece01 100644 --- a/tests/test_PythonFile.py +++ b/tests/test_PythonFile.py @@ -22,7 +22,7 @@ class TestPythonFile(unittest.TestCase): for py in [py1, py2]: - self.assertEqual(len(py.code.split(os.linesep)), 17) + self.assertEqual(len(py.code), 17) self.assertEqual(len(py.text.split(os.linesep)), 32, msg=py.filepath) self.assertIs(type(py.header_data), dict) self.assertTrue(py in dyn2py.PythonFile.open_files) diff --git a/tests/test_PythonNode.py b/tests/test_PythonNode.py index d6a40ba..7909d99 100644 --- a/tests/test_PythonNode.py +++ b/tests/test_PythonNode.py @@ -21,7 +21,7 @@ class TestPythonNode(unittest.TestCase): self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.engine, "CPython3") - self.assertEqual(node.checksum, "ec2c85a11ddbf8375da03f11272d427a") + self.assertEqual(node.checksum, "92d46019bf538072db6bd1287267c147") self.assertEqual(node.name, "Python Script") self.assertEqual( node.filename, "single_node_1c5d99792882409e97e132b3e9f814b0.py") @@ -36,7 +36,7 @@ class TestPythonNode(unittest.TestCase): self.assertEqual(node.id, "1c5d99792882409e97e132b3e9f814b0") self.assertEqual(node.engine, "CPython3") - self.assertEqual(node.checksum, "8d9091d24788a6fdfa5e1e109298b50e") + self.assertEqual(node.checksum, "64a73c52bd213b6f1b593697cdde789b") def test_init_exception(self):