gclient_paths_test.py 8.9 KB

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