git_migrate_default_branch_test.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env vpython3
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for git_migrate_default_branch.py."""
  6. import collections
  7. import os
  8. import sys
  9. import unittest
  10. from unittest import mock
  11. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import git_migrate_default_branch
  13. class CMDFormatTestCase(unittest.TestCase):
  14. def setUp(self):
  15. self.addCleanup(mock.patch.stopall)
  16. def test_no_remote(self):
  17. def RunMock(*args):
  18. if args[0] == 'remote':
  19. return ''
  20. self.fail('did not expect such run git command: %s' % args[0])
  21. mock.patch('git_migrate_default_branch.git_common.run', RunMock).start()
  22. with self.assertRaisesRegexp(RuntimeError, 'Could not find any remote'):
  23. git_migrate_default_branch.main()
  24. def test_migration_not_ready(self):
  25. def RunMock(*args):
  26. if args[0] == 'remote':
  27. return 'origin\ngerrit'
  28. raise Exception('Did not expect such run git command: %s' % args[0])
  29. mock.patch('git_migrate_default_branch.git_common.run', RunMock).start()
  30. mock.patch('git_migrate_default_branch.git_common.repo_root',
  31. return_value='.').start()
  32. mock.patch('git_migrate_default_branch.scm.GIT.GetConfig',
  33. return_value='https://chromium.googlesource.com').start()
  34. mock.patch('git_migrate_default_branch.gerrit_util.GetProjectHead',
  35. return_value=None).start()
  36. with self.assertRaisesRegexp(RuntimeError, 'not migrated yet'):
  37. git_migrate_default_branch.main()
  38. def test_migration_no_master(self):
  39. def RunMock(*args):
  40. if args[0] == 'remote':
  41. return 'origin\ngerrit'
  42. if args[0] == 'fetch':
  43. return
  44. if args[0] == 'branch':
  45. return
  46. if args[0] == 'config':
  47. return
  48. raise Exception('Did not expect such run git command: %s' % args[0])
  49. mock_runs = mock.patch('git_migrate_default_branch.git_common.run',
  50. side_effect=RunMock).start()
  51. mock.patch('git_migrate_default_branch.git_common.repo_root',
  52. return_value='.').start()
  53. mock.patch('git_migrate_default_branch.scm.GIT.GetConfig',
  54. return_value='https://chromium.googlesource.com').start()
  55. mock.patch('git_migrate_default_branch.gerrit_util.GetProjectHead',
  56. return_value='refs/heads/main').start()
  57. BranchesInfo = collections.namedtuple('BranchesInfo',
  58. 'hash upstream commits behind')
  59. branches = {
  60. '': None, # always returned
  61. 'master': BranchesInfo('0000', 'origin/master', '0', '0'),
  62. 'feature': BranchesInfo('0000', 'master', '0', '0'),
  63. 'another_feature': BranchesInfo('0000', 'feature', '0', '0'),
  64. 'remote_feature': BranchesInfo('0000', 'origin/master', '0', '0'),
  65. }
  66. mock.patch('git_migrate_default_branch.git_common.get_branches_info',
  67. return_value=branches).start()
  68. mock_merge_base = mock.patch(
  69. 'git_migrate_default_branch.git_common.remove_merge_base',
  70. return_value=branches).start()
  71. git_migrate_default_branch.main()
  72. mock_merge_base.assert_any_call('feature')
  73. mock_merge_base.assert_any_call('remote_feature')
  74. mock_runs.assert_any_call('branch', '-m', 'master', 'main')
  75. mock_runs.assert_any_call('branch', '--set-upstream-to', 'main', 'feature')
  76. mock_runs.assert_any_call('branch', '--set-upstream-to', 'origin/main',
  77. 'remote_feature')
  78. if __name__ == '__main__':
  79. unittest.main()