diff options
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) | ||
