Skip to content

Commit 46e9c1f

Browse files
committed
tests/cmd/git(test): Add tests for branch create checkout parameter
why: Commit 4b8a0f7 changed create() to use 'git branch' instead of 'checkout -b', adding an optional checkout parameter. Tests verify the new behavior: HEAD stays on original branch unless checkout=True. what: - Add BranchCreateFixture NamedTuple with test_id pattern - Add parameterized tests for GitBranchCmd.create() checkout behavior - Add parameterized tests for GitBranchManager.create() checkout behavior - Fix stash pop() doctests to match actual git output format
1 parent 12065f6 commit 46e9c1f

2 files changed

Lines changed: 106 additions & 3 deletions

File tree

src/libvcs/cmd/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4891,13 +4891,13 @@ def pop(
48914891
'No stash entries found.'
48924892
48934893
>>> GitStashCmd(path=example_git_repo.path).pop(stash=0)
4894-
'error: refs/stash@{0} is not a valid reference'
4894+
'error: stash@{0} is not a valid reference'
48954895
48964896
>>> GitStashCmd(path=example_git_repo.path).pop(stash=1, index=True)
4897-
'error: refs/stash@{1} is not a valid reference'
4897+
'error: stash@{1} is not a valid reference'
48984898
48994899
>>> GitStashCmd(path=example_git_repo.path).pop(stash=1, quiet=True)
4900-
'error: refs/stash@{1} is not a valid reference'
4900+
'error: stash@{1} is not a valid reference'
49014901
49024902
>>> GitStashCmd(path=example_git_repo.path).push(path='.')
49034903
'No local changes to save'

tests/cmd/test_git.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,109 @@ def test_branch_ls_filters(git_repo: GitSync) -> None:
533533
assert any(b.branch_name == "master" for b in verbose_branches)
534534

535535

536+
class BranchCreateFixture(t.NamedTuple):
537+
"""Test fixture for GitBranchCmd.create() and GitBranchManager.create()."""
538+
539+
test_id: str
540+
checkout: bool
541+
expect_switch: bool
542+
543+
544+
BRANCH_CREATE_FIXTURES: list[BranchCreateFixture] = [
545+
BranchCreateFixture(
546+
test_id="create-without-checkout",
547+
checkout=False,
548+
expect_switch=False,
549+
),
550+
BranchCreateFixture(
551+
test_id="create-with-checkout",
552+
checkout=True,
553+
expect_switch=True,
554+
),
555+
]
556+
557+
558+
@pytest.mark.parametrize(
559+
list(BranchCreateFixture._fields),
560+
BRANCH_CREATE_FIXTURES,
561+
ids=[test.test_id for test in BRANCH_CREATE_FIXTURES],
562+
)
563+
def test_branch_cmd_create_checkout_parameter(
564+
git_repo: GitSync,
565+
test_id: str,
566+
checkout: bool,
567+
expect_switch: bool,
568+
) -> None:
569+
"""Test GitBranchCmd.create() checkout parameter behavior.
570+
571+
Verifies commit 4b8a0f7: create() uses 'git branch' instead of 'checkout -b',
572+
and only switches HEAD when checkout=True.
573+
"""
574+
branch_name = f"test-create-{test_id}"
575+
576+
# Record current branch before creating
577+
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
578+
579+
# Create branch using GitBranchCmd
580+
branch_cmd = git.GitBranchCmd(path=git_repo.path, branch_name=branch_name)
581+
result = branch_cmd.create(checkout=checkout)
582+
583+
# Should succeed (empty string or no fatal error)
584+
assert "fatal" not in result.lower() or "already exists" in result.lower()
585+
586+
# Verify branch was created
587+
branches = git_repo.cmd.branches.ls()
588+
branch_names = [b.branch_name for b in branches]
589+
assert branch_name in branch_names
590+
591+
# Check if HEAD switched
592+
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
593+
if expect_switch:
594+
assert current_after == branch_name
595+
else:
596+
assert current_after == current_before
597+
598+
599+
@pytest.mark.parametrize(
600+
list(BranchCreateFixture._fields),
601+
BRANCH_CREATE_FIXTURES,
602+
ids=[test.test_id for test in BRANCH_CREATE_FIXTURES],
603+
)
604+
def test_branch_manager_create_checkout_parameter(
605+
git_repo: GitSync,
606+
test_id: str,
607+
checkout: bool,
608+
expect_switch: bool,
609+
) -> None:
610+
"""Test GitBranchManager.create() checkout parameter behavior.
611+
612+
Verifies commit 4b8a0f7: create() uses 'git branch' instead of 'checkout -b',
613+
and only switches HEAD when checkout=True.
614+
"""
615+
branch_name = f"test-mgr-create-{test_id}"
616+
617+
# Record current branch before creating
618+
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
619+
620+
# Create branch using GitBranchManager
621+
result = git_repo.cmd.branches.create(branch=branch_name, checkout=checkout)
622+
623+
# Should succeed (empty string or no fatal error)
624+
assert "fatal" not in result.lower() or "already exists" in result.lower()
625+
626+
# Verify branch was created
627+
branches = git_repo.cmd.branches.ls()
628+
branch_names = [b.branch_name for b in branches]
629+
assert branch_name in branch_names
630+
631+
# Check if HEAD switched
632+
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
633+
if expect_switch:
634+
assert current_after == branch_name
635+
else:
636+
assert current_after == current_before
637+
638+
536639
# =============================================================================
537640
# GitRemoteCmd Tests
538641
# =============================================================================

0 commit comments

Comments
 (0)