|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import platform |
| 7 | +import shutil |
| 8 | +import sys |
| 9 | +from datetime import datetime |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +HERE = Path(__file__).absolute().parent |
| 15 | +ROOT = HERE.parent.parent |
| 16 | +AUTH = os.environ.get("AUTH", "noauth") |
| 17 | +SSL = os.environ.get("SSL", "nossl") |
| 18 | +UV_ARGS = os.environ.get("UV_ARGS", "") |
| 19 | +TEST_PERF = os.environ.get("TEST_PERF") |
| 20 | +GREEN_FRAMEWORK = os.environ.get("GREEN_FRAMEWORK") |
| 21 | +TEST_ARGS = os.environ.get("TEST_ARGS", "").split() |
| 22 | + |
| 23 | +LOGGER = logging.getLogger(__name__) |
| 24 | +logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") |
| 25 | + |
| 26 | + |
| 27 | +def handle_perf(start_time: datetime): |
| 28 | + end_time = datetime.now() |
| 29 | + elapsed_secs = (end_time - start_time).total_seconds() |
| 30 | + with open("results.json") as fid: |
| 31 | + results = json.load(fid) |
| 32 | + LOGGER.info("results.json:\n%s", json.dumps(results, indent=2)) |
| 33 | + |
| 34 | + results = dict( |
| 35 | + status="PASS", |
| 36 | + exit_code=0, |
| 37 | + test_file="BenchMarkTests", |
| 38 | + start=int(start_time.timestamp()), |
| 39 | + end=int(end_time.timestamp()), |
| 40 | + elapsed=elapsed_secs, |
| 41 | + ) |
| 42 | + report = dict(failures=0, results=[results]) |
| 43 | + LOGGER.info("report.json\n%s", json.dumps(report, indent=2)) |
| 44 | + |
| 45 | + with open("report.json", "w", newline="\n") as fid: |
| 46 | + json.dump(report, fid) |
| 47 | + |
| 48 | + |
| 49 | +def handle_green_framework() -> None: |
| 50 | + if GREEN_FRAMEWORK == "eventlet": |
| 51 | + import eventlet |
| 52 | + |
| 53 | + # https://github.com/eventlet/eventlet/issues/401 |
| 54 | + eventlet.sleep() |
| 55 | + eventlet.monkey_patch() |
| 56 | + elif GREEN_FRAMEWORK == "gevent": |
| 57 | + from gevent import monkey |
| 58 | + |
| 59 | + monkey.patch_all() |
| 60 | + |
| 61 | + # Never run async tests with a framework. |
| 62 | + if len(TEST_ARGS) <= 1: |
| 63 | + TEST_ARGS.extend(["-m", "not default_async and default"]) |
| 64 | + else: |
| 65 | + for i in range(len(TEST_ARGS) - 1): |
| 66 | + if "-m" in TEST_ARGS[i]: |
| 67 | + TEST_ARGS[i + 1] = f"not default_async and {TEST_ARGS[i + 1]}" |
| 68 | + |
| 69 | + LOGGER.info(f"Running tests with {GREEN_FRAMEWORK}...") |
| 70 | + |
| 71 | + |
| 72 | +def handle_c_ext() -> None: |
| 73 | + if platform.python_implementation() != "CPython": |
| 74 | + return |
| 75 | + sys.path.insert(0, str(ROOT / "tools")) |
| 76 | + from fail_if_no_c import main as fail_if_no_c |
| 77 | + |
| 78 | + fail_if_no_c() |
| 79 | + |
| 80 | + |
| 81 | +def handle_pymongocrypt() -> None: |
| 82 | + import pymongocrypt |
| 83 | + |
| 84 | + LOGGER.info(f"pymongocrypt version: {pymongocrypt.__version__})") |
| 85 | + LOGGER.info(f"libmongocrypt version: {pymongocrypt.libmongocrypt_version()})") |
| 86 | + |
| 87 | + |
| 88 | +def run() -> None: |
| 89 | + # Handle green framework first so they can patch modules. |
| 90 | + if GREEN_FRAMEWORK: |
| 91 | + handle_green_framework() |
| 92 | + |
| 93 | + # Ensure C extensions if applicable. |
| 94 | + if not os.environ.get("NO_EXT"): |
| 95 | + handle_c_ext() |
| 96 | + |
| 97 | + if os.environ.get("PYMONGOCRYPT_LIB"): |
| 98 | + handle_pymongocrypt() |
| 99 | + |
| 100 | + LOGGER.info(f"Test setup:\n{AUTH=}\n{SSL=}\n{UV_ARGS=}\n{TEST_ARGS=}") |
| 101 | + |
| 102 | + # Record the start time for a perf test. |
| 103 | + if TEST_PERF: |
| 104 | + start_time = datetime.now() |
| 105 | + |
| 106 | + # Run the tests. |
| 107 | + pytest.main(TEST_ARGS) |
| 108 | + |
| 109 | + # Handle perf test post actions. |
| 110 | + if TEST_PERF: |
| 111 | + handle_perf(start_time) |
| 112 | + |
| 113 | + # Handle coverage post actions. |
| 114 | + if os.environ.get("COVERAGE"): |
| 115 | + shutil.rmtree(".pytest_cache", ignore_errors=True) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + run() |
0 commit comments