Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

chore(hooks): add pre-commit hook to detect duplicated test fixtures (#3262)

LearningCircuit36d8056c4434
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 526b8f8ac3..43cf9c2194 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -301,6 +301,13 @@ repos:
files: ^tests/.*\.py$
description: "Ensure test functions with time.sleep(>0.1s) are marked
@pytest.mark.slow so CI can skip them with -m 'not slow'"
+ - id: check-fixture-duplication
+ name: Check for duplicated test fixtures
+ entry: .pre-commit-hooks/check-fixture-duplication.py
+ language: script
+ files: ^tests/.*\.py$
+ description: "Warn when test files redefine app/client/authenticated_client
+ fixtures from root conftest.py"
# Hooks intentionally NOT added
# See docs/decisions/0002-pre-commit-hook-reviews.md for full rationale.
diff --git a/.pre-commit-hooks/check-fixture-duplication.py b/.pre-commit-hooks/check-fixture-duplication.py
new file mode 100755
index 0000000000..6555070a00
--- /dev/null
+++ b/.pre-commit-hooks/check-fixture-duplication.py
@@ -0,0 +1,258 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook to warn when test files redefine root-conftest fixtures.
+
+The root tests/conftest.py provides `app`, `client`, and `authenticated_client`
+fixtures with security-relevant setup (CSRF, auth DB init, temp data dir).
+Module-level redefinitions in individual test files often skip this setup,
+creating subtle differences in test environments.
+
+Class-level fixtures are allowed because they cannot easily inherit from
+conftest. Module-level redefinitions produce a WARNING for allowlisted
+files (exit 0) and an ERROR for new occurrences (exit 1).
+"""
+
+import ast
+import os
+import sys
+
+# Fixture names defined in tests/conftest.py that should not be redefined
+# at module level in individual test files.
+PROTECTED_FIXTURES = {"app", "client", "authenticated_client"}
+
+# Existing violations — these files already redefine the fixtures.
+# They emit a soft warning (exit 0) instead of blocking the commit.
+# Remove entries as files are migrated to use the shared conftest fixtures.
+ALLOWLIST: dict[str, set[str]] = {
+ # --- app fixture ---
+ "tests/auth_tests/test_auth_decorators.py": {"app"},
+ "tests/auth_tests/test_auth_integration.py": {"app"},
+ "tests/auth_tests/test_auth_routes.py": {"app"},
+ "tests/database/test_session_context.py": {"app"},
+ "tests/database/test_session_context_extended.py": {"app"},
+ "tests/followup_research/test_routes.py": {"app"},
+ "tests/followup_research/test_routes_coverage.py": {"app"},
+ "tests/news/test_flask_api.py": {"app"},
+ "tests/news/test_flask_api_coverage.py": {"app"},
+ "tests/news/test_flask_api_coverage_gaps.py": {"app"},
+ "tests/news/test_flask_api_deep_coverage.py": {"app"},
+ "tests/news/test_flask_api_extra_coverage.py": {"app"},
+ "tests/news/test_flask_api_routes.py": {"app"},
+ "tests/news/test_flask_api_scheduler_coverage.py": {"app"},
+ "tests/news/test_web_blueprint.py": {"app"},
+ "tests/research_library/routes/test_library_routes_deep_coverage.py": {
+ "app"
+ },
+ "tests/research_library/routes/test_library_routes_extra_coverage.py": {
+ "app"
+ },
+ "tests/research_library/routes/test_library_routes_view_coverage.py": {
+ "app"
+ },
+ "tests/research_library/routes/test_rag_routes_coverage.py": {"app"},
+ "tests/research_library/routes/test_rag_routes_deep_coverage.py": {"app"},
+ "tests/research_library/routes/test_rag_routes_gaps_coverage.py": {"app"},
+ "tests/research_library/routes/test_rag_routes_indexing_coverage.py": {
+ "app"
+ },
+ "tests/research_library/routes/test_rag_routes_upload_coverage.py": {"app"},
+ "tests/routes/test_settings_routes.py": {"app"},
+ "tests/security/test_cookie_security.py": {"app"},
+ "tests/security/test_decorators.py": {"app"},
+ "tests/security/test_rate_limiter.py": {"app"},
+ "tests/test_link_analytics.py": {"app"},
+ "tests/web/auth/test_decorators_coverage.py": {"app"},
+ "tests/web/auth/test_middleware_optimizer.py": {"app"},
+ "tests/web/auth/test_queue_middleware_v2_behavior.py": {"app"},
+ "tests/web/routes/test_api_routes_coverage.py": {"app"},
+ "tests/web/routes/test_context_overflow_coverage.py": {"app"},
+ "tests/web/routes/test_history_routes_coverage.py": {"app"},
+ "tests/web/routes/test_history_routes_extended.py": {"app"},
+ "tests/web/routes/test_metrics_routes_coverage.py": {"app"},
+ "tests/web/routes/test_research_routes_deep_coverage.py": {"app"},
+ "tests/web/routes/test_research_routes_extra_coverage.py": {"app"},
+ "tests/web/routes/test_research_routes_extracted_helpers.py": {"app"},
+ "tests/web/routes/test_research_routes_start_research_coverage.py": {"app"},
+ "tests/web/routes/test_settings_routes_deep_coverage2.py": {"app"},
+ "tests/web/test_api_coverage.py": {"app"},
+ "tests/web/test_error_handler_behavior.py": {"app"},
+ "tests/web/test_teardown_cleanup.py": {"app"},
+ "tests/web/test_websocket_middleware.py": {"app"},
+ # --- client fixture ---
+ "tests/web/test_api.py": {"client", "authenticated_client"},
+ "tests/web/routes/test_history_routes.py": {
+ "client",
+ "authenticated_client",
+ },
+}
+
+# Merge client/authenticated_client into files that also have app
+_client_only: dict[str, set[str]] = {
+ "tests/auth_tests/test_auth_decorators.py": {"client"},
+ "tests/auth_tests/test_auth_integration.py": {"client"},
+ "tests/auth_tests/test_auth_routes.py": {"client"},
+ "tests/followup_research/test_routes.py": {"client"},
+ "tests/news/test_flask_api.py": {"client"},
+ "tests/news/test_flask_api_coverage.py": {"client"},
+ "tests/news/test_flask_api_coverage_gaps.py": {"client"},
+ "tests/news/test_flask_api_deep_coverage.py": {"client"},
+ "tests/news/test_flask_api_extra_coverage.py": {"client"},
+ "tests/news/test_flask_api_routes.py": {"client"},
+ "tests/news/test_flask_api_scheduler_coverage.py": {"client"},
+ "tests/news/test_web_blueprint.py": {"client"},
+ "tests/routes/test_settings_routes.py": {"client"},
+ "tests/security/test_cookie_security.py": {"client"},
+ "tests/security/test_decorators.py": {"client"},
+ "tests/test_link_analytics.py": {"client"},
+ "tests/web/auth/test_decorators_coverage.py": {"client"},
+ "tests/web/routes/test_context_overflow_coverage.py": {"client"},
+ "tests/web/routes/test_history_routes_coverage.py": {"client"},
+ "tests/web/routes/test_history_routes_extended.py": {"client"},
+ "tests/web/routes/test_metrics_routes_coverage.py": {"client"},
+ "tests/web/routes/test_research_routes_deep_coverage.py": {"client"},
+ "tests/web/routes/test_research_routes_extra_coverage.py": {"client"},
+ "tests/web/routes/test_research_routes_extracted_helpers.py": {"client"},
+ "tests/web/routes/test_research_routes_start_research_coverage.py": {
+ "client"
+ },
+ "tests/web/test_api_coverage.py": {"client"},
+ "tests/web/test_error_handler_behavior.py": {"client"},
+ "tests/web/test_websocket_middleware.py": {"client"},
+}
+
+for _path, _fixtures in _client_only.items():
+ ALLOWLIST.setdefault(_path, set()).update(_fixtures)
+
+
+def _normalize(filepath: str) -> str:
+ """Normalize path to use forward slashes and be relative to repo root."""
+ path = filepath.replace(os.sep, "/")
+ # Strip leading ./ if present
+ if path.startswith("./"):
+ path = path[2:]
+ # If path has tests/ in it, keep from tests/ onward
+ idx = path.find("tests/")
+ if idx >= 0:
+ path = path[idx:]
+ return path
+
+
+def _is_fixture_decorator(decorator: ast.expr) -> bool:
+ """Check if an AST decorator node is @pytest.fixture (with or without args)."""
+ # @pytest.fixture
+ if isinstance(decorator, ast.Attribute):
+ return (
+ isinstance(decorator.value, ast.Name)
+ and decorator.value.id == "pytest"
+ and decorator.attr == "fixture"
+ )
+ # @pytest.fixture() or @pytest.fixture(scope=...)
+ if isinstance(decorator, ast.Call):
+ return _is_fixture_decorator(decorator.func)
+ return False
+
+
+def find_module_level_fixture_redefinitions(
+ filepath: str,
+) -> list[tuple[int, str]]:
+ """Find module-level pytest fixtures that shadow root conftest definitions.
+
+ Returns list of (line_number, fixture_name) tuples.
+ """
+ try:
+ with open(filepath, "r", encoding="utf-8") as f:
+ source = f.read()
+ except (OSError, UnicodeDecodeError) as exc:
+ print(f"WARNING: could not read {filepath}: {exc}", file=sys.stderr)
+ return []
+
+ try:
+ tree = ast.parse(source, filename=filepath)
+ except SyntaxError as exc:
+ print(f"WARNING: could not parse {filepath}: {exc}", file=sys.stderr)
+ return []
+
+ violations = []
+ for node in ast.iter_child_nodes(tree):
+ # Only look at module-level function definitions
+ if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
+ continue
+ if node.name not in PROTECTED_FIXTURES:
+ continue
+ if any(_is_fixture_decorator(d) for d in node.decorator_list):
+ violations.append((node.lineno, node.name))
+
+ return violations
+
+
+def main() -> int:
+ if len(sys.argv) < 2:
+ return 0
+
+ new_violations: list[str] = []
+ allowlisted_warnings: list[str] = []
+
+ for filepath in sys.argv[1:]:
+ norm = _normalize(filepath)
+
+ # Only check files under tests/
+ if not norm.startswith("tests/"):
+ continue
+
+ # Skip the root conftest itself — it's the canonical source
+ if norm == "tests/conftest.py":
+ continue
+
+ violations = find_module_level_fixture_redefinitions(filepath)
+ if not violations:
+ continue
+
+ allowed = ALLOWLIST.get(norm, set())
+
+ for lineno, fixture_name in violations:
+ msg = f"{norm}:{lineno}: module-level redefinition of `{fixture_name}` fixture"
+ if fixture_name in allowed:
+ allowlisted_warnings.append(msg)
+ else:
+ new_violations.append(msg)
+
+ if allowlisted_warnings:
+ print(
+ "WARNING: The following files redefine root-conftest fixtures "
+ "(allowlisted, but please migrate):"
+ )
+ for w in allowlisted_warnings:
+ print(f" {w}")
+ print()
+
+ if new_violations:
+ print(
+ "ERROR: New module-level redefinitions of root-conftest fixtures detected!\n"
+ )
+ print(
+ "The root tests/conftest.py provides `app`, `client`, and "
+ "`authenticated_client` fixtures with security-relevant setup "
+ "(CSRF disable, auth DB init, temp data dir)."
+ )
+ print(
+ "Redefining these at module level skips that setup and may "
+ "create subtle test-environment differences.\n"
+ )
+ print("New violations:")
+ for v in new_violations:
+ print(f" {v}")
+ print(
+ "\nTo fix: remove the local fixture and use the shared one from "
+ "tests/conftest.py."
+ )
+ print(
+ "If you must keep it, add the file to the ALLOWLIST in "
+ ".pre-commit-hooks/check-fixture-duplication.py\n"
+ )
+ return 1
+
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())