-
Notifications
You must be signed in to change notification settings - Fork 4
perf: cache or index TestSuiteGate relevant-test discovery #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,61 @@ | ||
| import ast | ||
| from pathlib import Path | ||
| from typing import Dict, List, Optional | ||
|
|
||
| # Global cache for module -> relevant tests mapping | ||
| # Structure: {project_root_str: {target_module: [test_paths]}} | ||
| _TEST_DISCOVERY_CACHE: Dict[str, Dict[str, List[Path]]] = {} | ||
|
|
||
|
|
||
| class TestSuiteGate: | ||
| def __init__(self, search_root: Path, project_root: Optional[Path] = None): | ||
| self.search_root = search_root | ||
| self.project_root = project_root | ||
|
|
||
|
Comment on lines
+10
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Duplicate class definition missing This
The caching logic should be integrated into the existing class at line 73, not defined as a separate class. Based on learnings: "BaseCheck ABC must have 3 implementations: SyntaxVerifier, ImportIntegrityVerifier, and TestSuiteGate in verification/engine.py" 🤖 Prompt for AI Agents |
||
| def _imports_module(self, test_file: Path, target_module: str) -> bool: | ||
| try: | ||
| source_code = test_file.read_text(encoding="utf-8") | ||
| tree = ast.parse(source_code) | ||
|
|
||
| for node in ast.walk(tree): | ||
| if isinstance(node, ast.Import): | ||
| for alias in node.names: | ||
| if target_module in alias.name: | ||
| return True | ||
| elif isinstance(node, ast.ImportFrom): | ||
| if node.module and target_module in node.module: | ||
| return True | ||
|
Comment on lines
+21
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Substring matching produces false positives. Using The existing implementation at line 197-201 correctly uses exact matching: # Correct approach (from existing code)
if alias.name == module_name or alias.name.startswith(module_name + "."):🤖 Prompt for AI Agents |
||
| except Exception: | ||
| pass | ||
| return False | ||
|
Comment on lines
+28
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid silent exception swallowing. Catching all exceptions and silently passing hides AST parsing errors, encoding issues, and other problems that would be valuable for debugging. At minimum, log the exception. Proposed fix+ import logging
+ logger = logging.getLogger(__name__)
+ # ...
except Exception:
- pass
+ logger.debug("Failed to parse %s for import detection", test_file, exc_info=True)
return False🧰 Tools🪛 Ruff (0.15.7)[error] 28-29: (S110) [warning] 28-28: Do not catch blind exception: (BLE001) 🤖 Prompt for AI Agents |
||
|
|
||
| def _find_relevant_tests(self, target_module: str) -> List[Path]: | ||
| cache_key = str(self.project_root.resolve()) if self.project_root else str(self.search_root.resolve()) | ||
|
|
||
| # Check cache | ||
| if cache_key in _TEST_DISCOVERY_CACHE and target_module in _TEST_DISCOVERY_CACHE[cache_key]: | ||
| return _TEST_DISCOVERY_CACHE[cache_key][target_module] | ||
|
|
||
| relevant_tests = [] | ||
| ignore_patterns = {"venv", ".venv", "site-packages", "node_modules", ".git", ".tox", ".pytest_cache", "__pycache__"} | ||
|
|
||
| for p in self.search_root.rglob("*.py"): | ||
| # Skip obvious non-target paths if project_root is set | ||
| if self.project_root: | ||
| # Check if any parent directory is in the ignore list | ||
| if any(part in ignore_patterns for part in p.parts): | ||
| continue | ||
|
|
||
| if p.is_file() and p.name.startswith("test_"): | ||
| if self._imports_module(p, target_module): | ||
| relevant_tests.append(p) | ||
|
Comment on lines
+42
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic issues: Inconsistent filtering and incomplete test file detection.
Proposed fix for test pattern- if p.is_file() and p.name.startswith("test_"):
+ if p.is_file() and (p.name.startswith("test_") or p.name.endswith("_test.py")):🤖 Prompt for AI Agents |
||
|
|
||
| # Update cache | ||
| if cache_key not in _TEST_DISCOVERY_CACHE: | ||
| _TEST_DISCOVERY_CACHE[cache_key] = {} | ||
|
|
||
| _TEST_DISCOVERY_CACHE[cache_key][target_module] = relevant_tests | ||
| return relevant_tests | ||
| """TestSuiteGate — Check 3: run relevant tests against transformed code.""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Module docstring is misplaced due to code prepending. The module docstring should be at the top of the file (before imports). Its current position confirms that lines 1-58 were incorrectly prepended to the existing file content. 🤖 Prompt for AI Agents |
||
|
|
||
| import ast | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: New code was prepended without integrating with existing file content.
The pipeline failures (F811, E402) confirm this file now has duplicate imports and a duplicate
TestSuiteGateclass definition. The new caching logic in lines 1-58 must be merged into the existing class at line 73, not added as a separate block.Additionally, the module-level
_TEST_DISCOVERY_CACHEis unbounded and will grow indefinitely in long-running processes. Consider adding a max-size eviction policy or usingfunctools.lru_cache.🧰 Tools
🪛 GitHub Actions: Pre-commit
[error] 1-1: pre-commit hook
blackfailed and modified files (reformattedrefactron/verification/checks/test_gate.py).🤖 Prompt for AI Agents