App: Improve Metadata error handling

Report XML parse exception details.
This commit is contained in:
Chris Hennes
2022-03-13 21:00:49 -05:00
parent 2ac4e60ab5
commit 2fc9c93235
7 changed files with 103 additions and 1 deletions

View File

@@ -18,6 +18,9 @@ SET(Test_SRCS
SET(TestData_SRCS
TestData/basic_metadata.xml
TestData/bad_root_node.xml
TestData/bad_xml.xml
TestData/bad_version.xml
)
SOURCE_GROUP("" FILES ${Test_SRCS} ${TestData_SRCS})

View File

@@ -31,6 +31,40 @@ class TestMetadata(unittest.TestCase):
def setUp(self):
self.test_dir = os.path.join(FreeCAD.getHomePath(), "Mod", "Test", "TestData")
def test_xml_constructor(self):
try:
filename = os.path.join(self.test_dir, "basic_metadata.xml")
md = FreeCAD.Metadata(filename)
except Exception:
self.fail("Metadata construction from XML file failed")
try:
filename = os.path.join(self.test_dir, "bad_root_node.xml")
md = FreeCAD.Metadata(filename)
except Exception as e:
FreeCAD.Console.PrintMessage(str(e) + "\n")
pass
else:
self.fail("Metadata construction from XML file with bad root node did not raise an exception")
try:
filename = os.path.join(self.test_dir, "bad_xml.xml")
md = FreeCAD.Metadata(filename)
except Exception as e:
FreeCAD.Console.PrintMessage(str(e) + "\n")
pass
else:
self.fail("Metadata construction from invalid XML file did not raise an exception")
try:
filename = os.path.join(self.test_dir, "bad_version.xml")
md = FreeCAD.Metadata(filename)
except Exception as e:
FreeCAD.Console.PrintMessage(str(e) + "\n")
pass
else:
self.fail("Metadata construction from XML file with bad version did not raise an exception")
def test_toplevel_tags(self):
filename = os.path.join(self.test_dir, "basic_metadata.xml")
md = FreeCAD.Metadata(filename)

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<notpackage format="1" xmlns="https://wiki.freecad.org/Package_Metadata">
<name>Bad root node</name>
<description>The root node is not called "package".</description>
<version>1.0.0</version>
</notpackage>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<package format="2" xmlns="https://wiki.freecad.org/Package_Metadata">
<name>Bad format</name>
<description>There is no such thing as format version 2 (yet).</description>
<version>1.0.0</version>
</package>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<package format="1" xmlns="https://wiki.freecad.org/Package_Metadata">
<name>Bad XML</notname>
<description>The name tag is not closed properly.</description>
</package>