gclient_paths_test.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright (c) 2012 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. import os
  7. import sys
  8. import unittest
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from io import StringIO
  11. from unittest import mock
  12. import gclient_paths
  13. import gclient_utils
  14. import subprocess2
  15. EXCEPTION = subprocess2.CalledProcessError(128, ['cmd'], 'cwd', 'stdout',
  16. 'stderr')
  17. class TestBase(unittest.TestCase):
  18. def setUp(self):
  19. super(TestBase, self).setUp()
  20. self.file_tree = {}
  21. self.root = 'C:\\' if sys.platform == 'win32' else '/'
  22. # Use unique roots for each test to avoid cache hits from @lru_cache.
  23. self.root += self._testMethodName
  24. self.cwd = self.root
  25. mock.patch('gclient_utils.FileRead', self.read).start()
  26. mock.patch('os.environ', {}).start()
  27. mock.patch('os.getcwd', self.getcwd).start()
  28. mock.patch('os.path.exists', self.exists).start()
  29. mock.patch('os.path.realpath', side_effect=lambda path: path).start()
  30. mock.patch('subprocess2.check_output').start()
  31. mock.patch('sys.platform', '').start()
  32. mock.patch('sys.stderr', StringIO()).start()
  33. self.addCleanup(mock.patch.stopall)
  34. def getcwd(self):
  35. return self.cwd
  36. def exists(self, path):
  37. return path in self.file_tree
  38. def read(self, path):
  39. return self.file_tree[path]
  40. def make_file_tree(self, file_tree):
  41. self.file_tree = {
  42. os.path.join(self.root, path): content
  43. for path, content in file_tree.items()
  44. }
  45. class FindGclientRootTest(TestBase):
  46. def testFindGclientRoot(self):
  47. self.make_file_tree({'.gclient': ''})
  48. self.assertEqual(self.root, gclient_paths.FindGclientRoot(self.root))
  49. def testGclientRootInParentDir(self):
  50. self.make_file_tree({
  51. '.gclient': '',
  52. '.gclient_entries': 'entries = {"foo": "..."}',
  53. })
  54. self.assertEqual(
  55. self.root,
  56. gclient_paths.FindGclientRoot(os.path.join(self.root, 'foo',
  57. 'bar')))
  58. def testGclientRootInParentDir_NotInGclientEntries(self):
  59. self.make_file_tree({
  60. '.gclient': '',
  61. '.gclient_entries': 'entries = {"foo": "..."}',
  62. })
  63. self.assertIsNone(
  64. gclient_paths.FindGclientRoot(os.path.join(self.root, 'bar',
  65. 'baz')))
  66. def testGclientRootInParentDir_NoGclientEntriesFile(self):
  67. self.make_file_tree({'.gclient': ''})
  68. self.assertEqual(
  69. self.root,
  70. gclient_paths.FindGclientRoot(os.path.join(self.root, 'x', 'y',
  71. 'z')))
  72. self.assertEqual(
  73. '%s missing, .gclient file in parent directory %s might not be the '
  74. 'file you want to use.\n' %
  75. (os.path.join(self.root, '.gclient_entries'), self.root),
  76. sys.stderr.getvalue())
  77. def testGclientRootInParentDir_ErrorWhenParsingEntries(self):
  78. self.make_file_tree({'.gclient': '', '.gclient_entries': ':P'})
  79. with self.assertRaises(Exception):
  80. gclient_paths.FindGclientRoot(os.path.join(self.root, 'foo', 'bar'))
  81. def testRootNotFound(self):
  82. self.assertIsNone(
  83. gclient_paths.FindGclientRoot(os.path.join(self.root, 'x', 'y',
  84. 'z')))
  85. class GetGClientPrimarySolutionNameTest(TestBase):
  86. def testGetGClientPrimarySolutionName(self):
  87. self.make_file_tree({'.gclient': 'solutions = [{"name": "foo"}]'})
  88. self.assertEqual('foo',
  89. gclient_paths.GetGClientPrimarySolutionName(self.root))
  90. def testNoSolutionsInGclientFile(self):
  91. self.make_file_tree({'.gclient': ''})
  92. self.assertIsNone(gclient_paths.GetGClientPrimarySolutionName(
  93. self.root))
  94. class GetPrimarySolutionPathTest(TestBase):
  95. def testGetPrimarySolutionPath(self):
  96. self.make_file_tree({'.gclient': 'solutions = [{"name": "foo"}]'})
  97. self.assertEqual(os.path.join(self.root, 'foo'),
  98. gclient_paths.GetPrimarySolutionPath())
  99. def testSolutionNameDefaultsToSrc(self):
  100. self.make_file_tree({'.gclient': ''})
  101. self.assertEqual(os.path.join(self.root, 'src'),
  102. gclient_paths.GetPrimarySolutionPath())
  103. def testGclientRootNotFound_GitRootHasBuildtools(self):
  104. self.make_file_tree({os.path.join('foo', 'buildtools'): ''})
  105. self.cwd = os.path.join(self.root, 'foo', 'bar')
  106. subprocess2.check_output.return_value = (os.path.join(
  107. self.root, 'foo').replace(os.sep, '/').encode('utf-8') + b'\n')
  108. self.assertEqual(os.path.join(self.root, 'foo'),
  109. gclient_paths.GetPrimarySolutionPath())
  110. def testGclientRootNotFound_NoBuildtools(self):
  111. self.cwd = os.path.join(self.root, 'foo', 'bar')
  112. subprocess2.check_output.return_value = b'/foo\n'
  113. self.assertIsNone(gclient_paths.GetPrimarySolutionPath())
  114. def testGclientRootNotFound_NotInAGitRepo_CurrentDirHasBuildtools(self):
  115. self.make_file_tree({os.path.join('foo', 'bar', 'buildtools'): ''})
  116. self.cwd = os.path.join(self.root, 'foo', 'bar')
  117. subprocess2.check_output.side_effect = EXCEPTION
  118. self.assertEqual(self.cwd, gclient_paths.GetPrimarySolutionPath())
  119. def testGclientRootNotFound_NotInAGitRepo_NoBuildtools(self):
  120. self.cwd = os.path.join(self.root, 'foo')
  121. subprocess2.check_output.side_effect = EXCEPTION
  122. self.assertIsNone(gclient_paths.GetPrimarySolutionPath())
  123. class GetBuildtoolsPathTest(TestBase):
  124. def testEnvVarOverride(self):
  125. os.environ = {'CHROMIUM_BUILDTOOLS_PATH': 'foo'}
  126. self.assertEqual('foo', gclient_paths.GetBuildtoolsPath())
  127. def testNoSolutionsFound(self):
  128. self.cwd = os.path.join(self.root, 'foo', 'bar')
  129. subprocess2.check_output.side_effect = EXCEPTION
  130. self.assertIsNone(gclient_paths.GetBuildtoolsPath())
  131. def testBuildtoolsInSolution(self):
  132. self.make_file_tree({
  133. '.gclient': '',
  134. os.path.join('src', 'buildtools'): '',
  135. })
  136. self.cwd = os.path.join(self.root, 'src', 'foo')
  137. self.assertEqual(os.path.join(self.root, 'src', 'buildtools'),
  138. gclient_paths.GetBuildtoolsPath())
  139. def testBuildtoolsInGclientRoot(self):
  140. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  141. self.cwd = os.path.join(self.root, 'src', 'foo')
  142. self.assertEqual(os.path.join(self.root, 'buildtools'),
  143. gclient_paths.GetBuildtoolsPath())
  144. def testNoBuildtools(self):
  145. self.make_file_tree({'.gclient': ''})
  146. self.cwd = os.path.join(self.root, 'foo', 'bar')
  147. self.assertIsNone(gclient_paths.GetBuildtoolsPath())
  148. class GetBuildtoolsPlatformBinaryPath(TestBase):
  149. def testNoBuildtoolsPath(self):
  150. self.make_file_tree({'.gclient': ''})
  151. self.cwd = os.path.join(self.root, 'foo', 'bar')
  152. self.assertIsNone(gclient_paths.GetBuildtoolsPlatformBinaryPath())
  153. def testWin(self):
  154. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  155. sys.platform = 'win'
  156. self.assertEqual(os.path.join(self.root, 'buildtools', 'win'),
  157. gclient_paths.GetBuildtoolsPlatformBinaryPath())
  158. def testCygwin(self):
  159. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  160. sys.platform = 'cygwin'
  161. self.assertEqual(os.path.join(self.root, 'buildtools', 'win'),
  162. gclient_paths.GetBuildtoolsPlatformBinaryPath())
  163. def testMac(self):
  164. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  165. sys.platform = 'darwin'
  166. self.assertEqual(os.path.join(self.root, 'buildtools', 'mac'),
  167. gclient_paths.GetBuildtoolsPlatformBinaryPath())
  168. def testLinux(self):
  169. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  170. sys.platform = 'linux'
  171. self.assertEqual(os.path.join(self.root, 'buildtools', 'linux64'),
  172. gclient_paths.GetBuildtoolsPlatformBinaryPath())
  173. def testError(self):
  174. self.make_file_tree({'.gclient': '', 'buildtools': ''})
  175. sys.platform = 'foo'
  176. with self.assertRaises(gclient_utils.Error,
  177. msg='Unknown platform: foo'):
  178. gclient_paths.GetBuildtoolsPlatformBinaryPath()
  179. class GetExeSuffixTest(TestBase):
  180. def testGetExeSuffix(self):
  181. sys.platform = 'win'
  182. self.assertEqual('.exe', gclient_paths.GetExeSuffix())
  183. sys.platform = 'cygwin'
  184. self.assertEqual('.exe', gclient_paths.GetExeSuffix())
  185. sys.platform = 'foo'
  186. self.assertEqual('', gclient_paths.GetExeSuffix())
  187. if __name__ == '__main__':
  188. unittest.main()