From: Andrew Cooper Date: Fri, 5 Jan 2024 20:15:49 +0000 (+0000) Subject: runner: Use context managers to simplify get_all_test_info() X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=67a2f2154c143ba4b572c53e1c26aa5cab45f644;p=people%2Fandrewcoop%2Fxen-test-framework.git runner: Use context managers to simplify get_all_test_info() Signed-off-by: Andrew Cooper --- diff --git a/xtf-runner b/xtf-runner index b6f26fd..aa4fda3 100755 --- a/xtf-runner +++ b/xtf-runner @@ -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