diff options
| author | Mike Frysinger <vapier@google.com> | 2026-10-23 17:13:59 -0400 |
|---|---|---|
| committer | gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2026-10-23 17:00:18 -0700 |
| commit | f4ce9572d1f9e412a5685297d2806ce410e4d4a0 (patch) | |
| tree | cb85b68a49a5ce84832ee771230b3f9be5c3434b /tests | |
| parent | cc1f41d782b39d852dbd9c738ff01eb8814d9e86 (diff) | |
| download | git-repo-main.tar.gz git-repo-main.zip | |
Align with Google style guides and `import git_command` as a module
rather than import symbols out of it.
Change-Id: If80d95203e240dd6591e465ef75500c72401d2bf
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/648503
Commit-Queue: Mike Frysinger <vapier@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_git_refs.py | 17 | ||||
| -rw-r--r-- | tests/test_git_status.py | 11 | ||||
| -rw-r--r-- | tests/test_git_superproject.py | 10 | ||||
| -rw-r--r-- | tests/test_project.py | 88 | ||||
| -rw-r--r-- | tests/test_subcmds_cherry_pick.py | 20 | ||||
| -rw-r--r-- | tests/test_subcmds_rebase.py | 7 | ||||
| -rw-r--r-- | tests/test_subcmds_sync.py | 21 | ||||
| -rw-r--r-- | tests/test_subcmds_version.py | 5 |
8 files changed, 97 insertions, 82 deletions
diff --git a/tests/test_git_refs.py b/tests/test_git_refs.py index ab7bdbe97..108e855eb 100644 --- a/tests/test_git_refs.py +++ b/tests/test_git_refs.py @@ -23,6 +23,7 @@ from unittest import mock import pytest import utils_for_test +import git_command import git_refs @@ -99,8 +100,8 @@ def test_reads_head_with_root_refs_in_one_command( def Wait(self) -> int: return 0 - monkeypatch.setattr(git_refs, "GitCommand", FakeGitCommand) - monkeypatch.setattr(git_refs, "git_require", lambda _version: True) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "git_require", lambda _version: True) refs = git_refs.GitRefs("/nonexistent") with mock.patch.object(refs, "_ReadSymbolicRef") as read_head: assert refs.get("HEAD") == head @@ -131,8 +132,8 @@ def test_root_ref_snapshot_falls_back_for_unborn_head( def Wait(self) -> int: return 0 - monkeypatch.setattr(git_refs, "GitCommand", FakeGitCommand) - monkeypatch.setattr(git_refs, "git_require", lambda _version: True) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "git_require", lambda _version: True) refs = git_refs.GitRefs("/nonexistent") def read_head(name: str) -> None: @@ -161,8 +162,8 @@ def test_old_git_keeps_separate_head_fallback( def Wait(self) -> int: return 0 - monkeypatch.setattr(git_refs, "GitCommand", FakeGitCommand) - monkeypatch.setattr(git_refs, "git_require", lambda _version: False) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "git_require", lambda _version: False) refs = git_refs.GitRefs("/nonexistent") def read_head(name: str) -> None: @@ -194,8 +195,8 @@ def test_for_each_ref_failure_falls_back_to_symbolic_ref( def Wait(self) -> int: return 1 - monkeypatch.setattr(git_refs, "GitCommand", FakeGitCommand) - monkeypatch.setattr(git_refs, "git_require", lambda _version: True) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "git_require", lambda _version: True) refs = git_refs.GitRefs("/nonexistent") def read_head(name: str) -> None: diff --git a/tests/test_git_status.py b/tests/test_git_status.py index 72698db26..352735f2e 100644 --- a/tests/test_git_status.py +++ b/tests/test_git_status.py @@ -20,6 +20,7 @@ from unittest import mock import pytest +import git_command import git_status @@ -143,8 +144,8 @@ def test_get_status_uses_versioned_machine_options( def Wait(self) -> int: return 0 - monkeypatch.setattr(git_status, "GitCommand", FakeGitCommand) - monkeypatch.setattr(git_status, "git_require", lambda _version: True) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "git_require", lambda _version: True) git_status.GetStatus( mock.sentinel.project, @@ -173,7 +174,7 @@ def test_get_status_uses_versioned_machine_options( def test_get_status_rejects_git_before_2_11( monkeypatch: pytest.MonkeyPatch, ) -> None: - monkeypatch.setattr(git_status, "git_require", lambda _version: False) + monkeypatch.setattr(git_command, "git_require", lambda _version: False) with pytest.raises(git_status.UnsupportedStatusError): git_status.GetStatus(mock.sentinel.project, mock.sentinel.gitdir) @@ -194,9 +195,9 @@ def test_get_status_omits_stash_header_before_2_35( def Wait(self) -> int: return 0 - monkeypatch.setattr(git_status, "GitCommand", FakeGitCommand) + monkeypatch.setattr(git_command, "GitCommand", FakeGitCommand) monkeypatch.setattr( - git_status, + git_command, "git_require", lambda version: version <= (2, 34, 0), ) diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index c28eb8ded..6d7140aa8 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py @@ -465,7 +465,7 @@ class SuperprojectTestCase(unittest.TestCase): """Test _Init succeeds and creates the work git dir.""" self.assertFalse(os.path.exists(self._superproject._work_git)) with mock.patch( - "git_superproject.GitCommand", autospec=True + "git_command.GitCommand", autospec=True ) as mock_git_command: instance = mock_git_command.return_value instance.Wait.return_value = 0 @@ -492,7 +492,7 @@ class SuperprojectTestCase(unittest.TestCase): os.mkdir(self._superproject._superproject_path) os.mkdir(self._superproject._work_git) with mock.patch( - "git_superproject.GitCommand", autospec=True + "git_command.GitCommand", autospec=True ) as mock_git_command: self.assertTrue(self._superproject._Init()) mock_git_command.assert_not_called() @@ -500,7 +500,7 @@ class SuperprojectTestCase(unittest.TestCase): def test_Init_failure_cleans_up(self): """Test _Init cleans up the temporary directory if 'git init' fails.""" with mock.patch( - "git_superproject.GitCommand", autospec=True + "git_command.GitCommand", autospec=True ) as mock_git_command: instance = mock_git_command.return_value instance.Wait.return_value = 1 @@ -540,7 +540,7 @@ class SuperprojectTestCase(unittest.TestCase): os.mkdir(self._superproject._work_git) with mock.patch.object(self._superproject, "_Init", return_value=True): with mock.patch( - "git_superproject.GitCommand", autospec=True + "git_command.GitCommand", autospec=True ) as mock_git_command: with mock.patch.object( self._superproject, "_GetRef" @@ -600,7 +600,7 @@ class SuperprojectTestCase(unittest.TestCase): command = mock.MagicMock(stdout="1234\n") command.Wait.return_value = 0 with mock.patch( - "git_superproject.GitCommand", return_value=command + "git_command.GitCommand", return_value=command ) as git_command: self.assertEqual("1234", self._superproject._GetRef("HEAD")) diff --git a/tests/test_project.py b/tests/test_project.py index 4277136c7..648ddcdc5 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -216,7 +216,7 @@ class ProjectTests(unittest.TestCase): """Check UploadForReview emits repo.uploadstate trace2 data events.""" proj, _ = self._create_project_for_upload_test() - with mock.patch("project.GitCommand") as mock_git_cmd, mock.patch( + with mock.patch("git_command.GitCommand") as mock_git_cmd, mock.patch( "project.ReviewableBranch" ) as mock_rb_cls: mock_cmd = mock.MagicMock() @@ -256,7 +256,7 @@ class ProjectTests(unittest.TestCase): """Check event emission when stderr contains no review URLs.""" proj, _ = self._create_project_for_upload_test() - with mock.patch("project.GitCommand") as mock_git_cmd, mock.patch( + with mock.patch("git_command.GitCommand") as mock_git_cmd, mock.patch( "project.ReviewableBranch" ) as mock_rb_cls: mock_cmd = mock.MagicMock() @@ -290,7 +290,7 @@ class ProjectTests(unittest.TestCase): """Check UploadForReview succeeds when git_event_log is None.""" proj, _ = self._create_project_for_upload_test() - with mock.patch("project.GitCommand") as mock_git_cmd, mock.patch( + with mock.patch("git_command.GitCommand") as mock_git_cmd, mock.patch( "project.ReviewableBranch" ) as mock_rb_cls: mock_cmd = mock.MagicMock() @@ -313,7 +313,7 @@ class ProjectTests(unittest.TestCase): """Check exceptions during tracing are caught and do not fail upload.""" proj, _ = self._create_project_for_upload_test() - with mock.patch("project.GitCommand") as mock_git_cmd, mock.patch( + with mock.patch("git_command.GitCommand") as mock_git_cmd, mock.patch( "project.ReviewableBranch" ) as mock_rb_cls, mock.patch("project.logger") as mock_logger: mock_cmd = mock.MagicMock() @@ -338,7 +338,7 @@ class ProjectTests(unittest.TestCase): """Check integration with real git_trace2_event_log.EventLog.""" proj, _ = self._create_project_for_upload_test() - with mock.patch("project.GitCommand") as mock_git_cmd, mock.patch( + with mock.patch("git_command.GitCommand") as mock_git_cmd, mock.patch( "project.ReviewableBranch" ) as mock_rb_cls: mock_cmd = mock.MagicMock() @@ -408,9 +408,9 @@ class ProjectTests(unittest.TestCase): command.Wait.return_value = 0 with mock.patch.object( - project, "git_require", return_value=True + git_command, "git_require", return_value=True ), mock.patch.object( - project, "GitCommand", return_value=command + git_command, "GitCommand", return_value=command ) as cmd: self.assertEqual("1" * 40, git.ResolveCommit("topic")) @@ -437,7 +437,7 @@ class ProjectTests(unittest.TestCase): proj.name = "project" git = project.Project._GitGetByExec(proj, bare=True, gitdir="gitdir") - with mock.patch.object(project, "git_require", return_value=False): + with mock.patch.object(git_command, "git_require", return_value=False): with self.assertRaises(error.GitError): git.ResolveCommit("--not-a-revision") @@ -735,7 +735,9 @@ class ProjectTests(unittest.TestCase): proj._GetStatusSnapshot = mock.MagicMock(return_value=status) proj.HasStash = mock.MagicMock() - with mock.patch.object(project, "git_require", return_value=True): + with mock.patch.object( + git_command, "git_require", return_value=True + ): self.assertTrue(proj._HasDirtyOrStash()) proj.HasStash.assert_not_called() @@ -748,7 +750,9 @@ class ProjectTests(unittest.TestCase): proj._GetStatusSnapshot = mock.MagicMock(return_value=status) proj.HasStash = mock.MagicMock() - with mock.patch.object(project, "git_require", return_value=True): + with mock.patch.object( + git_command, "git_require", return_value=True + ): self.assertFalse(proj._HasDirtyOrStash()) proj.HasStash.assert_not_called() @@ -761,7 +765,9 @@ class ProjectTests(unittest.TestCase): proj._GetStatusSnapshot = mock.MagicMock(return_value=status) proj.HasStash = mock.MagicMock(return_value=True) - with mock.patch.object(project, "git_require", return_value=False): + with mock.patch.object( + git_command, "git_require", return_value=False + ): self.assertTrue(proj._HasDirtyOrStash()) proj._GetStatusSnapshot.assert_called_once_with( @@ -778,7 +784,7 @@ class ProjectTests(unittest.TestCase): for use_status in (True, False): with self.subTest(expected=expected, status=use_status): with mock.patch.object( - project, "git_require", return_value=use_status + git_command, "git_require", return_value=use_status ): self.assertEqual(expected, proj.GetDirtyAndHead()) @@ -819,7 +825,9 @@ class ProjectTests(unittest.TestCase): proj._IsDirtyLegacy = mock.MagicMock(return_value=False) proj.HasStash = mock.MagicMock(return_value=False) - with mock.patch.object(project, "git_require", return_value=True): + with mock.patch.object( + git_command, "git_require", return_value=True + ): self.assertFalse(proj._HasDirtyOrStash()) proj._GetStatusSnapshot.assert_called_once_with( @@ -834,7 +842,9 @@ class ProjectTests(unittest.TestCase): proj.work_git = mock.MagicMock() proj.work_git.DiffZ.side_effect = [{}, {"tracked": mock.sentinel}] - with mock.patch.object(project, "git_require", return_value=False): + with mock.patch.object( + git_command, "git_require", return_value=False + ): self.assertTrue(proj.IsDirty()) proj.work_git.update_index.assert_called_once_with( @@ -851,7 +861,7 @@ class ProjectTests(unittest.TestCase): with utils_for_test.TempGitTree() as tempdir: proj = _create_mock_project(tempdir) with mock.patch.object( - project, "GitCommand", autospec=True + git_command, "GitCommand", autospec=True ) as run_git: run_git.return_value.Wait.return_value = 0 proj._FastForward("revision") @@ -1339,7 +1349,7 @@ class ProjectTests(unittest.TestCase): hex41 = "a" * 41 proj.SetRevision(hex41) - with mock.patch("project.GitCommand") as mock_git: + with mock.patch("git_command.GitCommand") as mock_git: mock_cmd = mock.MagicMock() mock_cmd.Wait.return_value = 0 mock_git.return_value = mock_cmd @@ -2042,8 +2052,8 @@ def test_default_branch_fallback( project._DefaultBranchFallback.cache_clear() try: with mock.patch.object( - project, "git_require", return_value=supports_git_var - ), mock.patch.object(project, "GitCommand", FakeGitCommand): + git_command, "git_require", return_value=supports_git_var + ), mock.patch.object(git_command, "GitCommand", FakeGitCommand): assert project._DefaultBranchFallback() == expected_ref assert seen == expected_calls finally: @@ -2134,7 +2144,7 @@ def test_remote_fetch_retry_sleep( proj = _create_meta_project(tempdir, project_cls) proj.config.GetRemote("origin").ResetFetch() proj.revisionExpr = "refs/heads/stable" - with mock.patch.object(project, "GitCommand") as mock_git: + with mock.patch.object(git_command, "GitCommand") as mock_git: # What git does while stable is deleted. mock_git.return_value.Wait.return_value = 128 mock_git.return_value.stdout = ( @@ -2353,7 +2363,7 @@ class StatelessSyncTests(unittest.TestCase): proj.work_git = mock.MagicMock() syncbuf = project.SyncBuffer(proj.config) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -2702,7 +2712,7 @@ class SyncOptimizationTests(unittest.TestCase): if os.path.exists(shallow_path): os.unlink(shallow_path) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -2725,7 +2735,7 @@ class SyncOptimizationTests(unittest.TestCase): with open(os.path.join(proj.gitdir, "shallow"), "w") as f: f.write("") - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: res = proj._RemoteFetch( current_branch_only=True, depth=1, @@ -2748,7 +2758,7 @@ class SyncOptimizationTests(unittest.TestCase): with open(os.path.join(other.gitdir, "shallow"), "w") as f: f.write("") - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -2777,7 +2787,7 @@ class SyncOptimizationTests(unittest.TestCase): with open(os.path.join(other.gitdir, "shallow"), "w") as f: f.write("") - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: res = proj._RemoteFetch( current_branch_only=True, depth=1, @@ -2971,7 +2981,7 @@ class SyncOptimizationTests(unittest.TestCase): mock_remote.PreConnectFetch.return_value = True proj.GetRemote = mock.MagicMock(return_value=mock_remote) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -3010,7 +3020,7 @@ class SyncOptimizationTests(unittest.TestCase): mock_remote.PreConnectFetch.return_value = True proj.GetRemote = mock.MagicMock(return_value=mock_remote) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -3052,7 +3062,7 @@ class SyncOptimizationTests(unittest.TestCase): mock_remote.PreConnectFetch.return_value = True proj.GetRemote = mock.MagicMock(return_value=mock_remote) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -3100,7 +3110,7 @@ class SyncOptimizationTests(unittest.TestCase): mock_remote.PreConnectFetch.return_value = True proj.GetRemote = mock.MagicMock(return_value=mock_remote) - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -3202,7 +3212,7 @@ class SyncOptimizationTests(unittest.TestCase): return inst with mock.patch( - "project.GitCommand", side_effect=_fake_git_command + "git_command.GitCommand", side_effect=_fake_git_command ): res = proj._RemoteFetch(current_branch_only=True) @@ -3578,7 +3588,7 @@ class ReprojectCmdTests(unittest.TestCase): ) -> mock.MagicMock: """Run _Reproject against mocked Git; return the subprocess mock.""" with mock.patch( - "project.GitCommand", side_effect=self._git_command(**outputs) + "git_command.GitCommand", side_effect=self._git_command(**outputs) ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock(returncode=0, stdout="") proj._Reproject(self.REVID) @@ -3589,7 +3599,7 @@ class ReprojectCmdTests(unittest.TestCase): with utils_for_test.TempGitTree() as tempdir: proj = self._get_project(tempdir) with mock.patch( - "project.GitCommand", side_effect=self._git_command() + "git_command.GitCommand", side_effect=self._git_command() ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock(returncode=0, stdout="") proj._Reproject(self.REVID) @@ -3649,7 +3659,7 @@ class ReprojectCmdTests(unittest.TestCase): with utils_for_test.TempGitTree() as tempdir: proj = self._get_project(tempdir) with mock.patch( - "project.GitCommand", side_effect=self._git_command() + "git_command.GitCommand", side_effect=self._git_command() ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock( returncode=3, stdout="disk on fire\n" @@ -3665,7 +3675,7 @@ class ReprojectCmdTests(unittest.TestCase): proj = self._get_project(tempdir) proj.work_git.GetHead.side_effect = [self.HEAD_ID, self.REVID] with mock.patch( - "project.GitCommand", side_effect=self._git_command() + "git_command.GitCommand", side_effect=self._git_command() ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock(returncode=0, stdout="") with self.assertRaises(project.LocalSyncFail) as e: @@ -3683,7 +3693,7 @@ class ReprojectCmdTests(unittest.TestCase): self.REVID, # HEAD after command ] with mock.patch( - "project.GitCommand", side_effect=self._git_command() + "git_command.GitCommand", side_effect=self._git_command() ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock(returncode=0, stdout="") with self.assertRaises(project.LocalSyncFail) as e: @@ -3695,7 +3705,7 @@ class ReprojectCmdTests(unittest.TestCase): with utils_for_test.TempGitTree() as tempdir: proj = self._get_project(tempdir) with mock.patch( - "project.GitCommand", + "git_command.GitCommand", side_effect=self._git_command(diff="lib/a.c\n"), ), mock.patch("subprocess.run") as mock_run: mock_run.return_value = mock.MagicMock(returncode=0, stdout="") @@ -4361,9 +4371,9 @@ class DownloadPatchSetTests(unittest.TestCase): def test_download_patch_set_with_no_filter_and_no_tags(self) -> None: """Test DownloadPatchSet includes --no-filter and --no-tags.""" with mock.patch( - "project.git_require", return_value=True + "git_command.git_require", return_value=True ) as mock_git_require: - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance @@ -4389,8 +4399,8 @@ class DownloadPatchSetTests(unittest.TestCase): def test_download_patch_set_legacy_git_omits_no_filter(self) -> None: """Test DownloadPatchSet omits --no-filter on git < 2.17.""" - with mock.patch("project.git_require", return_value=False): - with mock.patch("project.GitCommand") as mock_git_cmd: + with mock.patch("git_command.git_require", return_value=False): + with mock.patch("git_command.GitCommand") as mock_git_cmd: mock_cmd_instance = mock.MagicMock() mock_cmd_instance.Wait.return_value = 0 mock_git_cmd.return_value = mock_cmd_instance diff --git a/tests/test_subcmds_cherry_pick.py b/tests/test_subcmds_cherry_pick.py index e454cb9e0..af5702668 100644 --- a/tests/test_subcmds_cherry_pick.py +++ b/tests/test_subcmds_cherry_pick.py @@ -19,7 +19,7 @@ from unittest import mock import pytest import error -from git_command import GitCommand +import git_command from subcmds import cherry_pick @@ -30,12 +30,12 @@ def test_resolve_reference_uses_one_typed_batch_request( commit = "tree " + "2" * 40 + "\n\nSubject 🚀\n\nBody\n" output = oid + " commit " + str(len(commit.encode("utf-8"))) + "\n" output += commit + "\n" - command = mock.create_autospec(GitCommand, instance=True) + command = mock.create_autospec(git_command.GitCommand, instance=True) command.stdout = output command.stderr = "" command.Wait.return_value = 0 - run_git = mock.create_autospec(GitCommand, return_value=command) - monkeypatch.setattr(cherry_pick, "GitCommand", run_git) + run_git = mock.create_autospec(git_command.GitCommand, return_value=command) + monkeypatch.setattr(git_command, "GitCommand", run_git) resolved, contents = cherry_pick.CherryPick()._ResolveReference("topic") @@ -54,14 +54,14 @@ def test_resolve_reference_uses_one_typed_batch_request( def test_resolve_reference_rejects_missing_object( monkeypatch: pytest.MonkeyPatch, ) -> None: - command = mock.create_autospec(GitCommand, instance=True) + command = mock.create_autospec(git_command.GitCommand, instance=True) command.stdout = "topic^{commit} missing\n" command.stderr = "" command.Wait.return_value = 0 monkeypatch.setattr( - cherry_pick, + git_command, "GitCommand", - mock.create_autospec(GitCommand, return_value=command), + mock.create_autospec(git_command.GitCommand, return_value=command), ) with pytest.raises(error.GitError, match="commit topic not found"): @@ -71,14 +71,14 @@ def test_resolve_reference_rejects_missing_object( def test_resolve_reference_rejects_ambiguous_object( monkeypatch: pytest.MonkeyPatch, ) -> None: - command = mock.create_autospec(GitCommand, instance=True) + command = mock.create_autospec(git_command.GitCommand, instance=True) command.stdout = "topic^{commit} ambiguous\n" command.stderr = "" command.Wait.return_value = 0 monkeypatch.setattr( - cherry_pick, + git_command, "GitCommand", - mock.create_autospec(GitCommand, return_value=command), + mock.create_autospec(git_command.GitCommand, return_value=command), ) with pytest.raises(error.GitError, match="commit topic not found"): diff --git a/tests/test_subcmds_rebase.py b/tests/test_subcmds_rebase.py index 201ecc516..46e3317d3 100644 --- a/tests/test_subcmds_rebase.py +++ b/tests/test_subcmds_rebase.py @@ -20,6 +20,7 @@ from types import SimpleNamespace from unittest import mock import error +import git_command from subcmds import rebase @@ -77,11 +78,11 @@ def test_execute_delegates_autostash_to_rebase() -> None: onto_manifest=False, this_manifest_only=False, ) - git_command = mock.MagicMock() - git_command.Wait.return_value = 0 + command = mock.MagicMock() + command.Wait.return_value = 0 with mock.patch.object( - rebase, "GitCommand", return_value=git_command + git_command, "GitCommand", return_value=command ) as run_git, contextlib.redirect_stdout(io.StringIO()): assert cmd.Execute(opt, []) == 0 diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index 4c54a9fc8..e695e8e4e 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py @@ -32,6 +32,7 @@ import pytest import command import error +import git_command import manifest_xml from project import SyncNetworkHalfResult from subcmds import sync @@ -1475,14 +1476,14 @@ class CheckForBloatedProjects(unittest.TestCase): self.project.GetRevisionId.assert_not_called() self.project.bare_git.count_objects.assert_not_called() - @mock.patch("subcmds.sync.git_require") + @mock.patch.object(git_command, "git_require") def test_git_version_unsupported(self, mock_git_require): """Test that it returns early if git version is unsupported.""" mock_git_require.return_value = False self.cmd._CheckForBloatedProjects([self.project], self.opt) self.assertFalse(self.cmd.git_event_log.ErrorEvent.called) - @mock.patch("subcmds.sync.git_require") + @mock.patch.object(git_command, "git_require") def test_no_projects(self, mock_git_require): """Test that it returns early if no projects have clone_depth.""" mock_git_require.return_value = True @@ -1490,7 +1491,7 @@ class CheckForBloatedProjects(unittest.TestCase): self.cmd._CheckForBloatedProjects([self.project], self.opt) self.assertFalse(self.cmd.git_event_log.ErrorEvent.called) - @mock.patch("subcmds.sync.git_require", return_value=True) + @mock.patch.object(git_command, "git_require", return_value=True) @mock.patch("subcmds.sync.Progress") def test_network_only_skips_check( self, mock_progress: mock.Mock, mock_git_require: mock.Mock @@ -1504,7 +1505,7 @@ class CheckForBloatedProjects(unittest.TestCase): mock_progress.assert_not_called() self.cmd.ExecuteInParallel.assert_not_called() - @mock.patch("subcmds.sync.git_require", return_value=True) + @mock.patch.object(git_command, "git_require", return_value=True) @mock.patch("subcmds.sync.Progress") def test_projects_without_worktree_excluded( self, mock_progress: mock.Mock, mock_git_require: mock.Mock @@ -1526,7 +1527,7 @@ class CheckForBloatedProjects(unittest.TestCase): mock_progress.assert_not_called() self.cmd.ExecuteInParallel.assert_not_called() - @mock.patch("subcmds.sync.git_require") + @mock.patch.object(git_command, "git_require") @mock.patch("subcmds.sync.Progress") def test_bloated_project_found(self, mock_progress, mock_git_require): """Test that it adds project to _bloated_projects.""" @@ -1549,7 +1550,7 @@ class CheckForBloatedProjects(unittest.TestCase): self.assertEqual(self.cmd._bloated_projects, ["project"]) - @mock.patch("subcmds.sync.git_require") + @mock.patch.object(git_command, "git_require") @mock.patch("subcmds.sync.Progress") def test_stateless_prune_excluded(self, mock_progress, mock_git_require): """Test that projects pruned for stateless sync are excluded.""" @@ -2847,7 +2848,7 @@ class SyncToSuperprojectRevTests(unittest.TestCase): def tearDown(self): shutil.rmtree(self.repodir) - @mock.patch("subcmds.sync.GitCommand") + @mock.patch.object(git_command, "GitCommand") def test_successful_sync(self, mock_git_command): """Test successful sync to superproject rev.""" mock_superproject = self.manifest.superproject @@ -2872,7 +2873,7 @@ class SyncToSuperprojectRevTests(unittest.TestCase): mock_update.assert_called_once() self.assertEqual(self.errors, []) - @mock.patch("subcmds.sync.GitCommand") + @mock.patch.object(git_command, "GitCommand") def test_parse_error(self, mock_git_command): """Test error when .supermanifest cannot be parsed.""" mock_superproject = self.manifest.superproject @@ -2890,7 +2891,7 @@ class SyncToSuperprojectRevTests(unittest.TestCase): ) self.assertIn("could not parse .supermanifest", str(e.exception)) - @mock.patch("subcmds.sync.GitCommand") + @mock.patch.object(git_command, "GitCommand") def test_read_error(self, mock_git_command): """Test error when reading .supermanifest fails.""" mock_superproject = self.manifest.superproject @@ -2917,7 +2918,7 @@ class SyncToSuperprojectRevTests(unittest.TestCase): ) self.assertIn("superproject not defined", str(e.exception)) - @mock.patch("subcmds.sync.GitCommand") + @mock.patch.object(git_command, "GitCommand") def test_sync_failure(self, mock_git_command): """Test error when superproject sync fails.""" mock_superproject = self.manifest.superproject diff --git a/tests/test_subcmds_version.py b/tests/test_subcmds_version.py index 72935e22b..37c94bd65 100644 --- a/tests/test_subcmds_version.py +++ b/tests/test_subcmds_version.py @@ -18,6 +18,7 @@ from unittest import mock import pytest +import git_command from subcmds import version @@ -26,7 +27,7 @@ def test_repo_version_uses_one_pretty_format_call( ) -> None: project = mock.MagicMock() project.bare_git.log.return_value = "v2.0-1-g12345678\nTue, 25 Aug\n" - monkeypatch.setattr(version, "git_require", lambda _version: True) + monkeypatch.setattr(git_command, "git_require", lambda _version: True) result = version.Version._RepoVersion(project) @@ -43,7 +44,7 @@ def test_repo_version_keeps_old_git_fallback( project = mock.MagicMock() project.bare_git.describe.return_value = "v2.0" project.bare_git.log.return_value = "Tue, 25 Aug" - monkeypatch.setattr(version, "git_require", lambda _version: False) + monkeypatch.setattr(git_command, "git_require", lambda _version: False) result = version.Version._RepoVersion(project) |
