git_squash_branch_tree_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright 2024 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Tests for git_squash_branch_tree."""
  7. import os
  8. import sys
  9. import unittest
  10. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  11. sys.path.insert(0, DEPOT_TOOLS_ROOT)
  12. from testing_support import git_test_utils
  13. import git_squash_branch_tree
  14. import git_common
  15. git_common.TEST_MODE = True
  16. class GitSquashBranchTreeTest(git_test_utils.GitRepoReadWriteTestBase):
  17. # Empty repo.
  18. REPO_SCHEMA = """
  19. """
  20. def setUp(self):
  21. super(GitSquashBranchTreeTest, self).setUp()
  22. # Note: Using the REPO_SCHEMA wouldn't simplify this test so it is not
  23. # used.
  24. #
  25. # Create a repo with the follow schema
  26. #
  27. # main <- branchA <- branchB
  28. # ^
  29. # \ branchC
  30. #
  31. # where each branch has 2 commits.
  32. # The repo is empty. Add the first commit or else most commands don't
  33. # work, including `git branch`, which doesn't even show the main branch.
  34. self.repo.git('commit', '-m', 'First commit', '--allow-empty')
  35. # Create the first branch downstream from `main` with 2 commits.
  36. self.repo.git('checkout', '-B', 'branchA', '--track', 'main')
  37. self._createFileAndCommit('fileA1')
  38. self._createFileAndCommit('fileA2')
  39. # Create a branch downstream from `branchA` with 2 commits.
  40. self.repo.git('checkout', '-B', 'branchB', '--track', 'branchA')
  41. self._createFileAndCommit('fileB1')
  42. self._createFileAndCommit('fileB2')
  43. # Create another branch downstream from `branchA` with 2 commits.
  44. self.repo.git('checkout', '-B', 'branchC', '--track', 'branchA')
  45. self._createFileAndCommit('fileC1')
  46. self._createFileAndCommit('fileC2')
  47. def testGitSquashBranchTreeDefaultCurrent(self):
  48. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  49. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 2)
  50. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 2)
  51. # Note: Passing --ignore-no-upstream as this repo has no remote and so
  52. # the `main` branch can't have an upstream.
  53. self.repo.git('checkout', 'branchB')
  54. self.repo.run(git_squash_branch_tree.main, ['--ignore-no-upstream'])
  55. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  56. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 1)
  57. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 2)
  58. def testGitSquashBranchTreeAll(self):
  59. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  60. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 2)
  61. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 2)
  62. self.repo.run(git_squash_branch_tree.main,
  63. ['--branch', 'branchA', '--ignore-no-upstream'])
  64. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 1)
  65. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 1)
  66. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 1)
  67. def testGitSquashBranchTreeSingle(self):
  68. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  69. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 2)
  70. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 2)
  71. self.repo.run(git_squash_branch_tree.main,
  72. ['--branch', 'branchB', '--ignore-no-upstream'])
  73. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  74. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 1)
  75. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 2)
  76. self.repo.run(git_squash_branch_tree.main,
  77. ['--branch', 'branchC', '--ignore-no-upstream'])
  78. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 2)
  79. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 1)
  80. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 1)
  81. self.repo.run(git_squash_branch_tree.main,
  82. ['--branch', 'branchA', '--ignore-no-upstream'])
  83. self.assertEqual(self._getCountAheadOfUpstream('branchA'), 1)
  84. self.assertEqual(self._getCountAheadOfUpstream('branchB'), 1)
  85. self.assertEqual(self._getCountAheadOfUpstream('branchC'), 1)
  86. # Creates a file with arbitrary contents and commit it to the current
  87. # branch.
  88. def _createFileAndCommit(self, filename):
  89. with self.repo.open(filename, 'w') as f:
  90. f.write('content')
  91. self.repo.git('add', filename)
  92. self.repo.git_commit('Added file ' + filename)
  93. # Returns the count of how many commits `branch` is ahead of its upstream.
  94. def _getCountAheadOfUpstream(self, branch):
  95. upstream = branch + '@{u}'
  96. output = self.repo.git('rev-list', '--count',
  97. upstream + '..' + branch).stdout
  98. return int(output)
  99. if __name__ == '__main__':
  100. unittest.main()