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

[Libosinfo] [PATCH osinfo-db 08/15] tests: Sort input OS/files before testing



Right now we can get quite random ordering of test case running,
based on whatever order os.listdir() returns. This sorts filenames
and OS shortids with a number aware sort function which gives more
sensible pytest-3 -vv output

Signed-off-by: Cole Robinson <crobinso redhat com>
---
 tests/test_isoinfo.py |  3 ++-
 tests/util.py         | 17 ++++++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/tests/test_isoinfo.py b/tests/test_isoinfo.py
index 07d6550..8809126 100644
--- a/tests/test_isoinfo.py
+++ b/tests/test_isoinfo.py
@@ -19,7 +19,8 @@ def _get_isodatapaths():
         'isodata')
 
     ret = []
-    for osdir in glob.glob(os.path.join(isodata_path, "*", "*")):
+    allpaths = glob.glob(os.path.join(isodata_path, "*", "*"))
+    for osdir in sorted(allpaths, key=util.human_sort):
         osname = os.path.basename(osdir)
         isodatapaths = glob.glob(os.path.join(osdir, "*"))
         ret.append((osname, isodatapaths))
diff --git a/tests/util.py b/tests/util.py
index bf04b74..a2e88f6 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -4,6 +4,7 @@
 from collections import defaultdict
 
 import os
+import re
 import xml.etree.ElementTree as ET
 
 import pytest
@@ -11,6 +12,20 @@ import pytest
 from . import osinfo
 
 
+def human_sort(text):
+    # natural/human sorting
+    # https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside
+    def atof(t):
+        try:
+            retval = float(t)
+        except ValueError:
+            retval = t
+        return retval
+
+    return [atof(c) for c in
+            re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text)]
+
+
 class _DataFiles():
     """
     Track a list of DATA_DIR XML files and provide APIs for querying
@@ -33,7 +48,7 @@ class _DataFiles():
         """
         if not self._all_xml_cache:
             for (dirpath, _, filenames) in os.walk(self.datadir):
-                for filename in filenames:
+                for filename in sorted(filenames, key=human_sort):
                     if not filename.endswith('.xml'):
                         continue
                     self._all_xml_cache.append(os.path.join(dirpath, filename))
-- 
2.21.0


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