diff options
Diffstat (limited to 'tests/test_subcmds_sync.py')
-rw-r--r-- | tests/test_subcmds_sync.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index e7213ed9..9cd19f10 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py | |||
@@ -527,6 +527,108 @@ class SyncCommand(unittest.TestCase): | |||
527 | self.assertIn(self.sync_network_half_error, e.aggregate_errors) | 527 | self.assertIn(self.sync_network_half_error, e.aggregate_errors) |
528 | 528 | ||
529 | 529 | ||
530 | class SyncUpdateRepoProject(unittest.TestCase): | ||
531 | """Tests for Sync._UpdateRepoProject.""" | ||
532 | |||
533 | def setUp(self): | ||
534 | """Common setup.""" | ||
535 | self.repodir = tempfile.mkdtemp(".repo") | ||
536 | self.manifest = manifest = mock.MagicMock(repodir=self.repodir) | ||
537 | # Create a repoProject with a mock Sync_NetworkHalf. | ||
538 | repoProject = mock.MagicMock(name="repo") | ||
539 | repoProject.Sync_NetworkHalf = mock.Mock( | ||
540 | return_value=SyncNetworkHalfResult(True, None) | ||
541 | ) | ||
542 | manifest.repoProject = repoProject | ||
543 | manifest.IsArchive = False | ||
544 | manifest.CloneFilter = None | ||
545 | manifest.PartialCloneExclude = None | ||
546 | manifest.CloneFilterForDepth = None | ||
547 | |||
548 | git_event_log = mock.MagicMock(ErrorEvent=mock.Mock(return_value=None)) | ||
549 | self.cmd = sync.Sync(manifest=manifest, git_event_log=git_event_log) | ||
550 | |||
551 | opt, _ = self.cmd.OptionParser.parse_args([]) | ||
552 | opt.local_only = False | ||
553 | opt.repo_verify = False | ||
554 | opt.verbose = False | ||
555 | opt.quiet = True | ||
556 | opt.force_sync = False | ||
557 | opt.clone_bundle = False | ||
558 | opt.tags = False | ||
559 | opt.optimized_fetch = False | ||
560 | opt.retry_fetches = 0 | ||
561 | opt.prune = False | ||
562 | self.opt = opt | ||
563 | self.errors = [] | ||
564 | |||
565 | mock.patch.object(sync.Sync, "_GetCurrentBranchOnly").start() | ||
566 | |||
567 | def tearDown(self): | ||
568 | shutil.rmtree(self.repodir) | ||
569 | mock.patch.stopall() | ||
570 | |||
571 | def test_fetches_when_stale(self): | ||
572 | """Test it fetches when the repo project is stale.""" | ||
573 | self.manifest.repoProject.LastFetch = time.time() - ( | ||
574 | sync._ONE_DAY_S + 1 | ||
575 | ) | ||
576 | |||
577 | with mock.patch.object(sync, "_PostRepoFetch") as mock_post_fetch: | ||
578 | self.cmd._UpdateRepoProject(self.opt, self.manifest, self.errors) | ||
579 | self.manifest.repoProject.Sync_NetworkHalf.assert_called_once() | ||
580 | mock_post_fetch.assert_called_once() | ||
581 | self.assertEqual(self.errors, []) | ||
582 | |||
583 | def test_skips_when_fresh(self): | ||
584 | """Test it skips fetch when repo project is fresh.""" | ||
585 | self.manifest.repoProject.LastFetch = time.time() | ||
586 | |||
587 | with mock.patch.object(sync, "_PostRepoFetch") as mock_post_fetch: | ||
588 | self.cmd._UpdateRepoProject(self.opt, self.manifest, self.errors) | ||
589 | self.manifest.repoProject.Sync_NetworkHalf.assert_not_called() | ||
590 | mock_post_fetch.assert_not_called() | ||
591 | |||
592 | def test_skips_local_only(self): | ||
593 | """Test it does nothing with --local-only.""" | ||
594 | self.opt.local_only = True | ||
595 | self.manifest.repoProject.LastFetch = time.time() - ( | ||
596 | sync._ONE_DAY_S + 1 | ||
597 | ) | ||
598 | |||
599 | with mock.patch.object(sync, "_PostRepoFetch") as mock_post_fetch: | ||
600 | self.cmd._UpdateRepoProject(self.opt, self.manifest, self.errors) | ||
601 | self.manifest.repoProject.Sync_NetworkHalf.assert_not_called() | ||
602 | mock_post_fetch.assert_not_called() | ||
603 | |||
604 | def test_post_repo_fetch_skipped_on_env_var(self): | ||
605 | """Test _PostRepoFetch is skipped when REPO_SKIP_SELF_UPDATE is set.""" | ||
606 | self.manifest.repoProject.LastFetch = time.time() | ||
607 | |||
608 | with mock.patch.dict(os.environ, {"REPO_SKIP_SELF_UPDATE": "1"}): | ||
609 | with mock.patch.object(sync, "_PostRepoFetch") as mock_post_fetch: | ||
610 | self.cmd._UpdateRepoProject( | ||
611 | self.opt, self.manifest, self.errors | ||
612 | ) | ||
613 | mock_post_fetch.assert_not_called() | ||
614 | |||
615 | def test_fetch_failure_is_handled(self): | ||
616 | """Test that a fetch failure is recorded and doesn't crash.""" | ||
617 | self.manifest.repoProject.LastFetch = time.time() - ( | ||
618 | sync._ONE_DAY_S + 1 | ||
619 | ) | ||
620 | fetch_error = GitError("Fetch failed") | ||
621 | self.manifest.repoProject.Sync_NetworkHalf.return_value = ( | ||
622 | SyncNetworkHalfResult(False, fetch_error) | ||
623 | ) | ||
624 | |||
625 | with mock.patch.object(sync, "_PostRepoFetch") as mock_post_fetch: | ||
626 | self.cmd._UpdateRepoProject(self.opt, self.manifest, self.errors) | ||
627 | self.manifest.repoProject.Sync_NetworkHalf.assert_called_once() | ||
628 | mock_post_fetch.assert_not_called() | ||
629 | self.assertEqual(self.errors, [fetch_error]) | ||
630 | |||
631 | |||
530 | class InterleavedSyncTest(unittest.TestCase): | 632 | class InterleavedSyncTest(unittest.TestCase): |
531 | """Tests for interleaved sync.""" | 633 | """Tests for interleaved sync.""" |
532 | 634 | ||