file -> suites defined there, test -> suites running it, suite universe, and the resolver from any suite to the registered suites that run it.
(root)
| 42 | |
| 43 | |
| 44 | def suitemap(root): |
| 45 | """file -> suites defined there, test -> suites running it, suite universe, |
| 46 | and the resolver from any suite to the registered suites that run it.""" |
| 47 | file_suites, test_suites, sources, parents = {}, {}, {}, {} |
| 48 | for directory, dirs, names in os.walk(os.path.join(root, "tests")): |
| 49 | dirs[:] = sorted(d for d in dirs if not (directory == os.path.join(root, "tests") |
| 50 | and d == "fixtures")) |
| 51 | for name in sorted(names): |
| 52 | if not name.endswith(".c"): |
| 53 | continue |
| 54 | path = os.path.join(directory, name) |
| 55 | rel = os.path.relpath(path, root).replace(os.sep, "/") |
| 56 | with open(path, encoding="utf-8", errors="replace") as fh: |
| 57 | src = fh.read() |
| 58 | sources[rel] = src |
| 59 | for m in SUITE_DEF.finditer(src): |
| 60 | suite = m.group(1) or m.group(2) |
| 61 | start = depth = m.end() - 1 |
| 62 | end = start |
| 63 | depth = 0 |
| 64 | while end < len(src): # the body is the balanced { } block |
| 65 | if src[end] == "{": |
| 66 | depth += 1 |
| 67 | elif src[end] == "}": |
| 68 | depth -= 1 |
| 69 | if depth == 0: |
| 70 | break |
| 71 | end += 1 |
| 72 | file_suites.setdefault(rel, []).append(suite) |
| 73 | for test in RUN_TEST.findall(src[start:end]): |
| 74 | test_suites.setdefault(test, []) |
| 75 | if suite not in test_suites[test]: |
| 76 | test_suites[test].append(suite) |
| 77 | for child in RUN_SUITE.findall(src[start:end]): |
| 78 | parents.setdefault(child, set()).add(suite) |
| 79 | main = sources.get("tests/test_main.c", "") |
| 80 | registered = set(re.findall(r"RUN_SELECTED_SUITE(?:_PERF)?\((\w+)\)", main)) |
| 81 | universe = registered - set(re.findall(r"RUN_SELECTED_SUITE_PERF\((\w+)\)", main)) |
| 82 | |
| 83 | def runnable(suite, seen=frozenset()): |
| 84 | """A registered suite runs itself; a nested one runs inside its parents.""" |
| 85 | if suite in registered: |
| 86 | return {suite} & universe |
| 87 | out = set() |
| 88 | for parent in parents.get(suite, ()): |
| 89 | if parent not in seen: |
| 90 | out |= runnable(parent, seen | {suite}) |
| 91 | return out |
| 92 | return file_suites, test_suites, universe, sources, runnable |
| 93 | |
| 94 | |
| 95 | def predict(root, changed, selection, detect, depth): |