[PATCH osinfo-db 05/10] tests: util: Add DataFiles class

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

 



Use this class to hold all the file lookup helper routines, everything
currently in util. Init one global instance and use that in the test
cases. This will make it easier to cache lookup results as well

Make sure this is initialized from conftest, so if we hit any errors
it will bail out before running any tests. Use this to validate we
have a valid DATA_DIR too

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 tests/conftest.py        |  4 +++
 tests/test_isoinfo.py    |  2 +-
 tests/test_urls.py       |  2 +-
 tests/test_validation.py |  4 +--
 tests/util.py            | 63 +++++++++++++++++-----------------------
 5 files changed, 35 insertions(+), 40 deletions(-)

diff --git a/tests/conftest.py b/tests/conftest.py
index ecc0813..8bd7236 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -12,3 +12,7 @@ def _setup_env():
 
 
 _setup_env()
+
+# This will trigger some DATA_DIR validation
+from . import util
+dummy = util
diff --git a/tests/test_isoinfo.py b/tests/test_isoinfo.py
index 189540b..7e3f7b0 100644
--- a/tests/test_isoinfo.py
+++ b/tests/test_isoinfo.py
@@ -9,7 +9,7 @@ import pytest
 from . import util
 
 
-OSES = util.oses()
+OSES = util.DataFiles.oses()
 
 
 def _os_id(_os):
diff --git a/tests/test_urls.py b/tests/test_urls.py
index 9c8affa..2429e5c 100644
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -7,7 +7,7 @@ import pytest
 from . import util
 
 
-OSES = util.oses()
+OSES = util.DataFiles.oses()
 
 
 def _os_id(_os):
diff --git a/tests/test_validation.py b/tests/test_validation.py
index fe07374..ec9fbec 100755
--- a/tests/test_validation.py
+++ b/tests/test_validation.py
@@ -7,8 +7,8 @@ import pytest
 from . import util
 
 
-XMLS = util.xmls()
-SCHEMA = util.schema()
+XMLS = util.DataFiles.xmls()
+SCHEMA = util.DataFiles.schema
 PARSER = libxml2.relaxNGNewParserCtxt(SCHEMA)
 VALID = PARSER.relaxNGParse().relaxNGNewValidCtxt()
 
diff --git a/tests/util.py b/tests/util.py
index 4d2e279..15ec3cc 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -1,54 +1,45 @@
 # This work is licensed under the GNU GPLv2 or later.
 # See the COPYING file in the top-level directory.
 
-import logging
 import os
 import xml.etree.ElementTree as ET
 
 from . import osinfo
 
 
-def _get_files(directory):
-    files = []
-    datadir = os.environ.get('INTERNAL_OSINFO_DB_DATA_DIR')
-    if datadir is not None:
-        root = os.path.join(datadir, directory)
+class _DataFiles():
+    """
+    Track a list of DATA_DIR XML files and provide APIs for querying
+    them. Meant to be initialized only once
+    """
+    def __init__(self):
+        self.datadir = os.environ['INTERNAL_OSINFO_DB_DATA_DIR']
+        self.schema = os.path.join(self.datadir, 'schema', 'osinfo.rng')
+
+        if not os.path.exists(self.datadir):
+            raise RuntimeError("INTERNAL_OSINFO_DB_DATA_DIR=%s "
+                "doesn't exist" % self.datadir)
+
+    def _get_files(self, directory):
+        files = []
+        root = os.path.join(self.datadir, directory)
         for (dirpath, _, filenames) in os.walk(root):
             for filename in filenames:
                 if not filename.endswith('.xml'):
                     continue
                 files.append(os.path.join(dirpath, filename))
-    else:
-        logging.error('INTERNAL_OSINFO_DB_DATA_DIR is not set')
-    return files
-
-
-def _get_os(path):
-    tree = ET.parse(path)
-    root = tree.getroot()
-
-    _os = root.find('os')
-    return _os
-
-
-def oses():
-    _oses = []
-    files = _get_files('os')
-    if files:
-        for _file in files:
-            _oses.append(osinfo.Os(_get_os(_file)))
-    return _oses
+        return files
 
+    def oses(self):
+        ret = []
+        files = self._get_files('os')
+        for path in files:
+            osroot = ET.parse(path).getroot().find('os')
+            ret.append(osinfo.Os(osroot))
+        return ret
 
-def xmls():
-    return _get_files('')
+    def xmls(self):
+        return self._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
+DataFiles = _DataFiles()
-- 
2.21.0

_______________________________________________
Libosinfo mailing list
Libosinfo@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libosinfo



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux