From d51c54f483f07cb4ac35bb78aa64c3955ef322fe Mon Sep 17 00:00:00 2001 From: lapy Date: Tue, 13 Oct 2026 12:55:55 +0100 Subject: [PATCH] fix(tests): make pytest suite runnable from a fresh checkout (CI fix) PR#2 CI: the Python job failed with 17 FileNotFoundError collection errors. Widening CI from upstream's smoke-test-only to the full suite exposed that the suite depends on main/xiaozhi-server/data/.config.yaml, which is gitignored and therefore absent on CI runners (dev boxes always have it, so the local rebase gate never caught it). - add tracked tests/testdata/test-config.yaml (placeholder keys, empty manager-api block, same shape as the local stub) - conftest falls back to it when data/.config.yaml is missing (explicit PYTEST_CONFIG_FILE still wins) - neutralize config.settings/config.logger check_config_file bindings and logger's load_config so setup_logging() at module import works without a real deployment config; these names are bound at import time via manage_api_client -> config.logger, so patching the source modules alone was not enough Verified on a clean clone with py3.10 + full requirements: 58 passed, 5 skipped; lifecycle unittests 11 OK on both clean clone and dev box. --- main/xiaozhi-server/tests/conftest.py | 26 +++++++++++-- .../tests/testdata/test-config.yaml | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 main/xiaozhi-server/tests/testdata/test-config.yaml diff --git a/main/xiaozhi-server/tests/conftest.py b/main/xiaozhi-server/tests/conftest.py index 7795aef31..d05c580a6 100644 --- a/main/xiaozhi-server/tests/conftest.py +++ b/main/xiaozhi-server/tests/conftest.py @@ -66,9 +66,19 @@ except Exception: # 1. File pointed to by environment variable ``PYTEST_CONFIG_FILE`` (recommended - isolated test config) # 2. data/.config.yaml (Production / Single-module deployment config) _DEFAULT_CUSTOM = _PROJECT_ROOT / "data" / ".config.yaml" -_CUSTOM_CONFIG = Path(os.environ["PYTEST_CONFIG_FILE"]) if os.environ.get( - "PYTEST_CONFIG_FILE" -) else _DEFAULT_CUSTOM + +# Fresh checkout / CI fallback: the real data/.config.yaml is gitignored, so +# without this the suite only passes on dev boxes that happen to have one. +# Fall back to the tracked test-only config (placeholder keys, empty +# manager-api block) so a clean clone runs the exact same provider tests. +_TESTDATA_CONFIG = Path(__file__).resolve().parent / "testdata" / "test-config.yaml" +if os.environ.get("PYTEST_CONFIG_FILE"): + # Explicit override always wins, even if the path does not exist yet. + _CUSTOM_CONFIG = Path(os.environ["PYTEST_CONFIG_FILE"]) +elif _DEFAULT_CUSTOM.exists(): + _CUSTOM_CONFIG = _DEFAULT_CUSTOM +else: + _CUSTOM_CONFIG = _TESTDATA_CONFIG def _load_via_file_merge() -> dict: @@ -141,7 +151,17 @@ def _init_service_noop(_config): import config.config_loader as _cl # noqa: E402 import config.manage_api_client as _mac # noqa: E402 +import config.settings as _settings # noqa: E402 +import config.logger as _logger_mod # noqa: E402 + _cl.load_config = _load_config_patched +# data/.config.yaml is gitignored, so a fresh checkout (CI) has none: skip the +# deployment-file check and keep log config coming from the merged CONFIG above. +# config.logger binds these names at import time (via manage_api_client), so +# patch the already-imported references as well, not just the source modules. +_settings.check_config_file = lambda: None +_logger_mod.check_config_file = lambda: None +_logger_mod.load_config = _load_config_patched _mac.init_service = _init_service_noop _mac.get_server_config = _get_server_config_patched # Clear ManageApiClient singleton to avoid prior instantiation triggered by other code. diff --git a/main/xiaozhi-server/tests/testdata/test-config.yaml b/main/xiaozhi-server/tests/testdata/test-config.yaml new file mode 100644 index 000000000..dc8314540 --- /dev/null +++ b/main/xiaozhi-server/tests/testdata/test-config.yaml @@ -0,0 +1,39 @@ +# Test-only deployment config, tracked in git and used by tests/conftest.py as +# a fallback when the real (gitignored) data/.config.yaml is absent — e.g. in +# CI or a fresh checkout. Minimal single-module stub so provider tests can +# construct objects against the mock shims in conftest. Placeholder keys only; +# never point it at a real manager-api. +manager-api: + url: "" + secret: "" + +selected_module: + VAD: SileroVAD + ASR: OpenaiASR + LLM: OpenAILLM + TTS: EdgeTTS + Memory: nomem + Intent: nointent + +ASR: + OpenaiASR: + type: openai + model_name: whisper-1 + api_key: placeholder-key-do-not-use + base_url: http://127.0.0.1:18000/v1 + +LLM: + OpenAILLM: + type: openai + model_name: gpt-4o-mini + api_key: placeholder-key-do-not-use + base_url: http://127.0.0.1:18000/v1 + +TTS: + EdgeTTS: + type: edge + voice_name: en-US-AriaNeural + +Memory: + nomem: + type: nomem