Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(test): make the async-plugin notice advisory and stop over-reporting

cyh4241d2e0244a
diff --git a/tests/conftest.py b/tests/conftest.py
index 05696ec1c..fe1935d55 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -49,6 +49,7 @@ import os
import shutil
import sqlite3
import sys
+import unittest
import warnings
from contextlib import closing, contextmanager
from functools import wraps
@@ -461,16 +462,32 @@ def _reset_show_runtime_manager():
def _async_items_missing_plugin(items) -> list:
- """Return the selected items that need pytest-asyncio but would not get it.
-
- ``anyio``-marked coroutines are excluded: anyio is a runtime dependency, so
- its pytest plugin is present wherever the package installs, and it drives
- those tests itself.
+ """Return the selected items pytest will try, and fail, to run as coroutines.
+
+ Three kinds of coroutine item are deliberately excluded because pytest
+ handles them without the plugin:
+
+ * ``unittest.TestCase`` methods -- ``IsolatedAsyncioTestCase`` brings its own
+ event loop, and 1087 of this suite's async items are of this kind;
+ * ``anyio``-marked coroutines -- anyio is a runtime dependency, so its plugin
+ is present wherever the package installs;
+ * items already marked ``skip`` -- pytest reports the skip without ever
+ awaiting them (``tests/e2e/conftest.py`` skips ``e2e_model_hub`` items
+ unless they are explicitly selected).
+
+ ``skipif`` is not evaluated here: deciding it needs pytest's private
+ evaluation helpers, and over-counting 15 items costs one inaccurate number
+ in an advisory message rather than a broken run.
"""
offenders = []
for item in items:
if item.get_closest_marker("anyio") is not None:
continue
+ if item.get_closest_marker("skip") is not None:
+ continue
+ cls = getattr(item, "cls", None)
+ if isinstance(cls, type) and issubclass(cls, unittest.TestCase):
+ continue
try:
func = item.obj
except Exception:
@@ -481,34 +498,44 @@ def _async_items_missing_plugin(items) -> list:
return offenders
[email protected](trylast=True)
def pytest_collection_modifyitems(config, items):
- """Fail once, up front, when async tests are selected without pytest-asyncio.
+ """Say once, up front, that a missing pytest-asyncio explains the failures.
``asyncio_mode = "auto"`` only takes effect when the plugin is installed.
- Without it each of the suite's ~1670 ``async def`` tests fails on its own
- with "async def functions are not natively supported", while the mode
- setting is reported merely as an unknown-config warning. That names the
- tests instead of the one missing dev dependency, so a suite run in an
- environment that never installed the dev group reads as a broad product
- regression -- measured as 10 failures across 2 files after 146s in a
- container whose runtime venv installs only the wheel.
-
- This is a collection-time check rather than a ``required_plugins`` entry in
- pyproject because that key is a rootdir-wide prerequisite enforced *before*
- collection: it also aborts the packaged-test job (.github/workflows/lint.yml,
- which installs ``dist/*.whl pytest``) and the publish finalizer
- (.github/workflows/publish.yml, ``pip install -e . build pytest``), both of
- which select synchronous tests only and legitimately have no async plugin.
- Keying off the selected items puts the guard on exactly the runs it is for.
+ Without it each native ``async def`` test fails on its own with "async def
+ functions are not natively supported", while the mode setting is reported
+ merely as an unknown-config warning. That names the tests instead of the one
+ missing dev dependency, so a suite run in an environment that never
+ installed the dev group reads as a broad product regression -- measured as
+ 10 failures across 2 files in a container whose runtime venv installs only
+ the wheel.
+
+ This reports and lets the run proceed; it deliberately does not abort.
+ Aborting requires being right about every item, and the cost of being wrong
+ is asymmetric: a wrong abort breaks a legitimate run, while a wrong count
+ costs one inaccurate line. An earlier revision of this guard aborted, and
+ broke the packaged-test and publish jobs before running a single test. The
+ value here was always diagnosis, never saving time, and a message buys that
+ without being able to break anything.
+
+ ``trylast`` matters: pytest's own ``-k`` / ``-m`` deselection runs in this
+ same hook, so an earlier-running implementation would count items the user
+ already filtered out.
"""
if config.pluginmanager.hasplugin("asyncio"):
return
offenders = _async_items_missing_plugin(items)
if not offenders:
return
- raise pytest.UsageError(
- f"{len(offenders)} selected tests are 'async def' but pytest-asyncio is "
- 'not installed, so asyncio_mode="auto" is inactive and every one of them '
- "would fail as 'async def functions are not natively supported'. "
- "Install the dev dependency group (uv sync --group dev)."
+ message = (
+ f"pytest-asyncio is not installed, so asyncio_mode=\"auto\" is inactive. "
+ f"{len(offenders)} selected tests are native 'async def' tests and will "
+ 'fail as "async def functions are not natively supported" -- that is the '
+ "missing dev dependency, not a product regression. Install it with "
+ "`uv sync --group dev`."
)
+ reporter = config.pluginmanager.get_plugin("terminalreporter")
+ if reporter is not None:
+ reporter.write_line(f"\n{message}", yellow=True, bold=True)
+ warnings.warn(message, pytest.PytestWarning, stacklevel=1)
diff --git a/tests/test_conftest_async_plugin_guard.py b/tests/test_conftest_async_plugin_guard.py
index 46cb7d4e3..5c721c621 100644
--- a/tests/test_conftest_async_plugin_guard.py
+++ b/tests/test_conftest_async_plugin_guard.py
@@ -1,24 +1,31 @@
-"""Regression guard: the missing-pytest-asyncio check must stay scoped to the
-runs that actually select ``async def`` tests.
-
-The first version of this guard was a ``required_plugins`` entry in the root
-``pyproject.toml``. That key is a rootdir-wide prerequisite enforced *before*
-collection, so it also aborted the two CI jobs that deliberately install only a
-built artifact plus ``pytest`` and select synchronous tests only: the
-packaged-test matrix in ``.github/workflows/lint.yml`` and the publish
-finalizer in ``.github/workflows/publish.yml`` -- the repository's sole release
-finalizer. Both died with "Missing required plugins: pytest-asyncio" before
-running a single test.
-
-These tests pin both directions: a synchronous selection with no async plugin
-still collects, and an ``async def`` selection with no async plugin fails once,
-naming the missing dependency rather than every test that would have failed.
+"""Regression guard: the missing-pytest-asyncio notice must never change a run.
+
+The notice exists so that a suite run in an environment that never installed the
+dev group reads as "one missing dependency" instead of a broad product
+regression. It is advisory on purpose.
+
+Two earlier revisions aborted collection instead, and each time the abort -- not
+the diagnosis -- was the defect:
+
+* as ``required_plugins`` in the root pyproject it was a rootdir-wide
+ prerequisite enforced before collection, so it also killed the packaged-test
+ matrix in lint.yml and the publish finalizer, which install only a built
+ artifact plus pytest;
+* as a collection-time abort it still killed legitimate selections, because
+ ``unittest.IsolatedAsyncioTestCase`` methods (1087 of them here) run fine
+ without the plugin, ``-k`` / ``-m`` deselection runs in the same hook, and
+ already-skipped items are never awaited.
+
+So these tests assert outcomes, not just messages: every selection that works
+without the plugin must keep working, and the one selection that genuinely
+fails must fail exactly as it would have anyway, with the notice added.
"""
from __future__ import annotations
import subprocess
import sys
+import unittest
from pathlib import Path
import pytest
@@ -26,17 +33,23 @@ import pytest
from tests.conftest import _async_items_missing_plugin
REPO_ROOT = Path(__file__).resolve().parents[1]
+NOTICE = "pytest-asyncio is not installed"
-# One of the files the packaged-test and publish jobs select. Kept in sync with
-# the workflows on purpose: if it ever grows an async test, those jobs need the
-# plugin and this guard should not be what tells us.
+# One of the files the packaged-test and publish jobs select.
SYNC_ONLY_CI_SELECTION = "tests/test_memory_distribution.py"
+# IsolatedAsyncioTestCase methods only -- unittest supplies the event loop.
+UNITTEST_ASYNC_FILE = "tests/test_feishu_post_messages.py"
+# Async tests behind the opt-in e2e_model_hub marker, skipped unless selected.
+SKIPPED_ASYNC_FILE = "tests/e2e/test_codex_request_gateway.py"
+# Contains one module-level `async def` test that genuinely needs the plugin.
+NATIVE_ASYNC_FILE = "tests/test_claude_agent_sessions.py"
class _FakeItem:
- def __init__(self, obj, *, anyio: bool = False, raises: bool = False):
+ def __init__(self, obj, *, marker: str | None = None, cls=None, raises=False):
self._obj = obj
- self._anyio = anyio
+ self._marker = marker
+ self.cls = cls
self._raises = raises
@property
@@ -46,7 +59,11 @@ class _FakeItem:
return self._obj
def get_closest_marker(self, name: str):
- return object() if (name == "anyio" and self._anyio) else None
+ return object() if name == self._marker else None
+
+
+class _AsyncCase(unittest.IsolatedAsyncioTestCase):
+ pass
async def _coroutine_test():
@@ -62,10 +79,16 @@ def test_plain_coroutine_items_are_reported() -> None:
assert _async_items_missing_plugin([item]) == [item]
-def test_anyio_marked_coroutines_are_not_reported() -> None:
- # anyio is a runtime dependency, so its plugin drives these wherever the
- # package installs -- flagging them would fire in environments that are fine.
- assert _async_items_missing_plugin([_FakeItem(_coroutine_test, anyio=True)]) == []
+ ("kwargs", "why"),
+ [
+ ({"cls": _AsyncCase}, "unittest brings its own event loop"),
+ ({"marker": "anyio"}, "anyio is a runtime dependency"),
+ ({"marker": "skip"}, "a skipped item is never awaited"),
+ ],
+)
+def test_items_pytest_handles_without_the_plugin_are_not_reported(kwargs, why) -> None:
+ assert _async_items_missing_plugin([_FakeItem(_coroutine_test, **kwargs)]) == [], why
def test_synchronous_items_are_not_reported() -> None:
@@ -73,42 +96,43 @@ def test_synchronous_items_are_not_reported() -> None:
def test_items_without_a_test_function_are_skipped() -> None:
- # Doctest and custom collectors raise on ``.obj``; the guard must not crash
- # the whole collection over one of them.
assert _async_items_missing_plugin([_FakeItem(None, raises=True)]) == []
-def _collect(selection: str) -> subprocess.CompletedProcess:
- return subprocess.run(
- [
- sys.executable,
- "-m",
- "pytest",
- "-p",
- "no:asyncio",
- "--collect-only",
- "-q",
- selection,
- ],
+def _run_without_plugin(*args: str) -> tuple[int, str]:
+ result = subprocess.run(
+ [sys.executable, "-m", "pytest", "-p", "no:asyncio", "-q", *args],
cwd=REPO_ROOT,
capture_output=True,
text=True,
)
+ return result.returncode, result.stdout + result.stderr
@pytest.mark.uses_real_paths
-def test_sync_only_ci_selection_still_collects_without_the_async_plugin() -> None:
- """The exact shape of the packaged-test and publish jobs must stay green."""
- result = _collect(SYNC_ONLY_CI_SELECTION)
- output = result.stdout + result.stderr
- assert result.returncode == 0, output
- assert "pytest-asyncio" not in output
+ ("args", "why"),
+ [
+ (("--collect-only", SYNC_ONLY_CI_SELECTION), "the packaged-test/publish shape"),
+ ((UNITTEST_ASYNC_FILE,), "unittest-managed async tests need no plugin"),
+ ((SKIPPED_ASYNC_FILE,), "opt-in e2e items are skipped, not awaited"),
+ (
+ ("-k", "test_ambiguous_results_emit_each_answer_in_order", NATIVE_ASYNC_FILE),
+ "the notice must run after -k deselection",
+ ),
+ ],
+)
+def test_selections_that_work_without_the_plugin_stay_untouched(args, why) -> None:
+ returncode, output = _run_without_plugin(*args)
+ assert returncode == 0, f"{why}: {output}"
+ assert NOTICE not in output, f"{why}: {output}"
@pytest.mark.uses_real_paths
-def test_async_selection_fails_once_naming_the_missing_plugin() -> None:
- result = _collect("tests/test_claude_agent_sessions.py")
- # pytest writes a UsageError raised from a hook to stderr, not stdout.
- output = result.stdout + result.stderr
- assert result.returncode == pytest.ExitCode.USAGE_ERROR, output
- assert "pytest-asyncio is not installed" in output
+def test_a_genuine_async_selection_is_explained_but_not_aborted() -> None:
+ returncode, output = _run_without_plugin(NATIVE_ASYNC_FILE)
+ assert NOTICE in output, output
+ # Not an abort: the run still executed, and failed only on the one test that
+ # genuinely needs the plugin -- exactly what it would do without the notice.
+ assert returncode != pytest.ExitCode.USAGE_ERROR, output
+ assert " passed" in output, output