]> xenbits.xensource.com Git - people/andrewcoop/xen-test-framework.git/commitdiff
runner: Use context managers to simplify get_all_test_info()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 5 Jan 2024 20:15:49 +0000 (20:15 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 8 Jan 2024 14:40:12 +0000 (14:40 +0000)
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
xtf-runner

index b6f26fd6c28d7130fd5bd44bccf42147324a1153..aa4fda399096ca0c670c76247a218e2fa0392fbd 100755 (executable)
@@ -240,42 +240,27 @@ def parse_test_instance_string(arg):
     return env, name, variation
 
 
-# Cached test json from disk
+# Cached data from tests/*/info.json
 _all_test_info = {}
 
 def get_all_test_info():
     """ Open and collate each info.json """
+    if not _all_test_info: # Cache on first request
 
-    # Short circuit if already cached
-    if _all_test_info:
-        return _all_test_info
-
-    for test in os.listdir("tests"):
-
-        info_file = None
-        try:
-
-            # Ignore directories which don't have a info.json inside them
-            try:
-                info_file = open(path.join("tests", test, "info.json"))
-            except IOError:
-                continue
-
-            # Ignore tests which have bad JSON
+        for test in os.listdir("tests"):
             try:
-                test_info = TestInfo(json.load(info_file))
+                with open(path.join("tests", test, "info.json")) as f:
 
-                if test_info.name != test:
-                    continue
+                    info = TestInfo(json.load(f))
 
-            except (ValueError, KeyError, TypeError):
-                continue
+                    if info.name != test:
+                        raise ValueError # JSON also looks bad
 
-            _all_test_info[test] = test_info
+                    _all_test_info[test] = info
 
-        finally:
-            if info_file:
-                info_file.close()
+            except (IOError, # Ignore directories without a info.json
+                    ValueError, KeyError, TypeError): # Ingore bad JSON
+                continue
 
     return _all_test_info