Skip to content

Commit a8e210b

Browse files
Refactor garbage collection handling in tests
Refactor garbage collection test to mock gc.collect and ensure it is not called when repo is dropped. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent d7fd408 commit a8e210b

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

‎test/test_index.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ def test_adding_a_file_does_not_keep_the_repository_alive():
5858
# Repo.close() does not force a garbage collection, so staging must not leave a
5959
# reference cycle that delays releasing a dropped repository's Git processes.
6060
with fixture_repo("index.sh", "file", "testfile.txt", "content of testfile", write=True) as owner:
61+
gc_was_enabled = gc.isenabled()
6162
gc.disable()
6263
try:
63-
repo = Repo(owner.working_dir)
64-
repo.index.add(["testfile.txt"])
65-
dropped = weakref.ref(repo)
66-
del repo
67-
assert dropped() is None
64+
with mock.patch.object(gc, "collect") as collect:
65+
repo = Repo(owner.working_dir)
66+
repo.index.add(["testfile.txt"])
67+
dropped = weakref.ref(repo)
68+
del repo
69+
assert dropped() is None
70+
collect.assert_not_called()
6871
finally:
69-
gc.enable()
72+
if gc_was_enabled:
73+
gc.enable()
7074

7175

7276
@pytest.mark.skipif(os.name == "nt", reason="Windows symlink targets are Unicode paths")

0 commit comments

Comments
 (0)