Skip to content

Commit 60b4ab9

Browse files
add detached head unit test
1 parent ddeb4fe commit 60b4ab9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

bdiff/__init__.py

Whitespace-only changes.

bdiff/tests/__init__.py

Whitespace-only changes.

bdiff/tests/test_git_bdiff.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import subprocess
1313
import pytest
1414

15-
from git_bdiff import GitBDiff, GitBDiffError, GitBDiffNotGit, GitInfo
15+
from ..git_bdiff import GitBDiff, GitBDiffError, GitBDiffNotGit, GitInfo, GitBase
1616

1717

1818
# Disable warnings caused by the use of pytest fixtures
@@ -233,3 +233,30 @@ def test_is_main(git_repo):
233233
assert not info.is_main()
234234
else:
235235
assert info.is_main()
236+
237+
238+
def find_previous_hash():
239+
"""
240+
Loop over a git log output and extract a hash that isn't the current head
241+
"""
242+
243+
result = subprocess.run(["git", "log"], check=True, capture_output=True, text=True)
244+
for line in result.stdout.split("\n"):
245+
if line.startswith("commit") and "HEAD" not in line:
246+
return line.split()[1]
247+
248+
249+
def test_detached_head(git_repo):
250+
"""Test Detached Head State"""
251+
252+
os.chdir(git_repo)
253+
subprocess.run(["git", "checkout", "main"], check=True)
254+
255+
commit_hash = find_previous_hash()
256+
subprocess.run(["git", "checkout", commit_hash], check=True)
257+
258+
git_base = GitBase()
259+
assert git_base.get_branch_name() == git_base.detached_head_reference
260+
261+
262+

0 commit comments

Comments
 (0)