From 00c58dc4e45cbe8286d491f3d9cae9af4a7d5727 Mon Sep 17 00:00:00 2001 From: Tobias Brox Date: Sat, 24 Oct 2026 15:20:20 +0200 Subject: [PATCH] feat: ship the test-server registry in the package The caldav source code has rather a lot of testing logic, including allowing ordinary configuration files to enable testing towards the configured servers, allowing special test server configuration files to be read, and also start/stop-scripts that can instantiate docker test servers if docker or podman is installed. There is a legitimate need for testing other packages (like plann, calendar-cli, calendaring, calendaring-sync) towards a wide range of test servers, hence we're bringing the test server registry logic out as a public part of the package. Arguably it would better belong in a separate package (or in the caldav-server-tester package). As for now the costs of spawning Yet Another Package have been considered bigger than the costs of adding some few kilobytes of pure-Python logic that ordinary users of the caldav package do not need - but this may be considered in the future - filed as https://github.com/python-caldav/caldav/issues/823 The registry, config loader, locks and the embedded, Docker and external server classes moved from tests/test_servers into the installed caldav.testing package, so other projects can use them without a source checkout. tests/test_servers re-exports them, with each submodule aliased to its new home so existing imports and monkeypatching keep working. The docker-compose files stay in the repository; CALDAV_DOCKER_TEST_SERVERS_DIR names them when caldav is installed rather than checked out. get_davclient() on a test server now uses that registry on an installed caldav too, in place of the embedded-plus-test_allowed fallback; a config file named explicitly or through CALDAV_CONFIG_FILE still adds its test_allowed sections. https://github.com/python-caldav/caldav/issues/817 Prompt: The other session reports that #817, closed by #820, has only been partially done. Please investigate [#817: move the test-server registry into the installed caldav.testing; #820 merged only the follow-up comment] Followup-Prompt: Please just start working on a new PR instead [rather than commenting on #817] Prompts (comments made inline in the document while reviewing it): > (on this commit message, after rewriting its first paragraphs) Please do another QA of my editing here Assisted-By: Claude Sonnet 5.5 Assisted-By: Claude Opus 5.5 (1M context) --- caldav/config.py | 77 ++++++------------- caldav/{testing.py => testing/__init__.py} | 12 +-- .../test_servers => caldav/testing}/base.py | 24 ++++-- .../testing}/config_loader.py | 2 +- .../test_servers => caldav/testing}/docker.py | 6 +- .../testing}/embedded.py | 0 .../testing}/helpers.py | 2 +- .../testing}/registry.py | 11 ++- changes/+testing-registry-in-package.added.md | 1 + docs/design/TODO.md | 6 +- pyproject.toml | 6 +- tests/README.md | 26 +++++-- tests/test_servers/__init__.py | 21 ++++- .../test_base.py => test_testing_base.py} | 71 ++++++++++++++++- ...oader.py => test_testing_config_loader.py} | 30 ++++---- 15 files changed, 182 insertions(+), 113 deletions(-) rename caldav/{testing.py => testing/__init__.py} (96%) rename {tests/test_servers => caldav/testing}/base.py (96%) rename {tests/test_servers => caldav/testing}/config_loader.py (98%) rename {tests/test_servers => caldav/testing}/docker.py (99%) rename {tests/test_servers => caldav/testing}/embedded.py (100%) rename {tests/test_servers => caldav/testing}/helpers.py (98%) rename {tests/test_servers => caldav/testing}/registry.py (98%) create mode 100644 changes/+testing-registry-in-package.added.md rename tests/{test_servers/test_base.py => test_testing_base.py} (79%) rename tests/{test_servers/test_config_loader.py => test_testing_config_loader.py} (92%) diff --git a/caldav/config.py b/caldav/config.py index eca999208..4735a82de 100644 --- a/caldav/config.py +++ b/caldav/config.py @@ -364,7 +364,7 @@ def _caldav_connections( ## TODO: the test server selection below, get_test_setting and ## get_all_test_servers are test tooling, not config; move them to -## caldav/testing.py (planned for 3.6.1). +## the caldav.testing package. def _get_test_server_config( name: str | None, environment: bool, config_file: str | None = None ) -> dict[str, Any] | None: @@ -376,15 +376,15 @@ def _get_test_server_config( (20) and configured / test_allowed servers (30) by default. Any server can override its priority via ``test_priority: `` in its config. - When running from the source tree the full registry is used (embedded + - docker + external). When caldav is pip-installed without the test - infrastructure, only embedded servers from ``caldav.testing`` plus any - ``test_allowed`` config-file sections are available. + The servers are those of the ``caldav.testing`` registry (embedded, + docker and external), plus the ``test_allowed`` sections of + ``config_file``. Args: name: Optional server name or server_type to restrict selection. - environment: Whether to read PYTHON_CALDAV_TEST_SERVER_NAME. - config_file: Explicit config file path (for test_allowed fallback). + environment: Whether to read PYTHON_CALDAV_TEST_SERVER_NAME, and to + find the config file through CALENDARING_CONFIG_FILE / CALDAV_CONFIG_FILE. + config_file: Explicit config file path, for its test_allowed sections. Returns: Connection parameters dict, or None if no server could be started. @@ -401,7 +401,7 @@ def _get_test_server_config( for server in _collect_test_servers(name, config_file): was_already_started = server._started - # Servers from caldav.testing (pip-installed caldav) have no session lock + # A _ConfiguredServer has no session lock claim = getattr(server, "claim_server", None) claimed = False try: @@ -427,59 +427,32 @@ def _collect_test_servers(name: str | None, config_file: str | None) -> list[Any """ Return a priority-ordered list of available test servers. - Tries the full registry first (source tree); falls back to embedded - servers from ``caldav.testing`` plus ``test_allowed`` config sections - (pip-installed users). + The registry reads the default config file, so ``config_file`` adds + only what it holds beyond that: a file named explicitly or through + CALENDARING_CONFIG_FILE / CALDAV_CONFIG_FILE. """ - try: - from tests.test_servers.registry import get_registry - - servers: list[Any] = list(get_registry().enabled_servers()) - except ImportError: - servers = _get_pip_test_servers(config_file) + from caldav.testing.registry import get_registry + + servers: list[Any] = list(get_registry().enabled_servers()) + if config_file: + known = {s.name.lower() for s in servers} + servers += [ + _ConfiguredServer(section_name, params) + for section_name, params in get_all_test_servers(config_file).items() + if section_name.lower() not in known + ] + servers.sort(key=lambda s: s.priority) if name is not None: servers = [s for s in servers if name.lower() in (s.name.lower(), s.server_type.lower())] return servers -def _get_pip_test_servers(config_file: str | None) -> list[Any]: - """ - Build a server list for pip-installed users (no tests/ available). - - Includes embedded servers from caldav.testing and any test_allowed - sections from config files, all sorted by priority. - """ - from caldav.testing import RadicaleServer, XandikosServer - - servers: list[Any] = [] - - try: - import xandikos # noqa: F401 - - servers.append(XandikosServer()) - except ImportError: - pass - - try: - import radicale # noqa: F401 - - servers.append(RadicaleServer()) - except ImportError: - pass - - for section_name, params in get_all_test_servers(config_file).items(): - servers.append(_ConfiguredServer(section_name, params)) - - return sorted(servers, key=lambda s: s.priority) - - class _ConfiguredServer: """ - Thin wrapper around a ``test_allowed`` config-file section. - - Used only in pip-installed mode; in the source tree these sections are - loaded into the registry as ``ExternalTestServer`` instances instead. + Thin wrapper around a ``test_allowed`` section of a config file that + the registry does not read (see ``_collect_test_servers``); the + registry loads the default file's sections as ``ExternalTestServer``. """ server_type = "external" diff --git a/caldav/testing.py b/caldav/testing/__init__.py similarity index 96% rename from caldav/testing.py rename to caldav/testing/__init__.py index 790cb2cfe..6b48dc8c5 100644 --- a/caldav/testing.py +++ b/caldav/testing/__init__.py @@ -1,12 +1,12 @@ """ -Lightweight embedded CalDAV test servers. +CalDAV test servers, shipped with the package. -Provides XandikosServer and RadicaleServer that are part of the installed -package so that pip-installed users can use get_davclient() with -PYTHON_CALDAV_USE_TEST_SERVER=1 without needing the full test infrastructure -from the source tree. +This module holds the lightweight embedded XandikosServer and RadicaleServer, +used by get_davclient() with PYTHON_CALDAV_USE_TEST_SERVER=1. -Docker and external server support lives in tests/test_servers/ (source only). +The registry, config loader and the Docker and external server classes are +submodules of this package (base, registry, config_loader, docker, ...); the +docker-compose files stay in the source repository. """ import copy diff --git a/tests/test_servers/base.py b/caldav/testing/base.py similarity index 96% rename from tests/test_servers/base.py rename to caldav/testing/base.py index d199fe152..16b256331 100644 --- a/tests/test_servers/base.py +++ b/caldav/testing/base.py @@ -17,10 +17,22 @@ from caldav.config import get_test_setting -try: - import niquests as requests -except ImportError: - import requests # type: ignore + +def docker_servers_dir() -> Path: + """Directory holding the per-server ``start.sh``/``docker-compose.yml`` files. + + These stay in the source repository, not in the installed package. Point + ``CALDAV_DOCKER_TEST_SERVERS_DIR`` at ``tests/docker-test-servers`` of a + checkout to use them from an installed caldav; without it, the directory is + looked up relative to the module (found when caldav is a source checkout). + """ + env = os.environ.get("CALDAV_DOCKER_TEST_SERVERS_DIR") + if env: + return Path(env).expanduser() + return Path(__file__).resolve().parent.parent.parent / "tests" / "docker-test-servers" + + +from caldav.lib.http_sync import requests # Constants - extracted from magic numbers in conf.py DEFAULT_HTTP_TIMEOUT = 5 @@ -402,9 +414,7 @@ def __init__(self, config: dict[str, Any] | None = None) -> None: # Docker directory - default to tests/docker-test-servers/ from pathlib import Path - default_docker_dir = ( - Path(__file__).parent.parent / "docker-test-servers" / self.name.lower() - ) + default_docker_dir = docker_servers_dir() / self.name.lower() self.docker_dir = Path(self.config.get("docker_dir", default_docker_dir)) # Set when start() fails, so later tests fail fast instead of # re-running start.sh and waiting for the full timeout every time. diff --git a/tests/test_servers/config_loader.py b/caldav/testing/config_loader.py similarity index 98% rename from tests/test_servers/config_loader.py rename to caldav/testing/config_loader.py index 192291476..e8ba5ea1a 100644 --- a/tests/test_servers/config_loader.py +++ b/caldav/testing/config_loader.py @@ -19,7 +19,7 @@ # Test-specific config file locations (don't need test_allowed). The # checkout's file is found relative to this module, not to the current # directory, so projects running from elsewhere see it too. -_TESTS_DIR = Path(__file__).resolve().parent.parent +_TESTS_DIR = Path(__file__).resolve().parent.parent.parent / "tests" TEST_CONFIG_LOCATIONS = [ str(_TESTS_DIR / "caldav_test_servers.yaml"), str(_TESTS_DIR / "caldav_test_servers.json"), diff --git a/tests/test_servers/docker.py b/caldav/testing/docker.py similarity index 99% rename from tests/test_servers/docker.py rename to caldav/testing/docker.py index bd8db86f6..c508e5f6d 100644 --- a/tests/test_servers/docker.py +++ b/caldav/testing/docker.py @@ -10,12 +10,8 @@ from typing import Any from urllib.parse import quote -try: - import niquests as requests -except ImportError: - import requests # type: ignore - from caldav import compatibility_hints +from caldav.lib.http_sync import requests from .base import DEFAULT_HTTP_TIMEOUT, DockerTestServer from .registry import register_server_class diff --git a/tests/test_servers/embedded.py b/caldav/testing/embedded.py similarity index 100% rename from tests/test_servers/embedded.py rename to caldav/testing/embedded.py diff --git a/tests/test_servers/helpers.py b/caldav/testing/helpers.py similarity index 98% rename from tests/test_servers/helpers.py rename to caldav/testing/helpers.py index 02b396fcb..0408cadd9 100644 --- a/tests/test_servers/helpers.py +++ b/caldav/testing/helpers.py @@ -36,7 +36,7 @@ def client_context(server_index: int = 0, server_name: str | None = None): Usage:: - from tests.test_servers import client_context + from caldav.testing.helpers import client_context with client_context() as client: principal = client.principal() diff --git a/tests/test_servers/registry.py b/caldav/testing/registry.py similarity index 98% rename from tests/test_servers/registry.py rename to caldav/testing/registry.py index 3485ca96a..f63fc95af 100644 --- a/tests/test_servers/registry.py +++ b/caldav/testing/registry.py @@ -302,17 +302,16 @@ def _discover_embedded_servers(self) -> None: def _discover_docker_servers(self) -> None: """Discover available Docker servers.""" - from pathlib import Path - from .base import DockerTestServer - - docker_available = DockerTestServer.verify_docker() + from .base import docker_servers_dir as _docker_servers_dir # Look for docker-test-servers directories - docker_servers_dir = Path(__file__).parent.parent / "docker-test-servers" + docker_servers_dir = _docker_servers_dir() if not docker_servers_dir.exists(): return + docker_available = DockerTestServer.verify_docker() + # Check each subdirectory for a start.sh script for server_dir in docker_servers_dir.iterdir(): if server_dir.is_dir() and (server_dir / "start.sh").exists(): @@ -378,7 +377,7 @@ def get_registry() -> ServerRegistry: warnings.warn( f"Failed to load test server configuration: {e}. " - "Check tests/caldav_test_servers.yaml for errors.", + "Check the test server configuration for errors.", UserWarning, stacklevel=2, ) diff --git a/changes/+testing-registry-in-package.added.md b/changes/+testing-registry-in-package.added.md new file mode 100644 index 000000000..f8afe4322 --- /dev/null +++ b/changes/+testing-registry-in-package.added.md @@ -0,0 +1 @@ +The test-server framework - `ServerRegistry`, `get_registry()`, the config loader and the Docker and external server classes - is now part of the installed package, as submodules of `caldav.testing`, so other projects can run their tests against caldav's test servers without a source checkout (https://github.com/python-caldav/caldav/issues/817). `tests/test_servers` re-exports it. The docker-compose files stay in the repository: set `CALDAV_DOCKER_TEST_SERVERS_DIR` to `tests/docker-test-servers` of a checkout to start the Docker servers from an installed caldav. `get_davclient()` with `PYTHON_CALDAV_USE_TEST_SERVER=1` now picks from that registry on an installed caldav too, so the servers in `~/.config/caldav/test_servers.yaml` are candidates there as well. diff --git a/docs/design/TODO.md b/docs/design/TODO.md index 4f6063254..1f04ed4f9 100644 --- a/docs/design/TODO.md +++ b/docs/design/TODO.md @@ -42,7 +42,7 @@ A response whose `Content-Type` says XML but whose body is not XML (an HTML erro ## A waiting session-lock claim blocks other threads -`TestServer.claim_server()` in `tests/test_servers/base.py` polls for the flock (up to `PYTHON_CALDAV_TEST_SERVER_LOCK_TIMEOUT`, 30 minutes by default) while holding the module-wide `_session_locks_mutex`. A thread waiting for one busy server therefore blocks every other thread claiming a different server, which contradicts "runs against different servers proceed in parallel". Harmless for single-threaded pytest. Fix: hold the mutex only around the `_session_locks` checks and inserts, with a per-name lock against double claims. Found in the review of https://github.com/python-caldav/caldav/pull/776. +`TestServer.claim_server()` in `caldav/testing/base.py` polls for the flock (up to `PYTHON_CALDAV_TEST_SERVER_LOCK_TIMEOUT`, 30 minutes by default) while holding the module-wide `_session_locks_mutex`. A thread waiting for one busy server therefore blocks every other thread claiming a different server, which contradicts "runs against different servers proceed in parallel". Harmless for single-threaded pytest. Fix: hold the mutex only around the `_session_locks` checks and inserts, with a per-name lock against double claims. Found in the review of https://github.com/python-caldav/caldav/pull/776. ## calendar_multiget() fails on an async client @@ -72,3 +72,7 @@ On Nextcloud a deleted calendar moves to the trashbin, keeps its cal_id occupied The tests and the caldav-server-tester leave calendars behind, and on servers that cap the number of calendars per principal (ecloud, see above) that eventually blocks all creation. On 2026-10-23 ecloud held 15 empty calendars with bare-uuid cal_ids and the uuid as display name; which code made them was not found. Every calendar the tests or the tester create should carry a cal_id prefix, like the caldav-server-tester's `csc_` UIDs, so a cleanup step can delete leftovers safely - with the purge from the section above where the server has a trashbin. * caldav tests: there is no common cal_id prefix on every calendar, and no sweep. Several tests create calendars by name only (`test_caldav.py` "CalConfigA"/"CalConfigB", "XandikosMetaCal"/"RadicaleMetaCal", `test_get_calendars_sections.py`, `test_examples.py`), so `make_calendar()` gives them a bare uuid. Give all of them a `pythoncaldav-test` prefixed cal_id and add a session-level sweep. * caldav-server-tester: has `PROBE_CALENDAR_PREFIXES` and a sweep matching cal_id or display name, but `CheckDuplicateUID` creates its second calendar by name only, so its cal_id is a bare uuid that the sweep cannot recognise when the server drops the display name. Pass a prefixed cal_id. Its sweep also only moves calendars to the Nextcloud trashbin. + +## An installed caldav.testing looks for the checkout in site-packages + +`caldav/testing/config_loader.py` (`_TESTS_DIR`) and `docker_servers_dir()` in `caldav/testing/base.py` look for a checkout's `tests/` three levels up from the module. For an installed caldav that is `site-packages/tests`, so a stray top-level `tests` directory left by another badly packaged wheel would be searched for `caldav_test_servers.yaml` and `docker-test-servers/`. Also, while `CALDAV_DOCKER_TEST_SERVERS_DIR` locates the compose files of a checkout, nothing locates its `caldav_test_servers.yaml`, so an installed caldav reads only `~/.config/caldav/test_servers.yaml`. Fix: use the `tests/` path only when it belongs to a caldav checkout (e.g. a sibling `pyproject.toml` naming caldav), and perhaps add a `CALDAV_TEST_SERVERS_CONFIG` variable. Found in the review of https://github.com/python-caldav/caldav/issues/817. diff --git a/pyproject.toml b/pyproject.toml index 6be285c56..478964fc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -139,13 +139,11 @@ test = [ ignore = ["DEP002"] # Test dependencies (pytest, coverage, etc.) are not imported in main code [tool.deptry.per_rule_ignores] -DEP001 = ["conf", "h2", "tests", "calendaring_jmap"] # conf: Local test config, h2: Optional HTTP/2 support, calendaring_jmap: optional dep for caldav.jmap. +DEP001 = ["conf", "h2", "calendaring_jmap"] # conf: Local test config, h2: Optional HTTP/2 support, calendaring_jmap: optional dep for caldav.jmap. ## httpxyz needed an entry here while it was imported by name; the httpx-family ## libraries are now imported dynamically (see _ASYNC_HTTPX_CANDIDATES), which ## deptry does not see at all. -## tests: caldav/config.py optionally imports tests.test_servers.registry (source -## tree only, guarded by ImportError) to auto-start a test server from a checkout. -DEP003 = ["aiohttp", "h2"] # aiohttp: optional dep used only in caldav/testing.py +DEP003 = ["aiohttp", "h2"] # aiohttp: optional dep used only in caldav/testing/__init__.py ## h2 needs an entry in both DEP001 and DEP003: which of the two the optional ## `import h2` in async_davclient.py trips depends on whether h2 happens to be ## installed - DEP001 "missing" when it is not (CI), DEP003 "transitive" when it diff --git a/tests/README.md b/tests/README.md index fae624129..d641fafc2 100644 --- a/tests/README.md +++ b/tests/README.md @@ -232,14 +232,24 @@ test-servers: ## Test Server Framework -The `test_servers/` directory contains the unified test server framework: - -- `base.py` - Base classes for test servers -- `config_loader.py` - Configuration file loading -- `registry.py` - Server discovery and registration -- `embedded.py` - Embedded server implementations (Radicale, Xandikos) -- `docker.py` - Docker server implementations -- `helpers.py` - Test helper utilities +The framework is part of the installed package, as `caldav.testing`, so other +projects can use it without a source checkout: + +- `caldav/testing/__init__.py` - the embedded Radicale and Xandikos servers +- `caldav/testing/base.py` - Base classes for test servers +- `caldav/testing/config_loader.py` - Configuration file loading +- `caldav/testing/registry.py` - Server discovery and registration +- `caldav/testing/embedded.py` - Registry wrappers for the embedded servers +- `caldav/testing/docker.py` - Docker server implementations +- `caldav/testing/helpers.py` - Test helper utilities + +`tests/test_servers/` re-exports all of it, so `from tests.test_servers import ...` +keeps working. The docker-compose files (`tests/docker-test-servers/`) are not +shipped: they are found relative to a source checkout, or, from an installed +caldav, through `CALDAV_DOCKER_TEST_SERVERS_DIR` pointing at that directory. +Without them, a docker server that is already running (e.g. a CI service +container) is not discovered; give it a config entry instead (`type: baikal` +and its `url`). ## Continuous Integration diff --git a/tests/test_servers/__init__.py b/tests/test_servers/__init__.py index a81b83988..587a8593a 100644 --- a/tests/test_servers/__init__.py +++ b/tests/test_servers/__init__.py @@ -1,6 +1,11 @@ """ Test server framework for caldav tests. +The implementation lives in caldav.testing (shipped with the package, so that +other projects can use it); this package re-exports it, and each submodule is +an alias of its caldav.testing counterpart, so existing imports and +monkeypatching keep working. + This package provides a unified framework for starting and managing test servers (Radicale, Xandikos, Docker containers) for both sync and async tests. @@ -23,7 +28,10 @@ server.stop() """ -from .base import ( +import importlib +import sys + +from caldav.testing.base import ( DEFAULT_HTTP_TIMEOUT, MAX_STARTUP_WAIT_SECONDS, STARTUP_POLL_INTERVAL, @@ -32,9 +40,14 @@ ExternalTestServer, TestServer, ) -from .config_loader import ConfigParseError, load_test_server_config -from .helpers import client_context, has_test_servers -from .registry import ServerRegistry, get_available_servers, get_registry +from caldav.testing.config_loader import ConfigParseError, load_test_server_config +from caldav.testing.helpers import client_context, has_test_servers +from caldav.testing.registry import ServerRegistry, get_available_servers, get_registry + +for _name in ("base", "config_loader", "docker", "embedded", "helpers", "registry"): + sys.modules[f"{__name__}.{_name}"] = globals()[_name] = importlib.import_module( + f"caldav.testing.{_name}" + ) __all__ = [ # High-level helpers diff --git a/tests/test_servers/test_base.py b/tests/test_testing_base.py similarity index 79% rename from tests/test_servers/test_base.py rename to tests/test_testing_base.py index f9926b3db..6c822888d 100644 --- a/tests/test_servers/test_base.py +++ b/tests/test_testing_base.py @@ -5,8 +5,8 @@ import pytest -from . import base -from .base import DockerTestServer +from caldav.testing import base +from caldav.testing.base import DockerTestServer @pytest.fixture(autouse=True) @@ -112,7 +112,7 @@ class TestSessionLock: CLAIM = ( "import sys; sys.path.insert(0, {root!r}); " - "from tests.test_servers.base import TestServer; " + "from caldav.testing.base import TestServer; " "TestServer.claim_server({name!r})" ) @@ -230,7 +230,7 @@ def test_get_davclient_falls_through_busy_server( def test_fallback_uses_servers_without_session_lock( self, monkeypatch: pytest.MonkeyPatch ) -> None: - """Pip-installed caldav has no tests/ and so no claim_server() on its servers.""" + """A server from an explicit config file has no claim_server().""" from caldav import config server = config._ConfiguredServer("Lambda", {"url": "http://localhost:1/Lambda/"}) @@ -285,3 +285,66 @@ def is_accessible(self) -> bool: server._started = True # as get_server_params() leaves it at collection server.start() assert counter.read_text().count("run") == 1 + + +class TestDockerServersDir: + def test_env_var_names_the_checkout_directory(self, tmp_path, monkeypatch) -> None: + monkeypatch.setenv("CALDAV_DOCKER_TEST_SERVERS_DIR", str(tmp_path)) + assert base.docker_servers_dir() == tmp_path + from caldav.testing.docker import BaikalTestServer + + assert BaikalTestServer().docker_dir == tmp_path / "baikal" + + def test_registry_discovers_servers_from_env_var_dir(self, tmp_path, monkeypatch) -> None: + from caldav.testing import docker # noqa: F401 (registers the classes) + from caldav.testing.registry import ServerRegistry + + (tmp_path / "baikal").mkdir() + (tmp_path / "baikal" / "start.sh").write_text("#!/bin/sh\n") + monkeypatch.setenv("CALDAV_DOCKER_TEST_SERVERS_DIR", str(tmp_path)) + monkeypatch.setattr(DockerTestServer, "verify_docker", staticmethod(lambda: True)) + registry = ServerRegistry() + registry._discover_docker_servers() + server = registry.get("Baikal") + assert server is not None and server.docker_dir == tmp_path / "baikal" + + def test_docker_is_not_probed_without_the_compose_directory( + self, tmp_path, monkeypatch + ) -> None: + from caldav.testing.registry import ServerRegistry + + monkeypatch.setenv("CALDAV_DOCKER_TEST_SERVERS_DIR", str(tmp_path / "missing")) + + def probe() -> bool: + raise AssertionError("docker probed with no docker-test-servers directory") + + monkeypatch.setattr(DockerTestServer, "verify_docker", staticmethod(probe)) + ServerRegistry()._discover_docker_servers() + + +class TestCollectTestServers: + def test_explicit_config_file_reaches_the_server_list(self, tmp_path, monkeypatch) -> None: + """A config file given to get_davclient() (or by CALDAV_CONFIG_FILE) is honoured.""" + import json + + from caldav import config + from caldav.testing import registry + + monkeypatch.setattr(registry, "_global_registry", registry.ServerRegistry()) + config_file = tmp_path / "calendar.conf" + config_file.write_text( + json.dumps( + {"OnlyHere": {"caldav_url": "https://dav.example.com/", "test_allowed": True}} + ) + ) + names = [s.name for s in config._collect_test_servers(None, str(config_file))] + assert "OnlyHere" in names + + +def test_test_servers_submodules_are_package_attributes(monkeypatch) -> None: + """``monkeypatch.setattr("tests.test_servers.base.X", ...)`` resolves through attributes.""" + import tests.test_servers + + assert tests.test_servers.base is base + monkeypatch.setattr("tests.test_servers.base.DEFAULT_HTTP_TIMEOUT", 1) + assert base.DEFAULT_HTTP_TIMEOUT == 1 diff --git a/tests/test_servers/test_config_loader.py b/tests/test_testing_config_loader.py similarity index 92% rename from tests/test_servers/test_config_loader.py rename to tests/test_testing_config_loader.py index b80341405..1e60a907c 100644 --- a/tests/test_servers/test_config_loader.py +++ b/tests/test_testing_config_loader.py @@ -4,7 +4,7 @@ import pytest -from .config_loader import ConfigParseError, load_test_server_config +from caldav.testing.config_loader import ConfigParseError, load_test_server_config class TestLoadTestServerConfig: @@ -58,7 +58,7 @@ def test_nonexistent_explicit_config_falls_back(self, tmp_path: Path, monkeypatc """Test that nonexistent explicit config falls back to default locations.""" # Temporarily change to a directory without any config files # and set HOME to temp dir to avoid finding real user configs - from . import config_loader + from caldav.testing import config_loader monkeypatch.chdir(tmp_path) monkeypatch.setenv("HOME", str(tmp_path)) @@ -141,8 +141,8 @@ class TestEnabledFalseDisablesRegisteredServer: """ def _registry_with_registered_server(self): - from .base import TestServer - from .registry import ServerRegistry + from caldav.testing.base import TestServer + from caldav.testing.registry import ServerRegistry class _FakeDocker(TestServer): server_type = "docker" @@ -192,7 +192,7 @@ class TestConfigSourcesUnion: @pytest.fixture def sources(self, tmp_path: Path, monkeypatch) -> tuple[Path, Path]: - from . import config_loader + from caldav.testing import config_loader checkout_yaml = tmp_path / "checkout.yaml" user_yaml = tmp_path / "user.yaml" @@ -236,7 +236,7 @@ def test_name_clash_is_case_insensitive(self, sources) -> None: assert cfg["Both"]["enabled"] is False def test_broken_caldav_config_keeps_yaml_servers(self, sources, monkeypatch) -> None: - from . import config_loader + from caldav.testing import config_loader checkout_yaml, _ = sources checkout_yaml.write_text("fromcheckout: {type: external, url: 'https://c.example.com/'}\n") @@ -255,12 +255,14 @@ def test_explicit_file_is_used_alone(self, sources, tmp_path: Path) -> None: assert set(load_test_server_config(str(explicit))) == {"only"} def test_checkout_yaml_does_not_depend_on_cwd(self, tmp_path: Path, monkeypatch) -> None: - from . import config_loader + from caldav.testing import config_loader monkeypatch.chdir(tmp_path) - checkout_yaml = Path(__file__).resolve().parent.parent / "caldav_test_servers.yaml" - assert str(checkout_yaml) in [ - str(Path(loc).expanduser().resolve()) for loc in config_loader.TEST_CONFIG_LOCATIONS + # The directory is resolved, not the file: a developer's yaml may be a symlink + checkout_yaml = Path(__file__).resolve().parent / "caldav_test_servers.yaml" + assert checkout_yaml in [ + Path(loc).expanduser().parent.resolve() / Path(loc).name + for loc in config_loader.TEST_CONFIG_LOCATIONS ] @@ -278,7 +280,7 @@ def listener(self): sock.close() def test_reachable(self, listener: int) -> None: - from .base import ExternalTestServer + from caldav.testing.base import ExternalTestServer server = ExternalTestServer({"name": "ext", "url": f"http://127.0.0.1:{listener}/dav/"}) assert server.is_accessible() @@ -286,7 +288,7 @@ def test_reachable(self, listener: int) -> None: def test_unreachable(self) -> None: import socket - from .base import ExternalTestServer + from caldav.testing.base import ExternalTestServer ## Grab a free port, then release it, so nothing listens there sock = socket.socket() @@ -297,13 +299,13 @@ def test_unreachable(self) -> None: assert not server.is_accessible() def test_bare_domain_without_features(self) -> None: - from .base import ExternalTestServer + from caldav.testing.base import ExternalTestServer server = ExternalTestServer({"name": "ext", "url": "dav.invalid"}) assert not server.is_accessible() def test_url_from_features(self, listener: int) -> None: - from .base import ExternalTestServer + from caldav.testing.base import ExternalTestServer server = ExternalTestServer( {