]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kunit: tool: readability tweaks in KernelCI json generation logic
authorDaniel Latypov <dlatypov@google.com>
Thu, 24 Feb 2022 19:20:34 +0000 (11:20 -0800)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 4 Apr 2022 21:22:15 +0000 (15:22 -0600)
Use a more idiomatic check that a list is non-empty (`if mylist:`) and
simplify the function body by dedenting and using a dict to map between
the kunit TestStatus enum => KernelCI json status string.

The dict hopefully makes it less likely to have bugs like commit
9a6bb30a8830 ("kunit: tool: fix --json output for skipped tests").

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/kunit/kunit_json.py

index 24d103049bcaaccf0886d762c5d3844804d2f8f7..14a480d3308a974837bcca92a716090b6041b202 100644 (file)
@@ -16,24 +16,24 @@ from typing import Any, Dict
 
 JsonObj = Dict[str, Any]
 
+_status_map: Dict[TestStatus, str] = {
+       TestStatus.SUCCESS: "PASS",
+       TestStatus.SKIPPED: "SKIP",
+       TestStatus.TEST_CRASHED: "ERROR",
+}
+
 def _get_group_json(test: Test, def_config: str, build_dir: str) -> JsonObj:
        sub_groups = []  # List[JsonObj]
        test_cases = []  # List[JsonObj]
 
        for subtest in test.subtests:
-               if len(subtest.subtests):
+               if subtest.subtests:
                        sub_group = _get_group_json(subtest, def_config,
                                build_dir)
                        sub_groups.append(sub_group)
-               else:
-                       test_case = {"name": subtest.name, "status": "FAIL"}
-                       if subtest.status == TestStatus.SUCCESS:
-                               test_case["status"] = "PASS"
-                       elif subtest.status == TestStatus.SKIPPED:
-                               test_case["status"] = "SKIP"
-                       elif subtest.status == TestStatus.TEST_CRASHED:
-                               test_case["status"] = "ERROR"
-                       test_cases.append(test_case)
+                       continue
+               status = _status_map.get(subtest.status, "FAIL")
+               test_cases.append({"name": subtest.name, "status": status})
 
        test_group = {
                "name": test.name,