Files
romm/backend/tests/utils/test_git_branch.py
zurdi 5f02a99fd3 fix: trust the repo root as a safe directory for git branch lookup
Addresses Copilot review feedback on PR #4617.

The Docker dev image's bind-mounted .git is host-owned while the
romm-dev container runs as root; git 2.35.2+ refuses that mismatch
as "dubious ownership" and rev-parse fails, so GIT_BRANCH silently
stayed null and the UI kept showing "development" in the very
workflow the mount was added for. Pass safe.directory scoped to
this one read-only invocation instead of touching global git config.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TDMqXzXexRgC89YxeNjn9q
2026-09-18 13:29:38 +02:00

53 lines
1.7 KiB
Python

import subprocess
from unittest.mock import patch
import pytest
from utils import _REPO_ROOT, get_git_branch
@pytest.fixture(autouse=True)
def _clear_branch_cache():
get_git_branch.cache_clear()
yield
get_git_branch.cache_clear()
def test_get_git_branch_returns_current_branch():
with patch("utils.subprocess.run") as mock_run:
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="feature/foo\n"
)
assert get_git_branch() == "feature/foo"
def test_get_git_branch_trusts_the_repo_root_as_a_safe_directory():
"""A host-owned .git bind-mounted into a root-run container is otherwise
refused by git 2.35.2+ as having "dubious ownership"."""
with patch("utils.subprocess.run") as mock_run:
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="main\n"
)
get_git_branch()
args = mock_run.call_args.args[0]
assert f"safe.directory={_REPO_ROOT}" in args
def test_get_git_branch_returns_none_when_git_unavailable():
with patch("utils.subprocess.run", side_effect=FileNotFoundError):
assert get_git_branch() is None
def test_get_git_branch_returns_none_on_detached_head():
with patch("utils.subprocess.run") as mock_run:
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="HEAD\n"
)
assert get_git_branch() is None
def test_get_git_branch_returns_none_when_not_a_git_repo():
with patch("utils.subprocess.run") as mock_run:
mock_run.side_effect = subprocess.CalledProcessError(128, ["git"])
assert get_git_branch() is None