gclient_git_mutates_smoketest.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env vpython3
  2. # Copyright (c) 2021 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. """Smoke tests for gclient.py.
  6. Shell out 'gclient' and run git tests.
  7. """
  8. import logging
  9. import os
  10. import sys
  11. import unittest
  12. import gclient_smoketest_base
  13. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  14. sys.path.insert(0, ROOT_DIR)
  15. import subprocess2
  16. from testing_support.fake_repos import join, write
  17. class GClientSmokeGITMutates(gclient_smoketest_base.GClientSmokeBase):
  18. """testRevertAndStatus mutates the git repo so move it to its own suite."""
  19. def setUp(self):
  20. super(GClientSmokeGITMutates, self).setUp()
  21. self.enabled = self.FAKE_REPOS.set_up_git()
  22. if not self.enabled:
  23. self.skipTest('git fake repos not available')
  24. # TODO(crbug.com/1024683): Enable for windows.
  25. @unittest.skipIf(sys.platform == 'win32', 'not yet fixed on win')
  26. def testRevertAndStatus(self):
  27. # Commit new change to repo to make repo_2's hash use a custom_var.
  28. cur_deps = self.FAKE_REPOS.git_hashes['repo_1'][-1][1]['DEPS']
  29. repo_2_hash = self.FAKE_REPOS.git_hashes['repo_2'][1][0][:7]
  30. new_deps = cur_deps.replace('repo_2@%s\'' % repo_2_hash,
  31. 'repo_2@\' + Var(\'r2hash\')')
  32. new_deps = 'vars = {\'r2hash\': \'%s\'}\n%s' % (repo_2_hash, new_deps)
  33. self.FAKE_REPOS._commit_git('repo_1', { # pylint: disable=protected-access
  34. 'DEPS': new_deps,
  35. 'origin': 'git/repo_1@3\n',
  36. })
  37. config_template = ''.join([
  38. 'solutions = [{'
  39. ' "name" : "src",'
  40. ' "url" : %(git_base)r + "repo_1",'
  41. ' "deps_file" : "DEPS",'
  42. ' "managed" : True,'
  43. ' "custom_vars" : %(custom_vars)s,'
  44. '}]'
  45. ])
  46. self.gclient([
  47. 'config', '--spec', config_template % {
  48. 'git_base': self.git_base,
  49. 'custom_vars': {}
  50. }
  51. ])
  52. # Tested in testSync.
  53. self.gclient(['sync', '--deps', 'mac'])
  54. write(join(self.root_dir, 'src', 'repo2', 'hi'), 'Hey!')
  55. out = self.parseGclient(['status', '--deps', 'mac', '--jobs', '1'], [])
  56. # TODO(maruel): http://crosbug.com/3584 It should output the unversioned
  57. # files.
  58. self.assertEqual(0, len(out))
  59. # Revert implies --force implies running hooks without looking at
  60. # pattern matching. For each expected path, 'git reset' and 'git clean'
  61. # are run, so there should be two results for each. The last two results
  62. # should reflect writing git_hooked1 and git_hooked2. There's only one
  63. # result for the third because it is clean and has no output for 'git
  64. # clean'.
  65. out = self.parseGclient(['revert', '--deps', 'mac', '--jobs', '1'],
  66. ['running', 'running'])
  67. self.assertEqual(2, len(out))
  68. tree = self.mangle_git_tree(('repo_1@3', 'src'),
  69. ('repo_2@1', 'src/repo2'),
  70. ('repo_3@2', 'src/repo2/repo_renamed'))
  71. tree['src/git_hooked1'] = 'git_hooked1'
  72. tree['src/git_hooked2'] = 'git_hooked2'
  73. self.assertTree(tree)
  74. # Make a new commit object in the origin repo, to force reset to fetch.
  75. self.FAKE_REPOS._commit_git(
  76. 'repo_2',
  77. { # pylint: disable=protected-access
  78. 'origin': 'git/repo_2@3\n',
  79. })
  80. self.gclient([
  81. 'config', '--spec', config_template % {
  82. 'git_base': self.git_base,
  83. 'custom_vars': {
  84. 'r2hash': self.FAKE_REPOS.git_hashes['repo_2'][-1][0]
  85. }
  86. }
  87. ])
  88. out = self.parseGclient(['revert', '--deps', 'mac', '--jobs', '1'],
  89. ['running', 'running'])
  90. self.assertEqual(2, len(out))
  91. tree = self.mangle_git_tree(('repo_1@3', 'src'),
  92. ('repo_2@3', 'src/repo2'),
  93. ('repo_3@2', 'src/repo2/repo_renamed'))
  94. tree['src/git_hooked1'] = 'git_hooked1'
  95. tree['src/git_hooked2'] = 'git_hooked2'
  96. self.assertTree(tree)
  97. results = self.gclient(['status', '--deps', 'mac', '--jobs', '1'])
  98. out = results[0].splitlines(False)
  99. # TODO(maruel): http://crosbug.com/3584 It should output the unversioned
  100. # files.
  101. self.assertEqual(0, len(out))
  102. def testSyncNoHistory(self):
  103. # Create an extra commit in repo_2 and point DEPS to its hash.
  104. cur_deps = self.FAKE_REPOS.git_hashes['repo_1'][-1][1]['DEPS']
  105. repo_2_hash_old = self.FAKE_REPOS.git_hashes['repo_2'][1][0][:7]
  106. self.FAKE_REPOS._commit_git('repo_2', { # pylint: disable=protected-access
  107. 'last_file': 'file created in last commit',
  108. })
  109. repo_2_hash_new = self.FAKE_REPOS.git_hashes['repo_2'][-1][0]
  110. new_deps = cur_deps.replace(repo_2_hash_old, repo_2_hash_new)
  111. self.assertNotEqual(new_deps, cur_deps)
  112. self.FAKE_REPOS._commit_git('repo_1', { # pylint: disable=protected-access
  113. 'DEPS': new_deps,
  114. 'origin': 'git/repo_1@4\n',
  115. })
  116. config_template = ''.join([
  117. 'solutions = [{'
  118. ' "name" : "src",'
  119. ' "url" : %(git_base)r + "repo_1",'
  120. ' "deps_file" : "DEPS",'
  121. ' "managed" : True,'
  122. '}]'
  123. ])
  124. self.gclient(
  125. ['config', '--spec', config_template % {
  126. 'git_base': self.git_base
  127. }])
  128. self.gclient(['sync', '--no-history', '--deps', 'mac'])
  129. repo2_root = join(self.root_dir, 'src', 'repo2')
  130. # Check that repo_2 is actually shallow and its log has only one entry.
  131. rev_lists = subprocess2.check_output(['git', 'rev-list', 'HEAD'],
  132. cwd=repo2_root).decode('utf-8')
  133. self.assertEqual(repo_2_hash_new, rev_lists.strip('\r\n'))
  134. # Check that we have actually checked out the right commit.
  135. self.assertTrue(os.path.exists(join(repo2_root, 'last_file')))
  136. if __name__ == '__main__':
  137. if '-v' in sys.argv:
  138. logging.basicConfig(level=logging.DEBUG)
  139. unittest.main()