diff options
| author | Gavin Mak <gavinmak@google.com> | 2026-04-20 17:57:20 +0000 |
|---|---|---|
| committer | gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2026-04-20 15:34:07 -0700 |
| commit | baa281d99e59dbf1447524e6fd95b384cadbc06e (patch) | |
| tree | 412dab50845510a6b4a802638c7d055955e37505 /tests | |
| parent | 7e9079b7cf79988dd978d04bfb79250564829cc3 (diff) | |
| download | git-repo-baa281d99e59dbf1447524e6fd95b384cadbc06e.tar.gz | |
sync: Refactor to use _RunOneGC and fix config leakagev2.63
Extract _RunOneGC to handle GC on a single project. This refactoring
makes it easier to invoke GC from parallel worker tasks.
Also, avoid modifying the passed-in config dictionary in _RunOneGC by
creating a local copy, preventing unintended side effects on other
commands sharing the same config.
Bug: 498290329
Change-Id: I7b77ed6629b14b5ee3322870b9c6c8ce2bfd6ea2
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/574923
Reviewed-by: Becky Siegel <beckysiegel@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_subcmds_sync.py | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index a6be0807f..b3726d2d1 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py | |||
| @@ -552,35 +552,36 @@ class GCProjectsTest(unittest.TestCase): | |||
| 552 | def test_GCProjects_skip_gc(self, mock_progress): | 552 | def test_GCProjects_skip_gc(self, mock_progress): |
| 553 | """Test that it skips GC if opt.auto_gc is False.""" | 553 | """Test that it skips GC if opt.auto_gc is False.""" |
| 554 | self.opt.auto_gc = False | 554 | self.opt.auto_gc = False |
| 555 | self.cmd._SetPreciousObjectsState = mock.Mock() | 555 | with mock.patch.object( |
| 556 | self.cmd._GCProjects([self.project], self.opt, None) | 556 | sync.Sync, "_SetPreciousObjectsState" |
| 557 | self.cmd._SetPreciousObjectsState.assert_called_once_with( | 557 | ) as mock_set_state: |
| 558 | self.project, self.opt | 558 | self.cmd._GCProjects([self.project], self.opt, None) |
| 559 | ) | 559 | mock_set_state.assert_called_once_with(self.project, self.opt) |
| 560 | self.assertFalse(self.project.bare_git.gc.called) | 560 | self.assertFalse(self.project.bare_git.gc.called) |
| 561 | 561 | ||
| 562 | @mock.patch("subcmds.sync.Progress") | 562 | @mock.patch("subcmds.sync.Progress") |
| 563 | def test_GCProjects_sequential(self, mock_progress): | 563 | def test_GCProjects_sequential(self, mock_progress): |
| 564 | """Test sequential GC (jobs < 2).""" | 564 | """Test sequential GC (jobs < 2).""" |
| 565 | self.cmd._SetPreciousObjectsState = mock.Mock() | 565 | with mock.patch.object(sync.Sync, "_SetPreciousObjectsState"): |
| 566 | self.cmd._GCProjects([self.project], self.opt, None) | 566 | self.cmd._GCProjects([self.project], self.opt, None) |
| 567 | self.project.config.SetString.assert_called_once_with( | 567 | self.project.bare_git.gc.assert_called_once_with( |
| 568 | "gc.autoDetach", "false" | 568 | "--auto", config={"gc.autoDetach": "false"} |
| 569 | ) | 569 | ) |
| 570 | self.project.bare_git.gc.assert_called_once_with("--auto") | 570 | # Verify that gc.autoDetach was not permanently set in config. |
| 571 | for call in self.project.config.SetString.call_args_list: | ||
| 572 | self.assertNotEqual(call.args[0], "gc.autoDetach") | ||
| 571 | 573 | ||
| 572 | @mock.patch("subcmds.sync.Progress") | 574 | @mock.patch("subcmds.sync.Progress") |
| 573 | def test_GCProjects_parallel(self, mock_progress): | 575 | def test_GCProjects_parallel(self, mock_progress): |
| 574 | """Test parallel GC (jobs >= 2).""" | 576 | """Test parallel GC (jobs >= 2).""" |
| 575 | self.opt.jobs = 2 | 577 | self.opt.jobs = 2 |
| 576 | self.cmd._SetPreciousObjectsState = mock.Mock() | 578 | with mock.patch.object(sync.Sync, "_SetPreciousObjectsState"): |
| 577 | 579 | with mock.patch("subcmds.sync._threading.Thread") as mock_thread: | |
| 578 | with mock.patch("subcmds.sync._threading.Thread") as mock_thread: | 580 | mock_t = mock.MagicMock() |
| 579 | mock_t = mock.MagicMock() | 581 | mock_thread.return_value = mock_t |
| 580 | mock_thread.return_value = mock_t | 582 | err_event = mock.Mock() |
| 581 | err_event = mock.Mock() | 583 | err_event.is_set.return_value = False |
| 582 | err_event.is_set.return_value = False | 584 | self.cmd._GCProjects([self.project], self.opt, err_event) |
| 583 | self.cmd._GCProjects([self.project], self.opt, err_event) | ||
| 584 | 585 | ||
| 585 | self.assertTrue(mock_thread.called) | 586 | self.assertTrue(mock_thread.called) |
| 586 | 587 | ||
