Skip to content

Commit 9387645

Browse files
committed
tests(git): Adapt to verbatim run() output
why: Tests that read scalars via the low-level run() or thin cmd wrappers relied on the old implicit trim. what: - Strip rev-parse / symbolic-ref / is-shallow / version reads. - Invert the default-behavior test to assert verbatim output.
1 parent fc58df2 commit 9387645

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

tests/cmd/test_git.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def test_branch_cmd_create_checkout_parameter(
695695
branch_name = f"test-create-{test_id}"
696696

697697
# Record current branch before creating
698-
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
698+
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True).strip()
699699

700700
# Create branch using GitBranchCmd
701701
branch_cmd = git.GitBranchCmd(path=git_repo.path, branch_name=branch_name)
@@ -710,7 +710,7 @@ def test_branch_cmd_create_checkout_parameter(
710710
assert branch_name in branch_names
711711

712712
# Check if HEAD switched
713-
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
713+
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True).strip()
714714
if expect_switch:
715715
assert current_after == branch_name
716716
else:
@@ -736,7 +736,7 @@ def test_branch_manager_create_checkout_parameter(
736736
branch_name = f"test-mgr-create-{test_id}"
737737

738738
# Record current branch before creating
739-
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
739+
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True).strip()
740740

741741
# Create branch using GitBranchManager
742742
result = git_repo.cmd.branches.create(branch=branch_name, checkout=checkout)
@@ -750,7 +750,7 @@ def test_branch_manager_create_checkout_parameter(
750750
assert branch_name in branch_names
751751

752752
# Check if HEAD switched
753-
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
753+
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True).strip()
754754
if expect_switch:
755755
assert current_after == branch_name
756756
else:
@@ -1894,7 +1894,7 @@ def test_notes_get(git_repo: GitSync) -> None:
18941894
git_repo.cmd.notes.add(message="Test note for get", force=True)
18951895

18961896
# Get the HEAD revision
1897-
head_sha = git_repo.cmd.rev_parse(args="HEAD")
1897+
head_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
18981898

18991899
# Get the note by object_sha
19001900
note = git_repo.cmd.notes.get(object_sha=head_sha)
@@ -1920,7 +1920,7 @@ def test_notes_show(git_repo: GitSync) -> None:
19201920
git_repo.cmd.notes.add(message=note_message, force=True)
19211921

19221922
# Get the note
1923-
head_sha = git_repo.cmd.rev_parse(args="HEAD")
1923+
head_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
19241924
note = git_repo.cmd.notes.get(object_sha=head_sha)
19251925
assert note is not None
19261926

@@ -1936,7 +1936,7 @@ def test_notes_append(git_repo: GitSync) -> None:
19361936
git_repo.cmd.notes.add(message=initial_message, force=True)
19371937

19381938
# Get the note
1939-
head_sha = git_repo.cmd.rev_parse(args="HEAD")
1939+
head_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
19401940
note = git_repo.cmd.notes.get(object_sha=head_sha)
19411941
assert note is not None
19421942

@@ -1956,7 +1956,7 @@ def test_notes_remove(git_repo: GitSync) -> None:
19561956
git_repo.cmd.notes.add(message="Note to be removed", force=True)
19571957

19581958
# Get the note
1959-
head_sha = git_repo.cmd.rev_parse(args="HEAD")
1959+
head_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
19601960
note = git_repo.cmd.notes.get(object_sha=head_sha)
19611961
assert note is not None
19621962

@@ -1998,7 +1998,7 @@ def test_notes_edit(git_repo: GitSync, tmp_path: pathlib.Path) -> None:
19981998
git_repo.cmd.notes.add(message="Initial note for edit test", force=True)
19991999

20002000
# Get the note
2001-
head_sha = git_repo.cmd.rev_parse(args="HEAD")
2001+
head_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
20022002
note = git_repo.cmd.notes.get(object_sha=head_sha)
20032003
assert note is not None
20042004

@@ -2019,14 +2019,14 @@ def test_notes_copy(git_repo: GitSync) -> None:
20192019
git_repo.cmd.run(["commit", "-m", "Commit for copy note test"])
20202020

20212021
# Get the new commit SHA
2022-
new_commit_sha = git_repo.cmd.rev_parse(args="HEAD")
2022+
new_commit_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
20232023

