[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libosinfo] [PATCH osinfo-db 12/15] tests: test_devices: check for device name comment



Previously I submitted this change:

    commit 415e3bec0502a0b396f6d4436ef9e64860fb08a8
    Author: Cole Robinson <crobinso redhat com>
    Date:   Tue Aug 28 15:22:32 2018 -0400

        os: Add a name comment with every <device> block

To add a comment after every os <device> reference containing the
devices string name. This helps a lot with readability and grepability
of the database XML.

This adds a testcase to enforce that pattern.

Signed-off-by: Cole Robinson <crobinso redhat com>
---
 tests/osinfo.py       | 15 +++++++++++++++
 tests/test_devices.py | 41 +++++++++++++++++++++++++++++++++++++++--
 tests/util.py         |  7 +++++++
 3 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/tests/osinfo.py b/tests/osinfo.py
index e17dc38..ad0eea2 100644
--- a/tests/osinfo.py
+++ b/tests/osinfo.py
@@ -200,3 +200,18 @@ class ISO(_XMLBase):
     @_cache_property
     def volumesize(self):
         return self._get_int('volume-size', default=0)
+
+
+class Device(_XMLBase):
+    def __init__(self, filename):
+        self.filename = filename
+        root = ET.parse(self.filename).getroot().find('device')
+        super().__init__(root)
+
+    @_cache_property
+    def internal_id(self):
+        return self._root.attrib["id"]
+
+    @_cache_property
+    def name(self):
+        return self._get_text('name')
diff --git a/tests/test_devices.py b/tests/test_devices.py
index ac66b78..7cecadc 100644
--- a/tests/test_devices.py
+++ b/tests/test_devices.py
@@ -1,11 +1,18 @@
 # This work is licensed under the GNU GPLv2 or later.
 # See the COPYING file in the top-level directory.
 
+import re
+
 from . import util
 
 
- util os_parametrize('osxml', filter_devices=True)
-def test_devices_duplication(osxml):
+DEVICE_MAP = {d.internal_id: d for d in util.DataFiles.devices()}
+
+
+def _check_duplicate_devices(osxml):
+    """
+    Ensure an OS doesn't list a device that's defined in the parent
+    """
     broken = []
     related = util.DataFiles.getosxml_related(osxml)
     for osxml2 in related:
@@ -14,3 +21,33 @@ def test_devices_duplication(osxml):
                 if device in osxml.devices:
                     broken.append(device)
     assert broken == []
+
+
+def _check_uncommented_devices(osxml):
+    """
+    Ensure every device listed in the XML is followed by a comment with
+    the device string name in it. This helps readability/grepability
+    """
+    badlines = []
+    sourcefile = osxml.filename + ".in"
+    devlines = [l for l in open(sourcefile).read().splitlines() if
+                "<device id" in l]
+
+    for devid in osxml.devices:
+        devname = DEVICE_MAP[devid].name
+        for devline in devlines:
+            if devid not in devline:
+                continue
+            if not re.search(r"<!--.*%s.*-->" % devname, devline):
+                badlines.append(devline)
+
+    if badlines:
+        raise AssertionError("shortid=%s device lines don't contain a "
+                "comment with the device name:\n%s" %
+                (osxml.shortid, badlines))
+
+
+ util os_parametrize('osxml', filter_devices=True)
+def test_devices_duplication(osxml):
+    _check_duplicate_devices(osxml)
+    _check_uncommented_devices(osxml)
diff --git a/tests/util.py b/tests/util.py
index 255a78d..c3dc5d6 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -35,6 +35,7 @@ class _DataFiles():
         self.schema = os.path.join(self.datadir, 'schema', 'osinfo.rng')
         self._all_xml_cache = []
         self._oses_cache = []
+        self._devices_cache = []
         self._os_related_cache = defaultdict(list)
 
         if not os.path.exists(self.datadir):
@@ -111,6 +112,12 @@ class _DataFiles():
                     self._os_related_cache[osxml.internal_id].append(osxml2)
         return self._os_related_cache[osxml.internal_id]
 
+    def devices(self):
+        if not self._devices_cache:
+            for path in self._filter_xml('device'):
+                self._devices_cache.append(osinfo.Device(path))
+        return self._devices_cache
+
     def xmls(self):
         return self._get_all_xml()
 
-- 
2.21.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]