diff options
| author | Jason Chang <jasonnc@google.com> | 2023-07-26 13:23:40 -0700 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-07-31 21:31:36 +0000 |
| commit | a6413f5d88f12466b3daa833668d0f59fc65ece4 (patch) | |
| tree | 25410555a8941c500fbd55a974476ace04198dca /tests | |
| parent | 8c35d948cfa76ec685ad36fb1cb3a0fcc749f428 (diff) | |
| download | git-repo-a6413f5d88f12466b3daa833668d0f59fc65ece4.tar.gz | |
Update errors to extend BaseRepoError
In order to better analyze and track repo errors, repo command failures
need to be tied to specific errors in repo source code.
Additionally a new GitCommandError was added to differentiate between
general git related errors to failed git commands. Git commands that opt
into verification will raise a GitCommandError if the command failed.
The first step in this process is a general error refactoring
Bug: b/293344017
Change-Id: I46944b1825ce892757c8dd3f7e2fab7e460760c0
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/380994
Commit-Queue: Jason Chang <jasonnc@google.com>
Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com>
Tested-by: Jason Chang <jasonnc@google.com>
Reviewed-by: Joanna Wang <jojwang@google.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_error.py | 18 | ||||
| -rw-r--r-- | tests/test_git_command.py | 51 |
2 files changed, 65 insertions, 4 deletions
diff --git a/tests/test_error.py b/tests/test_error.py index 784e2d57..2733ab8c 100644 --- a/tests/test_error.py +++ b/tests/test_error.py | |||
| @@ -19,6 +19,15 @@ import pickle | |||
| 19 | import unittest | 19 | import unittest |
| 20 | 20 | ||
| 21 | import error | 21 | import error |
| 22 | import project | ||
| 23 | import git_command | ||
| 24 | from subcmds import all_modules | ||
| 25 | |||
| 26 | imports = all_modules + [ | ||
| 27 | error, | ||
| 28 | project, | ||
| 29 | git_command, | ||
| 30 | ] | ||
| 22 | 31 | ||
| 23 | 32 | ||
| 24 | class PickleTests(unittest.TestCase): | 33 | class PickleTests(unittest.TestCase): |
| @@ -26,10 +35,11 @@ class PickleTests(unittest.TestCase): | |||
| 26 | 35 | ||
| 27 | def getExceptions(self): | 36 | def getExceptions(self): |
| 28 | """Return all our custom exceptions.""" | 37 | """Return all our custom exceptions.""" |
| 29 | for name in dir(error): | 38 | for entry in imports: |
| 30 | cls = getattr(error, name) | 39 | for name in dir(entry): |
| 31 | if isinstance(cls, type) and issubclass(cls, Exception): | 40 | cls = getattr(entry, name) |
| 32 | yield cls | 41 | if isinstance(cls, type) and issubclass(cls, Exception): |
| 42 | yield cls | ||
| 33 | 43 | ||
| 34 | def testExceptionLookup(self): | 44 | def testExceptionLookup(self): |
| 35 | """Make sure our introspection logic works.""" | 45 | """Make sure our introspection logic works.""" |
diff --git a/tests/test_git_command.py b/tests/test_git_command.py index c4c3a4c5..1e8beabc 100644 --- a/tests/test_git_command.py +++ b/tests/test_git_command.py | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | import re | 17 | import re |
| 18 | import os | 18 | import os |
| 19 | import subprocess | ||
| 19 | import unittest | 20 | import unittest |
| 20 | 21 | ||
| 21 | try: | 22 | try: |
| @@ -65,6 +66,56 @@ class GitCommandTest(unittest.TestCase): | |||
| 65 | ) | 66 | ) |
| 66 | 67 | ||
| 67 | 68 | ||
| 69 | class GitCommandWaitTest(unittest.TestCase): | ||
| 70 | """Tests the GitCommand class .Wait()""" | ||
| 71 | |||
| 72 | def setUp(self): | ||
| 73 | class MockPopen(object): | ||
| 74 | rc = 0 | ||
| 75 | |||
| 76 | def communicate( | ||
| 77 | self, input: str = None, timeout: float = None | ||
| 78 | ) -> [str, str]: | ||
| 79 | """Mock communicate fn.""" | ||
| 80 | return ["", ""] | ||
| 81 | |||
| 82 | def wait(self, timeout=None): | ||
| 83 | return self.rc | ||
| 84 | |||
| 85 | self.popen = popen = MockPopen() | ||
| 86 | |||
| 87 | def popen_mock(*args, **kwargs): | ||
| 88 | return popen | ||
| 89 | |||
| 90 | def realpath_mock(val): | ||
| 91 | return val | ||
| 92 | |||
| 93 | mock.patch.object(subprocess, "Popen", side_effect=popen_mock).start() | ||
| 94 | |||
| 95 | mock.patch.object( | ||
| 96 | os.path, "realpath", side_effect=realpath_mock | ||
| 97 | ).start() | ||
| 98 | |||
| 99 | def tearDown(self): | ||
| 100 | mock.patch.stopall() | ||
| 101 | |||
| 102 | def test_raises_when_verify_non_zero_result(self): | ||
| 103 | self.popen.rc = 1 | ||
| 104 | r = git_command.GitCommand(None, ["status"], verify_command=True) | ||
| 105 | with self.assertRaises(git_command.GitCommandError): | ||
| 106 | r.Wait() | ||
| 107 | |||
| 108 | def test_returns_when_no_verify_non_zero_result(self): | ||
| 109 | self.popen.rc = 1 | ||
| 110 | r = git_command.GitCommand(None, ["status"], verify_command=False) | ||
| 111 | self.assertEqual(1, r.Wait()) | ||
| 112 | |||
| 113 | def test_default_returns_non_zero_result(self): | ||
| 114 | self.popen.rc = 1 | ||
| 115 | r = git_command.GitCommand(None, ["status"]) | ||
| 116 | self.assertEqual(1, r.Wait()) | ||
| 117 | |||
| 118 | |||
| 68 | class GitCallUnitTest(unittest.TestCase): | 119 | class GitCallUnitTest(unittest.TestCase): |
| 69 | """Tests the _GitCall class (via git_command.git).""" | 120 | """Tests the _GitCall class (via git_command.git).""" |
| 70 | 121 | ||
