|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from ..check import Check |
| 4 | + |
| 5 | + |
| 6 | +def load_module_2(dirname, module_name): |
| 7 | + import imp |
| 8 | + |
| 9 | + f = None |
| 10 | + try: |
| 11 | + (f, pathname, description) = imp.find_module(module_name, [dirname]) |
| 12 | + module = imp.load_module(module_name, f, pathname, description) |
| 13 | + return module |
| 14 | + except: |
| 15 | + raise ImportError("Can't load " + module_name) |
| 16 | + finally: |
| 17 | + if f: |
| 18 | + f.close() |
| 19 | + |
| 20 | + |
| 21 | +def load_module_3(dirname, module_name): |
| 22 | + import importlib.machinery |
| 23 | + import importlib.util |
| 24 | + |
| 25 | + # Create a FileFinder to load Python source files. |
| 26 | + loader_details = ( |
| 27 | + importlib.machinery.SourceFileLoader, |
| 28 | + importlib.machinery.SOURCE_SUFFIXES, |
| 29 | + ) |
| 30 | + finder = importlib.machinery.FileFinder(dirname, loader_details) |
| 31 | + |
| 32 | + # Find the module spec. |
| 33 | + spec = finder.find_spec(module_name) |
| 34 | + if spec is None: |
| 35 | + raise RuntimeError("Can't load " + module_name) |
| 36 | + |
| 37 | + # Create and execute the module. |
| 38 | + module = importlib.util.module_from_spec(spec) |
| 39 | + spec.loader.exec_module(module) |
| 40 | + |
| 41 | + # Return the executed module |
| 42 | + return module |
| 43 | + |
| 44 | + |
| 45 | +def load_parse_test_record(): |
| 46 | + checks_dir = os.path.dirname(os.path.realpath(__file__)) |
| 47 | + packing_dir = os.path.join(checks_dir, "../../../packaging") |
| 48 | + module_name = "parseTestRecord" |
| 49 | + |
| 50 | + # The "imp" module is deprecated in Python 3.4 and will be removed in |
| 51 | + # Python 3.12. Use it only if the current Python version is too old to use |
| 52 | + # the "importlib" module. |
| 53 | + if sys.version_info < (3, 4): |
| 54 | + return load_module_2(packing_dir, module_name) |
| 55 | + |
| 56 | + return load_module_3(packing_dir, module_name) |
| 57 | + |
| 58 | + |
| 59 | +parse_test_record = load_parse_test_record().parseTestRecord |
| 60 | + |
| 61 | + |
| 62 | +class CheckParseTestRecord(Check): |
| 63 | + '''Ensure tests can be parsed using parseTestRecord.py''' |
| 64 | + ID = 'ParseTestRecord' |
| 65 | + |
| 66 | + def run(self, name, meta, source): |
| 67 | + # Skip if not a test file. |
| 68 | + if not meta: |
| 69 | + return None |
| 70 | + |
| 71 | + errors = [] |
| 72 | + test_rec = parse_test_record(source, name, lambda e: errors.append(e)) |
| 73 | + |
| 74 | + # Return if parse_test_record encountered errors. |
| 75 | + if errors: |
| 76 | + return "\n".join(errors) |
| 77 | + |
| 78 | + # Ensure all flags in `test_rec` are consistent with `meta`. |
| 79 | + if "flags" in meta: |
| 80 | + if "flags" not in test_rec: |
| 81 | + return "Flags not present in parseTestRecord" |
| 82 | + |
| 83 | + if meta["flags"] != test_rec["flags"]: |
| 84 | + return "Flags don't match parseTestRecord" |
| 85 | + |
| 86 | + for flag in meta["flags"]: |
| 87 | + if flag not in test_rec: |
| 88 | + return "Flag not present in parseTestRecord: " + flag |
| 89 | + elif "flags" in test_rec: |
| 90 | + return "Unexpected flags present in parseTestRecord" |
| 91 | + |
| 92 | + return None |
0 commit comments