[PATCH b4 v2 26/44] tests: cover the restore from a detached HEAD
Christian Brauner <[email protected]> Fri, 31 Jul 2026 23:59:07 +0200
| Newsgroups | org.kernel.linux.tools |
|---|---|
| Message-ID | <20260731-work-b4-editor-branch-guard-v2-26-243fd19d322d@kernel.org> |
The helper names the branch when there is one and the commit when there is not, the session records it, and the review app puts HEAD back on the commit it started detached at. Signed-off-by: Christian Brauner (Amutable) <[email protected]> --- src/tests/test___init__.py | 30 +++++++++++++++++++++ src/tests/test_tui_review.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/tests/test___init__.py b/src/tests/test___init__.py index 9433606..b5e1f70 100644 --- a/src/tests/test___init__.py +++ b/src/tests/test___init__.py @@ -1352,3 +1352,33 @@ def test_edit_in_editor_reads_core_editor_from_topdir( assert b4.edit_in_editor(b'note\n', filehint='note.txt', topdir=other) == b'note\n' assert seen.read_text() == 'topdir' + + +def test_git_head_restore_args_names_the_branch(gitdir: str) -> None: + assert b4.git_head_restore_args(gitdir) == ['checkout', 'master'] + + +def test_git_head_restore_args_names_the_commit_when_detached(gitdir: str) -> None: + """A detached HEAD is a starting point too, and the only thing that can + be named for it is the commit.""" + ecode, out = b4.git_run_command(gitdir, ['rev-parse', 'HEAD']) + assert ecode == 0, out + sha = out.strip() + ecode, out = b4.git_run_command(gitdir, ['checkout', '-q', '--detach']) + assert ecode == 0, out + assert b4.git_head_restore_args(gitdir) == ['checkout', '--detach', sha] + + +def test_git_head_restore_args_round_trip(gitdir: str) -> None: + """Running what it returns puts HEAD back exactly where it was read.""" + ecode, out = b4.git_run_command(gitdir, ['checkout', '-q', '--detach']) + assert ecode == 0, out + restore = b4.git_head_restore_args(gitdir) + + ecode, out = b4.git_run_command(gitdir, ['checkout', '-q', '-b', 'elsewhere']) + assert ecode == 0, out + ecode, out = b4.git_run_command(gitdir, restore) + assert ecode == 0, out + + assert b4.git_get_current_branch(gitdir) is None + assert b4.git_head_restore_args(gitdir) == restore diff --git a/src/tests/test_tui_review.py b/src/tests/test_tui_review.py index 8ac6113..3d78ec3 100644 --- a/src/tests/test_tui_review.py +++ b/src/tests/test_tui_review.py @@ -662,3 +662,66 @@ class TestBranchRestore: app._restore_original_branch() assert b4.git_get_current_branch(gitdir) == 'master' assert app.branch_checked_out is False + + def test_restore_lands_back_on_a_detached_head(self, gitdir: str) -> None: + """A session that started detached has a commit to go back to, and a + branch name is not it -- so nothing used to put the user back.""" + branch, _shas = _create_review_branch_with_patches( + gitdir, 'restore-detached', ['patch 1'] + ) + ecode, _out = b4.git_run_command( + gitdir, ['checkout', '-q', '--detach', 'master'] + ) + assert ecode == 0 + ecode, out = b4.git_run_command(gitdir, ['rev-parse', 'HEAD']) + assert ecode == 0 + start_sha = out.strip() + + session = _build_session(gitdir, branch) + session['original_head'] = ['checkout', '--detach', start_sha] + app = ReviewApp(session) + assert app._ensure_branch_checked_out() + assert b4.git_get_current_branch(gitdir) == branch + + app._restore_original_branch() + assert b4.git_get_current_branch(gitdir) is None + ecode, out = b4.git_run_command(gitdir, ['rev-parse', 'HEAD']) + assert ecode == 0 + assert out.strip() == start_sha + assert app.branch_checked_out is False + + +class TestSessionRestorePoint: + """_prepare_review_session() records where HEAD has to go back to.""" + + def test_records_the_branch(self, gitdir: str) -> None: + import argparse + + branch, _shas = _create_review_branch_with_patches( + gitdir, 'session-head-branch', ['patch 1'] + ) + ecode, _out = b4.git_run_command(gitdir, ['checkout', '-q', 'master']) + assert ecode == 0 + session = b4.review._prepare_review_session(argparse.Namespace(branch=branch)) + assert session['original_branch'] == 'master' + assert session['original_head'] == ['checkout', 'master'] + + def test_records_a_detached_head(self, gitdir: str) -> None: + """Without the commit there is no name for where the user was, so the + review app has nothing to put them back on.""" + import argparse + + branch, _shas = _create_review_branch_with_patches( + gitdir, 'session-head-detached', ['patch 1'] + ) + ecode, _out = b4.git_run_command( + gitdir, ['checkout', '-q', '--detach', 'master'] + ) + assert ecode == 0 + ecode, out = b4.git_run_command(gitdir, ['rev-parse', 'HEAD']) + assert ecode == 0, out + sha = out.strip() + + session = b4.review._prepare_review_session(argparse.Namespace(branch=branch)) + assert session['original_branch'] is None + assert session['original_head'] == ['checkout', '--detach', sha] -- 2.53.0