|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run CodeQL tests with appropriate configuration. |
| 3 | +
|
| 4 | +Called from just recipes as: |
| 5 | + python3 codeql_test_run.py LANGUAGE BASE_FLAGS ALL_CHECKS_FLAGS EXTRA_ARGS |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import re |
| 10 | +import shlex |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +JUST = os.environ.get("JUST_EXECUTABLE", "just") |
| 16 | +ERROR = os.environ.get("JUST_ERROR", "error: ") |
| 17 | +CMD_BEGIN = os.environ.get("CMD_BEGIN", "") |
| 18 | +CMD_END = os.environ.get("CMD_END", "") |
| 19 | +SEMMLE_CODE = os.environ.get("SEMMLE_CODE") |
| 20 | + |
| 21 | + |
| 22 | +def invoke(invocation, *, cwd=None, log_prefix=""): |
| 23 | + prefix = f"{log_prefix} " if log_prefix else "" |
| 24 | + print(f"{CMD_BEGIN}{prefix}{' '.join(invocation)}{CMD_END}") |
| 25 | + try: |
| 26 | + subprocess.run(invocation, check=True, cwd=cwd) |
| 27 | + except subprocess.CalledProcessError as e: |
| 28 | + return e.returncode |
| 29 | + return 0 |
| 30 | + |
| 31 | + |
| 32 | +def error(message): |
| 33 | + print(f"{ERROR}{message}", file=sys.stderr) |
| 34 | + |
| 35 | + |
| 36 | +ENV_RE = re.compile(r"^[A-Z_][A-Z_0-9]*=.*$") |
| 37 | + |
| 38 | + |
| 39 | +def parse_args(args, argv_str): |
| 40 | + """Parse a space-separated argument string into categorized arguments.""" |
| 41 | + for arg in shlex.split(argv_str): |
| 42 | + if arg.startswith("--codeql="): |
| 43 | + args["codeql"] = arg.split("=", 1)[1] |
| 44 | + elif arg in ("+", "--all-checks"): |
| 45 | + args["all"] = True |
| 46 | + elif arg.startswith("-"): |
| 47 | + args["flags"].append(arg) |
| 48 | + elif ENV_RE.match(arg): |
| 49 | + args["env"].append(arg) |
| 50 | + elif arg: |
| 51 | + args["tests"].append(arg) |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + argv = sys.argv[1:] |
| 56 | + if len(argv) < 4: |
| 57 | + error( |
| 58 | + "Usage: codeql_test_run.py LANGUAGE BASE_FLAGS ALL_CHECKS_FLAGS EXTRA_ARGS" |
| 59 | + ) |
| 60 | + return 1 |
| 61 | + |
| 62 | + language, base_args, all_args, extra_args = argv[0], argv[1], argv[2], argv[3] |
| 63 | + ram_per_thread = 3000 if sys.platform == "linux" else 2048 |
| 64 | + cpus = os.cpu_count() or 1 |
| 65 | + |
| 66 | + args = { |
| 67 | + "tests": [], |
| 68 | + "flags": [f"--ram={ram_per_thread * cpus}", f"-j{cpus}"], |
| 69 | + "env": [], |
| 70 | + "codeql": "build" if SEMMLE_CODE else "host", |
| 71 | + "all": False, |
| 72 | + } |
| 73 | + parse_args(args, base_args) |
| 74 | + parse_args(args, extra_args) |
| 75 | + if args["all"]: |
| 76 | + parse_args(args, all_args) |
| 77 | + |
| 78 | + if not SEMMLE_CODE and args["codeql"] in ("build", "built"): |
| 79 | + error( |
| 80 | + "Using `--codeql=build` or `--codeql=built` requires working " |
| 81 | + "with the internal repository" |
| 82 | + ) |
| 83 | + return 1 |
| 84 | + |
| 85 | + if not args["tests"]: |
| 86 | + args["tests"].append(".") |
| 87 | + |
| 88 | + if args["codeql"] == "build": |
| 89 | + if invoke([JUST, language, "build"], cwd=SEMMLE_CODE) != 0: |
| 90 | + return 1 |
| 91 | + |
| 92 | + if args["codeql"] != "host": |
| 93 | + # Disable the default implicit config file, but keep an explicit one. |
| 94 | + # Same behavior wrt --codeql as the integration test runner. |
| 95 | + os.environ.setdefault("CODEQL_CONFIG_FILE", ".") |
| 96 | + |
| 97 | + for env_var in args["env"]: |
| 98 | + key, _, value = env_var.partition("=") |
| 99 | + if not key: |
| 100 | + error(f"Invalid environment variable assignment: {env_var}") |
| 101 | + return 1 |
| 102 | + os.environ[key] = value |
| 103 | + |
| 104 | + # Resolve codeql executable |
| 105 | + if args["codeql"] in ("built", "build"): |
| 106 | + codeql = Path(SEMMLE_CODE, "target", "intree", f"codeql-{language}", "codeql") |
| 107 | + if not codeql.exists(): |
| 108 | + error(f"CodeQL executable not found: {codeql}") |
| 109 | + return 1 |
| 110 | + elif args["codeql"] == "host": |
| 111 | + codeql = Path("codeql") |
| 112 | + else: |
| 113 | + codeql = Path(args["codeql"]) |
| 114 | + if not codeql.exists(): |
| 115 | + error(f"CodeQL executable not found: {codeql}") |
| 116 | + return 1 |
| 117 | + |
| 118 | + if codeql.is_dir(): |
| 119 | + codeql = codeql / "codeql" |
| 120 | + if sys.platform == "win32": |
| 121 | + codeql = codeql.with_suffix(".exe") |
| 122 | + if not codeql.exists(): |
| 123 | + error(f"CodeQL executable not found: {codeql}") |
| 124 | + return 1 |
| 125 | + |
| 126 | + return invoke( |
| 127 | + [str(codeql), "test", "run", *args["flags"], "--", *args["tests"]], |
| 128 | + log_prefix=" ".join(args["env"]), |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + try: |
| 134 | + sys.exit(main()) |
| 135 | + except KeyboardInterrupt: |
| 136 | + sys.exit(128 + 2) |
0 commit comments