test(project-scanner): harden git helper execution

Agent-Logs-Url: https://github.com/MemPalace/mempalace/sessions/3c277c46-20b3-4a43-8eb7-8ee2eb3cb55a

Co-authored-by: igorls <4753812+igorls@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-24 03:52:37 +00:00
committed by GitHub
parent ec9084f4d8
commit d4cc367261
2 changed files with 20 additions and 8 deletions
+4
View File
@@ -170,6 +170,7 @@ MANIFEST_PRIORITY = {
"Cargo.toml": 2, "Cargo.toml": 2,
"go.mod": 3, "go.mod": 3,
} }
# Sentinel so unknown manifests always sort after the known manifest types above.
UNKNOWN_MANIFEST_PRIORITY = 999 UNKNOWN_MANIFEST_PRIORITY = 999
MANIFEST_PARSERS = { MANIFEST_PARSERS = {
"package.json": _parse_package_json, "package.json": _parse_package_json,
@@ -311,6 +312,9 @@ def _has_git_marker(path: Path) -> bool:
def _manifest_sort_key(entry: tuple[str, str, Path], repo_root: Path) -> tuple[int, int, str]: def _manifest_sort_key(entry: tuple[str, str, Path], repo_root: Path) -> tuple[int, int, str]:
"""Sort manifests by shallowest path first, then known manifest priority,
then lexicographic path for deterministic tie-breaking.
"""
manifest_file, _project_name, manifest_dir = entry manifest_file, _project_name, manifest_dir = entry
try: try:
rel = manifest_dir.relative_to(repo_root) rel = manifest_dir.relative_to(repo_root)
+16 -8
View File
@@ -27,7 +27,9 @@ from mempalace.project_scanner import (
to_detected_dict, to_detected_dict,
) )
GIT_ENV_ALLOWLIST = ("PATH", "HOME", "SystemRoot", "ComSpec", "TMPDIR", "TEMP", "TMP") # Keep only a small portability-focused allowlist for git subprocesses in tests.
GIT_ENV_ALLOWLIST = ("HOME", "SystemRoot", "ComSpec", "TMPDIR", "TEMP", "TMP")
GIT_EXECUTABLE = shutil.which("git")
# ── manifest parsers ──────────────────────────────────────────────────── # ── manifest parsers ────────────────────────────────────────────────────
@@ -219,7 +221,7 @@ def test_find_git_repos_empty_dir(tmp_path):
def _require_git() -> None: def _require_git() -> None:
if shutil.which("git") is None: if GIT_EXECUTABLE is None:
pytest.skip("git executable not available") pytest.skip("git executable not available")
@@ -237,23 +239,29 @@ def _git_test_env(name: str, email: str) -> dict[str, str]:
return env return env
def _git(*args: str) -> list[str]:
_require_git()
assert GIT_EXECUTABLE is not None
return [GIT_EXECUTABLE, *args]
def _git_commit( def _git_commit(
path: Path, filename: str, content: str, message: str, name: str, email: str path: Path, filename: str, content: str, message: str, name: str, email: str
) -> None: ) -> None:
_require_git() _require_git()
env = _git_test_env(name, email) env = _git_test_env(name, email)
(path / filename).write_text(content) (path / filename).write_text(content)
subprocess.run(["git", "add", filename], cwd=path, check=True, env=env) subprocess.run(_git("add", filename), cwd=path, check=True, env=env)
subprocess.run(["git", "commit", "-q", "-m", message], cwd=path, check=True, env=env) subprocess.run(_git("commit", "-q", "-m", message), cwd=path, check=True, env=env)
def _init_git_repo(path: Path, name: str = "Jane Doe", email: str = "jane@example.com"): def _init_git_repo(path: Path, name: str = "Jane Doe", email: str = "jane@example.com"):
"""Helper: init a git repo with one commit.""" """Helper: init a git repo with one commit."""
_require_git() _require_git()
subprocess.run(["git", "init", "-q"], cwd=path, check=True) subprocess.run(_git("init", "-q"), cwd=path, check=True)
subprocess.run(["git", "config", "user.name", name], cwd=path, check=True) subprocess.run(_git("config", "user.name", name), cwd=path, check=True)
subprocess.run(["git", "config", "user.email", email], cwd=path, check=True) subprocess.run(_git("config", "user.email", email), cwd=path, check=True)
subprocess.run(["git", "config", "commit.gpgsign", "false"], cwd=path, check=True) subprocess.run(_git("config", "commit.gpgsign", "false"), cwd=path, check=True)
_git_commit(path, "README.md", "hello", "initial", name, email) _git_commit(path, "README.md", "hello", "initial", name, email)