From 38cd9ff08b57679cb058eb473d4d0c3591cefd03 Mon Sep 17 00:00:00 2001 From: bitsgalore Date: Tue, 19 Apr 2022 19:15:58 +0200 Subject: [PATCH] added test for validity against XSD, moved definition of out and err to processImages function --- isolyzer/isolyzer.py | 13 +++++++------ tests/unit/test_testfiles.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/isolyzer/isolyzer.py b/isolyzer/isolyzer.py index 5564700..68274e2 100644 --- a/isolyzer/isolyzer.py +++ b/isolyzer/isolyzer.py @@ -777,6 +777,13 @@ def processImages(images, offset): """ Process list of images """ + + global out + global err + + out = codecs.getwriter("UTF-8")(sys.stdout.buffer) + err = codecs.getwriter("UTF-8")(sys.stderr.buffer) + # Create output element root = ET.Element("isolyzer", {'xmlns': nsString, 'xmlns:xsi': xsiNsString, @@ -800,12 +807,6 @@ def processImages(images, offset): def main(): """Main command line application""" - global out - global err - - out = codecs.getwriter("UTF-8")(sys.stdout.buffer) - err = codecs.getwriter("UTF-8")(sys.stderr.buffer) - # Get input from command line args = parseCommandLine() diff --git a/tests/unit/test_testfiles.py b/tests/unit/test_testfiles.py index d8217ef..d101dfd 100644 --- a/tests/unit/test_testfiles.py +++ b/tests/unit/test_testfiles.py @@ -10,6 +10,7 @@ from lxml import etree from isolyzer.isolyzer import processImage +from isolyzer.isolyzer import processImages sizeDifferenceSectors = { "hfs.iso":2.0, @@ -53,6 +54,9 @@ # Root dir of isolyzer repo ISOLYZER_DIR = os.path.split(os.path.split(SCRIPT_DIR)[0])[0] +# XSD file (path resolved from SCRIPT_DIR) +xsdFile = os.path.join(ISOLYZER_DIR, "xsd/isolyzer-v-1-0.xsd") + # Directory with test files testFilesDir = os.path.join(ISOLYZER_DIR, "testFiles") @@ -112,3 +116,21 @@ def test_fileSystems(input): fsDetected.append(fsType) # Test if file systems in both lists are identical assert set(fsKnown) == set(fsDetected) + +def test_xml_is_valid(capsys): + """ + Run processImages function on all files in test corpus and + verify resulting XML output validates against XSD schema + """ + + processImages(testFiles, 0) + + # Capture output from stdout + captured = capsys.readouterr() + xmlOut = captured.out + # Parse XSD schema + xmlschema_doc = etree.parse(xsdFile) + xmlschema = etree.XMLSchema(xmlschema_doc) + # Parse XML + xml_doc = etree.fromstring(xmlOut.encode()) + assert xmlschema.validate(xml_doc) \ No newline at end of file