[Libosinfo] [osinfo-db 1/2] tests: Add a XML validation test

Fabiano Fidêncio fidencio at redhat.com
Mon Mar 18 21:28:23 UTC 2019


test_validation replaces the `xmllint` call from our Makefile.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 Makefile                 | 11 +----------
 tests/test_validation.py | 22 ++++++++++++++++++++++
 tests/util.py            | 13 ++++++++++++-
 3 files changed, 35 insertions(+), 11 deletions(-)
 create mode 100644 tests/test_validation.py

diff --git a/Makefile b/Makefile
index ca07fe3..f98993f 100644
--- a/Makefile
+++ b/Makefile
@@ -32,8 +32,6 @@ ARCHIVE = osinfo-db-$(TODAY).tar.xz
 
 ZANATA = zanata
 
-XMLLINT = xmllint
-
 PYTHON = python3
 
 V = 0
@@ -122,13 +120,6 @@ update-po:
           fi; \
         done
 
-lint: $(DATA_FILES) $(SCHEMA_FILES)
-	for xml in `find data -name '*.xml' | sort`; do \
-	  if ! $(XMLLINT) --relaxng data/schema/osinfo.rng --noout $$xml; then \
-	    exit 1; \
-	  fi; \
-	done
-
 unit-tests: $(DATA_FILES)
 	@command -v $(PYTHON) > /dev/null; \
 	if [ $$? -eq 0 ] ; then \
@@ -140,4 +131,4 @@ unit-tests: $(DATA_FILES)
 	  echo "unit-tests are not going to be executed as no $(PYTHON) has been found"; \
 	fi
 
-check: lint unit-tests
+check: unit-tests
diff --git a/tests/test_validation.py b/tests/test_validation.py
new file mode 100644
index 0000000..5234e5f
--- /dev/null
+++ b/tests/test_validation.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+
+import libxml2
+import pytest
+
+from . import util
+
+
+XMLS = util.xmls()
+SCHEMA = util.schema()
+PARSER = libxml2.relaxNGNewParserCtxt(SCHEMA)
+VALID = PARSER.relaxNGParse().relaxNGNewValidCtxt()
+
+def _file_id(_file):
+    return _file
+
+ at pytest.mark.parametrize('_file', [*XMLS], ids=_file_id)
+
+def test_validation(_file):
+    doc = libxml2.parseFile(_file)
+    assert VALID.relaxNGValidateDoc(doc) == 0
diff --git a/tests/util.py b/tests/util.py
index 48e7f7d..ed1ae68 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -6,7 +6,6 @@ import xml.etree.ElementTree as ET
 
 from . import osinfo
 
-
 def _get_files(directory):
     files = []
     datadir = os.environ.get('INTERNAL_OSINFO_DB_DATA_DIR')
@@ -35,3 +34,15 @@ def oses():
         for _file in files:
             _oses.append(osinfo.Os(_get_os(_file)))
     return _oses
+
+def xmls():
+    return _get_files('')
+
+def schema():
+    _schema = None
+    datadir = os.environ.get('INTERNAL_OSINFO_DB_DATA_DIR')
+    if datadir is not None:
+        _schema = os.path.join(datadir, 'schema', 'osinfo.rng')
+    else:
+        logging.error('INTERNAL_OSINFO_DB_DATA_DIR is not set')
+    return _schema
-- 
2.20.1




More information about the Libosinfo mailing list