20242024
# Checkout previous commit to add note there
20252025
git_repo.cmd.run(["checkout", "HEAD~1"])
20262026
git_repo.cmd.notes.add(message="Note to copy", force=True)
20272027

20282028
# Get the note and copy to new commit
2029-
old_commit_sha = git_repo.cmd.rev_parse(args="HEAD")
2029+
old_commit_sha = git_repo.cmd.rev_parse(args="HEAD").strip()
20302030
note = git_repo.cmd.notes.get(object_sha=old_commit_sha)
20312031
assert note is not None
20322032

@@ -2760,12 +2760,13 @@ def test_run_trim_false_preserves_blob(git_repo: GitSync) -> None:
27602760
assert blob == base
27612761

27622762

2763-
def test_run_default_trims_trailing_newline(git_repo: GitSync) -> None:
2764-
"""Default run() keeps the no-trailing-newline contract callers rely on."""
2765-
sha = git_repo.cmd.run(["rev-parse", "HEAD"])
2763+
def test_run_default_preserves_trailing_newline(git_repo: GitSync) -> None:
2764+
"""Default run() returns output verbatim, including the trailing newline."""
2765+
verbatim = git_repo.cmd.run(["rev-parse", "HEAD"])
27662766

2767-
assert "\n" not in sha
2768-
assert sha == sha.strip()
2767+
assert verbatim.endswith("\n")
2768+
# trim=True still yields the convenient bare value.
2769+
assert git_repo.cmd.run(["rev-parse", "HEAD"], trim=True) == verbatim.strip()
27692770

27702771

27712772
def test_run_failure_preserves_stderr_lines(git_repo: GitSync) -> None:

tests/sync/test_git.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_repo_git_obtain_full(
115115
git_repo: GitSync = constructor(**lazy_constructor_options(**locals()))
116116
git_repo.obtain()
117117

118-
test_repo_revision = run(["git", "rev-parse", "HEAD"], cwd=git_remote_repo)
118+
test_repo_revision = run(["git", "rev-parse", "HEAD"], cwd=git_remote_repo).strip()
119119

120120
assert git_repo.get_revision() == test_repo_revision
121121
assert (tmp_path / "myrepo").exists()
@@ -155,7 +155,7 @@ def test_git_shallow_and_tls_verify_kwargs_are_honored(
155155
["git", "rev-parse", "--is-shallow-repository"],
156156
cwd=tmp_path / "myrepo",
157157
)
158-
assert is_shallow == "true"
158+
assert is_shallow.strip() == "true"
159159

160160

161161
class DepthFixture(t.NamedTuple):
@@ -228,7 +228,7 @@ def test_obtain_honors_clone_depth(
228228
commit_count = run(["git", "rev-list", "--count", "HEAD"], cwd=checkout)
229229
is_shallow = run(["git", "rev-parse", "--is-shallow-repository"], cwd=checkout)
230230
assert int(commit_count) == expected_count
231-
assert is_shallow == ("true" if expected_shallow else "false")
231+
assert is_shallow.strip() == ("true" if expected_shallow else "false")
232232

233233

234234
@pytest.mark.parametrize(
@@ -800,7 +800,7 @@ def test_git_sync_remotes(git_repo: GitSync) -> None:
800800
remotes = git_repo.remotes()
801801

802802
assert "origin" in remotes
803-
assert git_repo.cmd.remotes.show() == "origin"
803+
assert git_repo.cmd.remotes.show().strip() == "origin"
804804
git_origin = git_repo.cmd.remotes.get(remote_name="origin")
805805
assert git_origin is not None
806806
assert "origin" in git_origin.show()
@@ -849,7 +849,7 @@ def test_set_remote(git_repo: GitSync, repo_name: str, new_repo_url: str) -> Non
849849

850850
def test_get_git_version(git_repo: GitSync) -> None:
851851
"""Test get_git_version()."""
852-
expected_version = git_repo.run(["--version"]).replace("git version ", "")
852+
expected_version = git_repo.run(["--version"]).replace("git version ", "").strip()
853853
assert git_repo.get_git_version()
854854
assert expected_version == git_repo.get_git_version()
855855

0 commit comments

Comments
 (0)