diff --git a/scripts/ci_backend_test_labels.py b/scripts/ci_backend_test_labels.py index 701988f4..fb2a8717 100644 --- a/scripts/ci_backend_test_labels.py +++ b/scripts/ci_backend_test_labels.py @@ -1,17 +1,40 @@ #!/usr/bin/env python3 -"""Map changed repository paths to Django test package labels for CI.""" +"""Map changed repository paths to Django test package labels for CI. + +Loads ``dispatcharr/test_discovery.py`` by file path so this script does not +import the ``dispatcharr`` package (which eagerly loads Celery). The label step +runs on the bare runner Python before the app venv exists. +""" from __future__ import annotations +import importlib.util import json import os import sys from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent -sys.path.insert(0, str(REPO_ROOT)) -from dispatcharr.test_discovery import labels_for_changed_paths # noqa: E402 + +def _load_test_discovery(): + path = REPO_ROOT / "dispatcharr" / "test_discovery.py" + spec = importlib.util.spec_from_file_location( + "dispatcharr_test_discovery", + path, + ) + if spec is None or spec.loader is None: + raise ImportError(f"Unable to load test discovery module from {path}") + module = importlib.util.module_from_spec(spec) + # Register before exec so dataclasses/typing edge cases that re-import + # the module name still resolve. + sys.modules[spec.name] = module + spec.loader.exec_module(module) + return module + + +_test_discovery = _load_test_discovery() +labels_for_changed_paths = _test_discovery.labels_for_changed_paths def _read_paths(argv: list[str]) -> list[str]: