Files
codeql/swift/ql/integration-tests/runner.py
Paolo Tranquilli 762b4ce42e Swift: prepare integration tests for internal running
This harmonizes Swift integration tests with the rest of the repository,
to prepare for the internal integration test runner to run them. The
stripped down runner is kept compatible, so that current CI can still
use it now. Maybe it will be kept for developer use.

This PR includes:
* moving the integration tests inside `ql`
* editing `qlpack.yml` so that the internal runner can use it
* change database directory to be `test-db` rather than `db`
2024-03-25 10:17:55 +01:00

92 lines
2.7 KiB
Python
Executable File

#!/bin/env python3
"""
recreation of internal `integration-tests-runner.py` to run the tests locally, with minimal
and swift-specialized functionality.
This runner requires:
* a codeql CLI binary in PATH
* `bazel run //swift:create_extractor_pack` to have been run
"""
import pathlib
import os
import sys
import subprocess
import argparse
import shutil
import platform
this_dir = pathlib.Path(__file__).parent.resolve()
codeql_root = this_dir.parents[2]
def options():
p = argparse.ArgumentParser()
p.add_argument("--test-dir", "-d", type=pathlib.Path, action="append")
# FIXME: the following should be the default
p.add_argument("--check-databases", action="store_true")
p.add_argument("--learn", action="store_true")
p.add_argument("--threads", "-j", type=int, default=0)
p.add_argument("--compilation-cache")
return p.parse_args()
def execute_test(path):
shutil.rmtree(path.parent / "test-db", ignore_errors=True)
return subprocess.run([sys.executable, "-u", path.name], cwd=path.parent).returncode == 0
def skipped(test):
if platform.system() == "Darwin":
return "linux-only" in test.parts
else:
return "osx-only" in test.parts
def main(opts):
test_dirs = opts.test_dir or [this_dir]
tests = [t for d in test_dirs for t in d.rglob("test.py") if not skipped(t)]
if opts.learn:
os.environ["CODEQL_INTEGRATION_TEST_LEARN"] = "true"
if not tests:
print("No tests found", file=sys.stderr)
return False
os.environ["PYTHONPATH"] = str(this_dir)
os.environ["CODEQL_CONFIG_FILE"] = "/dev/null"
failed_db_creation = []
succesful_db_creation = []
for t in tests:
(succesful_db_creation if execute_test(t) else failed_db_creation).append(t)
if succesful_db_creation:
cmd = [
"codeql", "test", "run",
f"--additional-packs={codeql_root}",
"--keep-databases",
"--no-cleanup",
"--dataset=test-db/db-swift",
f"--threads={opts.threads}",
]
if opts.check_databases:
cmd.append("--check-databases")
else:
cmd.append("--no-check-databases")
if opts.learn:
cmd.append("--learn")
if opts.compilation_cache:
cmd.append(f'--compilation-cache="{opts.compilation_cache}"')
cmd.extend(str(t.parent) for t in succesful_db_creation)
ql_test_success = subprocess.run(cmd).returncode == 0
if failed_db_creation:
print("Database creation failed:", file=sys.stderr)
for t in failed_db_creation:
print(" ", t.parent, file=sys.stderr)
return False
return ql_test_success
if __name__ == "__main__":
sys.exit(0 if main(options()) else 1)