diff options
author | Jason Chang <jasonnc@google.com> | 2023-07-14 16:45:35 -0700 |
---|---|---|
committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-08-02 18:29:05 +0000 |
commit | 32b59565b7bd41ec1a121869823557f0b2b022d7 (patch) | |
tree | 0cd0fe644ecc6e319df96861f26b77a55c9969eb /tests/test_subcmds_sync.py | |
parent | a6413f5d88f12466b3daa833668d0f59fc65ece4 (diff) | |
download | git-repo-32b59565b7bd41ec1a121869823557f0b2b022d7.tar.gz |
Refactor errors for sync command
Per discussion in go/repo-error-update updated aggregated and exit
errors for sync command.
Aggregated errors are errors that result in eventual command failure.
Exit errors are errors that result in immediate command failure.
Also updated main.py to log aggregated and exit errors to git sessions
log
Bug: b/293344017
Change-Id: I77a21f14da32fe2e68c16841feb22de72e86a251
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/379614
Reviewed-by: Aravind Vasudevan <aravindvasudev@google.com>
Tested-by: Jason Chang <jasonnc@google.com>
Commit-Queue: Jason Chang <jasonnc@google.com>
Diffstat (limited to 'tests/test_subcmds_sync.py')
-rw-r--r-- | tests/test_subcmds_sync.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index 057478ef..00c34852 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py | |||
@@ -17,12 +17,15 @@ import os | |||
17 | import shutil | 17 | import shutil |
18 | import tempfile | 18 | import tempfile |
19 | import unittest | 19 | import unittest |
20 | import time | ||
20 | from unittest import mock | 21 | from unittest import mock |
21 | 22 | ||
22 | import pytest | 23 | import pytest |
23 | 24 | ||
24 | import command | 25 | import command |
25 | from subcmds import sync | 26 | from subcmds import sync |
27 | from project import SyncNetworkHalfResult | ||
28 | from error import GitError, RepoExitError | ||
26 | 29 | ||
27 | 30 | ||
28 | @pytest.mark.parametrize( | 31 | @pytest.mark.parametrize( |
@@ -233,3 +236,83 @@ class GetPreciousObjectsState(unittest.TestCase): | |||
233 | self.assertFalse( | 236 | self.assertFalse( |
234 | self.cmd._GetPreciousObjectsState(self.project, self.opt) | 237 | self.cmd._GetPreciousObjectsState(self.project, self.opt) |
235 | ) | 238 | ) |
239 | |||
240 | |||
241 | class SyncCommand(unittest.TestCase): | ||
242 | """Tests for cmd.Execute.""" | ||
243 | |||
244 | def setUp(self): | ||
245 | """Common setup.""" | ||
246 | self.repodir = tempfile.mkdtemp(".repo") | ||
247 | self.manifest = manifest = mock.MagicMock( | ||
248 | repodir=self.repodir, | ||
249 | ) | ||
250 | |||
251 | git_event_log = mock.MagicMock(ErrorEvent=mock.Mock(return_value=None)) | ||
252 | self.outer_client = outer_client = mock.MagicMock() | ||
253 | outer_client.manifest.IsArchive = True | ||
254 | manifest.manifestProject.worktree = "worktree_path/" | ||
255 | manifest.repoProject.LastFetch = time.time() | ||
256 | self.sync_network_half_error = None | ||
257 | self.sync_local_half_error = None | ||
258 | self.cmd = sync.Sync( | ||
259 | manifest=manifest, | ||
260 | outer_client=outer_client, | ||
261 | git_event_log=git_event_log, | ||
262 | ) | ||
263 | |||
264 | def Sync_NetworkHalf(*args, **kwargs): | ||
265 | return SyncNetworkHalfResult(True, self.sync_network_half_error) | ||
266 | |||
267 | def Sync_LocalHalf(*args, **kwargs): | ||
268 | if self.sync_local_half_error: | ||
269 | raise self.sync_local_half_error | ||
270 | |||
271 | self.project = p = mock.MagicMock( | ||
272 | use_git_worktrees=False, | ||
273 | UseAlternates=False, | ||
274 | name="project", | ||
275 | Sync_NetworkHalf=Sync_NetworkHalf, | ||
276 | Sync_LocalHalf=Sync_LocalHalf, | ||
277 | RelPath=mock.Mock(return_value="rel_path"), | ||
278 | ) | ||
279 | p.manifest.GetProjectsWithName.return_value = [p] | ||
280 | |||
281 | mock.patch.object( | ||
282 | sync, | ||
283 | "_PostRepoFetch", | ||
284 | return_value=None, | ||
285 | ).start() | ||
286 | |||
287 | mock.patch.object( | ||
288 | self.cmd, "GetProjects", return_value=[self.project] | ||
289 | ).start() | ||
290 | |||
291 | opt, _ = self.cmd.OptionParser.parse_args([]) | ||
292 | opt.clone_bundle = False | ||
293 | opt.jobs = 4 | ||
294 | opt.quiet = True | ||
295 | opt.use_superproject = False | ||
296 | opt.current_branch_only = True | ||
297 | opt.optimized_fetch = True | ||
298 | opt.retry_fetches = 1 | ||
299 | opt.prune = False | ||
300 | opt.auto_gc = False | ||
301 | opt.repo_verify = False | ||
302 | self.opt = opt | ||
303 | |||
304 | def tearDown(self): | ||
305 | mock.patch.stopall() | ||
306 | |||
307 | def test_command_exit_error(self): | ||
308 | """Ensure unsuccessful commands raise expected errors.""" | ||
309 | self.sync_network_half_error = GitError( | ||
310 | "sync_network_half_error error", project=self.project | ||
311 | ) | ||
312 | self.sync_local_half_error = GitError( | ||
313 | "sync_local_half_error", project=self.project | ||
314 | ) | ||
315 | with self.assertRaises(RepoExitError) as e: | ||
316 | self.cmd.Execute(self.opt, []) | ||
317 | self.assertIn(self.sync_local_half_error, e.aggregate_errors) | ||
318 | self.assertIn(self.sync_network_half_error, e.aggregate_errors) | ||