git_map_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright 2020 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_map."""
  7. import io
  8. import os
  9. import re
  10. import sys
  11. import unittest
  12. from unittest import mock
  13. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  14. sys.path.insert(0, DEPOT_TOOLS_ROOT)
  15. from testing_support import git_test_utils
  16. import git_map
  17. import git_common
  18. git_common.TEST_MODE = True
  19. GitRepo = git_test_utils.GitRepo
  20. class GitMapTest(git_test_utils.GitRepoReadOnlyTestBase):
  21. REPO_SCHEMA = """"
  22. A B C D 😋 F G
  23. B H I J K
  24. J L
  25. """
  26. def setUp(self):
  27. # Include branch_K, branch_L to make sure that ABCDEFG all get the
  28. # same commit hashes as self.repo. Otherwise they get committed with the
  29. # wrong timestamps, due to commit ordering.
  30. # TODO(iannucci): Make commit timestamps deterministic in left to right,
  31. # top to bottom order, not in lexi-topographical order.
  32. origin_schema = git_test_utils.GitRepoSchema(
  33. """
  34. A B C D 😋 F G M N O
  35. B H I J K
  36. J L
  37. """, self.getRepoContent)
  38. self.origin = origin_schema.reify()
  39. self.origin.git('checkout', 'main')
  40. self.origin.git('branch', '-d', *['branch_' + l for l in 'KLG'])
  41. self.repo.git('remote', 'add', 'origin', self.origin.repo_path)
  42. self.repo.git('config', '--add', 'remote.origin.fetch',
  43. '+refs/tags/*:refs/tags/*')
  44. self.repo.git('update-ref', 'refs/remotes/origin/main', 'tag_E')
  45. self.repo.git('branch', '--set-upstream-to', 'branch_G', 'branch_K')
  46. self.repo.git('branch', '--set-upstream-to', 'branch_K', 'branch_L')
  47. self.repo.git('fetch', 'origin')
  48. mock.patch('git_map.RESET', '').start()
  49. mock.patch('git_map.BLUE_BACK', '').start()
  50. mock.patch('git_map.BRIGHT_RED', '').start()
  51. mock.patch('git_map.CYAN', '').start()
  52. mock.patch('git_map.GREEN', '').start()
  53. mock.patch('git_map.MAGENTA', '').start()
  54. mock.patch('git_map.RED', '').start()
  55. mock.patch('git_map.WHITE', '').start()
  56. mock.patch('git_map.YELLOW', '').start()
  57. self.addCleanup(mock.patch.stopall)
  58. def testHelp(self):
  59. outbuf = io.BytesIO()
  60. self.repo.run(git_map.main, ['-h'], outbuf)
  61. self.assertIn(b'usage: git map [-h] [--help] [<args>]',
  62. outbuf.getvalue())
  63. def testGitMap(self):
  64. expected = os.linesep.join([
  65. '* 6e85e877ea (tag_O, origin/main, origin/branch_O) 1970-01-30 ~ O',
  66. '* 4705470871 (tag_N) 1970-01-28 ~ N',
  67. '* 8761b1a94f (tag_M) 1970-01-26 ~ M',
  68. '* 5e7ce08691 (tag_G) 1970-01-24 ~ G',
  69. '* 78543ed411 (tag_F) 1970-01-18 ~ F',
  70. '* f5c2b77013 (tag_😋) 1970-01-16 ~ 😋',
  71. '* 5249c43079 (tag_D) 1970-01-10 ~ D',
  72. '* 072ade676a (tag_C) 1970-01-06 ~ C',
  73. '| * e77da937d5 (branch_G) 1970-01-26 ~ G',
  74. '| * acda9677fd 1970-01-20 ~ F',
  75. '| * b4bed3c8e1 1970-01-18 ~ 😋',
  76. '| * 5da071fda9 1970-01-12 ~ D',
  77. '| * 1ef9b2e4ca 1970-01-08 ~ C',
  78. '| | * ddd611f619 (branch_L) 1970-01-24 ~ L',
  79. '| | | * f07cbd8cfc (branch_K) 1970-01-22 ~ K',
  80. '| | |/ ',
  81. '| | * fb7da24708 1970-01-16 ~ J <(branch_L)',
  82. '| | * bb168f6d65 1970-01-14 ~ I',
  83. '| | * ee1032effa 1970-01-10 ~ H',
  84. '| |/ ',
  85. '| * db57edd2c0 1970-01-06 ~ B <(branch_K)',
  86. '| * e4f775f844 (root_A) 1970-01-04 ~ A',
  87. '| * 2824d6d8b6 (tag_L, origin/branch_L) 1970-01-22 ~ L',
  88. '| | * 4e599306f0 (tag_K, origin/branch_K) 1970-01-20 ~ K',
  89. '| |/ ',
  90. '| * 332f1b4499 (tag_J) 1970-01-14 ~ J',
  91. '| * 2fc0bc5ee5 (tag_I) 1970-01-12 ~ I',
  92. '| * 6e0ab26451 (tag_H) 1970-01-08 ~ H',
  93. '|/ ',
  94. '* 315457dbe8 (tag_B) 1970-01-04 ~ B',
  95. '* cd589e62d8 (tag_A, origin/root_A) 1970-01-02 ~ A',
  96. '* 7026d3d68e (tag_", root_", main, branch_") 1970-01-02 ~ "',
  97. ])
  98. outbuf = io.BytesIO()
  99. self.repo.run(git_map.main, [], outbuf)
  100. output = outbuf.getvalue()
  101. output = re.sub(br'.\[\d\dm', b'', output)
  102. output = re.sub(br'.\[m', b'', output)
  103. self.assertEqual(output.splitlines(),
  104. expected.encode('utf-8').splitlines())
  105. if __name__ == '__main__':
  106. unittest.main()