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.

refactor(hooks): narrow check-fixture-duplication to create_app() redefinitions (#3535)

LearningCircuitefa4cc2a7afa
diff --git a/.pre-commit-hooks/check-fixture-duplication.py b/.pre-commit-hooks/check-fixture-duplication.py
index 6555070a00..57c45b2dfc 100755
--- a/.pre-commit-hooks/check-fixture-duplication.py
+++ b/.pre-commit-hooks/check-fixture-duplication.py
@@ -4,11 +4,12 @@ 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.
+Full-app redefinitions (those calling `create_app()`) often skip that setup,
+which is the real harm. Fixtures that build a minimal `Flask(__name__)` app
+for blueprint isolation are intentionally different and are NOT flagged.
Class-level fixtures are allowed because they cannot easily inherit from
-conftest. Module-level redefinitions produce a WARNING for allowlisted
+conftest. Module-level redefinitions produce a WARNING for allowlisted
files (exit 0) and an ERROR for new occurrences (exit 1).
"""
@@ -17,123 +18,27 @@ import os
import sys
# Fixture names defined in tests/conftest.py that should not be redefined
-# at module level in individual test files.
+# at module level in individual test files when they use create_app().
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.
+# Existing violations — these files already redefine the fixtures using
+# create_app(). They emit a soft warning (exit 0) instead of blocking.
# 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/auth_tests/test_auth_integration.py": {"app", "client"},
+ "tests/auth_tests/test_auth_routes.py": {"app", "client"},
+ "tests/security/test_cookie_security.py": {"app", "client"},
+ "tests/web/test_error_handler_behavior.py": {"app", "client"},
"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",
- },
+ "tests/web/test_websocket_middleware.py": {"app", "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."""
+ """Normalize path to use forward slashes and strip a leading './'."""
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
@@ -152,11 +57,30 @@ def _is_fixture_decorator(decorator: ast.expr) -> bool:
return False
+def _calls_create_app(func_node: ast.AST) -> bool:
+ """Return True if the fixture body contains a call to `create_app`.
+
+ Matches `create_app(...)` (bare) and `module.create_app(...)` (attribute).
+ A fixture that only uses `Flask(__name__)` for blueprint isolation does
+ not trigger this and is treated as intentional, not a violation.
+ """
+ for node in ast.walk(func_node):
+ if not isinstance(node, ast.Call):
+ continue
+ func = node.func
+ if isinstance(func, ast.Name) and func.id == "create_app":
+ return True
+ if isinstance(func, ast.Attribute) and func.attr == "create_app":
+ return True
+ return False
+
+
def find_module_level_fixture_redefinitions(
filepath: str,
) -> list[tuple[int, str]]:
"""Find module-level pytest fixtures that shadow root conftest definitions.
+ Only full-app redefinitions (fixtures that call create_app) are reported.
Returns list of (line_number, fixture_name) tuples.
"""
try:
@@ -172,15 +96,38 @@ def find_module_level_fixture_redefinitions(
print(f"WARNING: could not parse {filepath}: {exc}", file=sys.stderr)
return []
+ # A `client`/`authenticated_client` fixture is flagged only when the file
+ # also has a local `app` fixture that uses create_app — because the client
+ # then inherits the problematic full-app without conftest security setup.
+ # If no such `app` exists (file uses a minimal Flask inside the client
+ # fixture itself), the client is flagged only if it calls create_app directly.
+ local_app_uses_create_app = False
+ for node in ast.iter_child_nodes(tree):
+ if (
+ isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
+ and node.name == "app"
+ and any(_is_fixture_decorator(d) for d in node.decorator_list)
+ and _calls_create_app(node)
+ ):
+ local_app_uses_create_app = True
+ break
+
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))
+ if not any(_is_fixture_decorator(d) for d in node.decorator_list):
+ continue
+
+ if node.name == "app":
+ if _calls_create_app(node):
+ violations.append((node.lineno, node.name))
+ else:
+ # client / authenticated_client
+ if _calls_create_app(node) or local_app_uses_create_app:
+ violations.append((node.lineno, node.name))
return violations
@@ -210,7 +157,7 @@ def main() -> int:
allowed = ALLOWLIST.get(norm, set())
for lineno, fixture_name in violations:
- msg = f"{norm}:{lineno}: module-level redefinition of `{fixture_name}` fixture"
+ msg = f"{norm}:{lineno}: module-level redefinition of `{fixture_name}` fixture (uses create_app)"
if fixture_name in allowed:
allowlisted_warnings.append(msg)
else:
@@ -235,8 +182,8 @@ def main() -> int:
"(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"
+ "Redefining these at module level with `create_app()` skips that "
+ "setup and may create subtle test-environment differences.\n"
)
print("New violations:")
for v in new_violations:
@@ -246,9 +193,14 @@ def main() -> int:
"tests/conftest.py."
)
print(
- "If you must keep it, add the file to the ALLOWLIST in "
+ "If this is intentional (e.g., testing a different app factory "
+ "configuration), add the file to the ALLOWLIST in "
".pre-commit-hooks/check-fixture-duplication.py\n"
)
+ print(
+ "Note: minimal `Flask(__name__)` fixtures for blueprint isolation "
+ "are NOT flagged — only fixtures calling create_app() are.\n"
+ )
return 1
return 0