diff --git a/src/Mod/AddonManager/AddonManagerTest/app/test_macro.py b/src/Mod/AddonManager/AddonManagerTest/app/test_macro.py new file mode 100644 index 0000000000..a7edb81773 --- /dev/null +++ b/src/Mod/AddonManager/AddonManagerTest/app/test_macro.py @@ -0,0 +1,182 @@ +# -*- coding: utf-8 -*- + +# *************************************************************************** +# * Copyright (c) 2022 FreeCAD Project Association * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This library is free software; you can redistribute it and/or * +# * modify it under the terms of the GNU Lesser General Public * +# * License as published by the Free Software Foundation; either * +# * version 2.1 of the License, or (at your option) any later version. * +# * * +# * This library is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * +# * Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Lesser General Public * +# * License along with this library; if not, write to the Free Software * +# * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * +# * 02110-1301 USA * +# * * +# *************************************************************************** + +import unittest +import os +import tempfile +import FreeCAD + +from typing import Dict + +from addonmanager_macro import Macro + +class TestMacro(unittest.TestCase): + + MODULE = "test_macro" # file name without extension + + def setUp(self): + self.test_dir = os.path.join(FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data") + + def test_basic_metadata(self): + replacements = { + "COMMENT": "test comment", + "WEB": "https://test.url", + "VERSION": "1.2.3", + "AUTHOR": "Test Author", + "DATE": "2022-03-09", + "ICON": "testicon.svg", + } + m = self.generate_macro(replacements) + self.assertEqual(m.comment, replacements["COMMENT"]) + self.assertEqual(m.url, replacements["WEB"]) + self.assertEqual(m.version, replacements["VERSION"]) + self.assertEqual(m.author, replacements["AUTHOR"]) + self.assertEqual(m.date, replacements["DATE"]) + self.assertEqual(m.icon, replacements["ICON"]) + + def test_other_files(self): + replacements = { + "FILES": "file_a,file_b,file_c", + } + m = self.generate_macro(replacements) + self.assertEqual(len(m.other_files), 3) + self.assertEqual(m.other_files[0], "file_a") + self.assertEqual(m.other_files[1], "file_b") + self.assertEqual(m.other_files[2], "file_c") + + replacements = { + "FILES": "file_a, file_b, file_c", + } + m = self.generate_macro(replacements) + self.assertEqual(len(m.other_files), 3) + self.assertEqual(m.other_files[0], "file_a") + self.assertEqual(m.other_files[1], "file_b") + self.assertEqual(m.other_files[2], "file_c") + + replacements = { + "FILES": "file_a file_b file_c", + } + m = self.generate_macro(replacements) + self.assertEqual(len(m.other_files), 1) + self.assertEqual(m.other_files[0], "file_a file_b file_c") + + def test_version_from_string(self): + replacements = { + "VERSION": "1.2.3", + } + m = self.generate_macro(replacements) + self.assertEqual(m.version, "1.2.3") + + def test_version_from_date(self): + replacements = { + "DATE": "2022-03-09", + } + outfile = self.generate_macro_file(replacements) + with open(outfile) as f: + lines = f.readlines() + output_lines = [] + for line in lines: + if "VERSION" in line: + line = "__Version__ = __Date__" + output_lines.append(line) + with open(outfile,"w") as f: + f.write("\n".join(output_lines)) + m = Macro("Unit Test Macro") + m.fill_details_from_file(outfile) + self.assertEqual(m.version, "2022-03-09") + + def test_version_from_float(self): + outfile = self.generate_macro_file() + with open(outfile) as f: + lines = f.readlines() + output_lines = [] + for line in lines: + if "VERSION" in line: + line = "__Version__ = 1.23" + output_lines.append(line) + with open(outfile,"w") as f: + f.write("\n".join(output_lines)) + m = Macro("Unit Test Macro") + m.fill_details_from_file(outfile) + self.assertEqual(m.version, "1.23") + + def test_version_from_int(self): + outfile = self.generate_macro_file() + with open(outfile) as f: + lines = f.readlines() + output_lines = [] + for line in lines: + if "VERSION" in line: + line = "__Version__ = 1" + output_lines.append(line) + with open(outfile,"w") as f: + f.write("\n".join(output_lines)) + m = Macro("Unit Test Macro") + m.fill_details_from_file(outfile) + self.assertEqual(m.version, "1") + + def test_xpm(self): + outfile = self.generate_macro_file() + xpm_data = """/* XPM */ +static char * blarg_xpm[] = { +"16 7 2 1", +"* c #000000", +". c #ffffff", +"**..*...........", +"*.*.*...........", +"**..*..**.**..**", +"*.*.*.*.*.*..*.*", +"**..*..**.*...**", +"...............*", +".............**." +};""" + with open(outfile) as f: + contents = f.read() + contents += f"\n__xpm__ = \"\"\"{xpm_data}\"\"\"\n" + + with open(outfile,"w") as f: + f.write(contents) + m = Macro("Unit Test Macro") + m.fill_details_from_file(outfile) + self.assertEqual(m.xpm, xpm_data) + + + def generate_macro_file(self, replacements:Dict[str,str] = {}) -> os.PathLike: + with open(os.path.join(self.test_dir,"macro_template.FCStd")) as f: + lines = f.readlines() + outfile = tempfile.NamedTemporaryFile(mode="wt",delete=False) + for line in lines: + for key,value in replacements.items(): + line = line.replace(key,value) + + outfile.write(line) + outfile.close() + return outfile.name + + def generate_macro(self, replacements:Dict[str,str] = {}) -> Macro: + outfile = self.generate_macro_file(replacements) + m = Macro("Unit Test Macro") + m.fill_details_from_file(outfile) + os.unlink(outfile) + return m \ No newline at end of file diff --git a/src/Mod/AddonManager/AddonManagerTest/data/macro_template.FCStd b/src/Mod/AddonManager/AddonManagerTest/data/macro_template.FCStd new file mode 100644 index 0000000000..74778e67a4 --- /dev/null +++ b/src/Mod/AddonManager/AddonManagerTest/data/macro_template.FCStd @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +# *************************************************************************** +# * Copyright (c) 2022 FreeCAD Project Association * +# * * +# * This file is part of the FreeCAD CAx development system. * +# * * +# * This library is free software; you can redistribute it and/or * +# * modify it under the terms of the GNU Lesser General Public * +# * License as published by the Free Software Foundation; either * +# * version 2.1 of the License, or (at your option) any later version. * +# * * +# * This library is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * +# * Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Lesser General Public * +# * License along with this library; if not, write to the Free Software * +# * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * +# * 02110-1301 USA * +# * * +# *************************************************************************** + +__Title__ = "TITLE" +__Author__ = "AUTHOR" +__Date__ = "DATE" +__Version__ = "VERSION" +__Comment__ = "COMMENT" +__Web__ = "WEB" +__Wiki__ = "WIKI" +__Icon__ = "ICON" +__Help__ = "HELP" +__Status__ = "STATUS" +__Requires__ = "REQUIRES" +__Communication__ = "COMMUNICATION" +__Files__ = "FILES" \ No newline at end of file diff --git a/src/Mod/AddonManager/CMakeLists.txt b/src/Mod/AddonManager/CMakeLists.txt index ced99b68f5..bfc7772c75 100644 --- a/src/Mod/AddonManager/CMakeLists.txt +++ b/src/Mod/AddonManager/CMakeLists.txt @@ -25,8 +25,8 @@ SET(AddonManager_SRCS NetworkManager.py package_details.py package_list.py - TestAddonManagerApp.py select_toolbar_dialog.ui + TestAddonManagerApp.py ) IF (BUILD_GUI) LIST(APPEND AddonManager_SRCS TestAddonManagerGui.py) @@ -41,8 +41,9 @@ SET(AddonManagerTests_SRCS SET(AddonManagerTestsApp_SRCS AddonManagerTest/app/__init__.py - AddonManagerTest/app/test_utilities.py AddonManagerTest/app/test_addon.py + AddonManagerTest/app/test_macro.py + AddonManagerTest/app/test_utilities.py ) SET(AddonManagerTestsGui_SRCS @@ -59,6 +60,7 @@ SET(AddonManagerTestsFiles_SRCS AddonManagerTest/data/good_macro_metadata.FCStd AddonManagerTest/data/good_package.xml AddonManagerTest/data/macro_only.xml + AddonManagerTest/data/macro_template.FCStd AddonManagerTest/data/missing_macro_metadata.FCStd AddonManagerTest/data/prefpack_only.xml AddonManagerTest/data/test_version_detection.xml diff --git a/src/Mod/AddonManager/TestAddonManagerApp.py b/src/Mod/AddonManager/TestAddonManagerApp.py index 2d0cc063a8..7602010674 100644 --- a/src/Mod/AddonManager/TestAddonManagerApp.py +++ b/src/Mod/AddonManager/TestAddonManagerApp.py @@ -29,7 +29,11 @@ from AddonManagerTest.app.test_utilities import ( from AddonManagerTest.app.test_addon import ( TestAddon as AddonManagerTestAddon, ) +from AddonManagerTest.app.test_macro import ( + TestMacro as AddonManagerTestMacro, +) # dummy usage to get flake8 and lgtm quiet False if AddonManagerTestUtilities.__name__ else True False if AddonManagerTestAddon.__name__ else True +False if AddonManagerTestMacro.__name__ else True