gclient_utils_test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/python
  2. # Copyright (c) 2010 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. # pylint: disable=E1101,W0403
  6. import StringIO
  7. # Fixes include path.
  8. from super_mox import SuperMoxTestBase
  9. import gclient_utils
  10. class GclientUtilBase(SuperMoxTestBase):
  11. def setUp(self):
  12. super(GclientUtilBase, self).setUp()
  13. gclient_utils.sys.stdout.flush = lambda: None
  14. self.mox.StubOutWithMock(gclient_utils, 'Popen')
  15. class GclientUtilsUnittest(GclientUtilBase):
  16. """General gclient_utils.py tests."""
  17. def testMembersChanged(self):
  18. members = [
  19. 'CheckCall', 'CheckCallError', 'CheckCallAndFilter',
  20. 'CheckCallAndFilterAndHeader', 'Error', 'ExecutionQueue', 'FileRead',
  21. 'FileWrite', 'FindFileUpwards', 'FindGclientRoot',
  22. 'GetGClientRootAndEntries', 'GetNamedNodeText', 'MakeFileAutoFlush',
  23. 'GetNodeNamedAttributeText', 'MakeFileAnnotated', 'PathDifference',
  24. 'ParseXML', 'Popen',
  25. 'PrintableObject', 'RemoveDirectory', 'SoftClone', 'SplitUrlRevision',
  26. 'SyntaxErrorToError', 'WorkItem',
  27. 'errno', 'hack_subprocess', 'logging', 'os', 'Queue', 're', 'stat',
  28. 'subprocess', 'sys','threading', 'time', 'xml',
  29. ]
  30. # If this test fails, you should add the relevant test.
  31. self.compareMembers(gclient_utils, members)
  32. class CheckCallTestCase(GclientUtilBase):
  33. def testCheckCallSuccess(self):
  34. args = ['boo', 'foo', 'bar']
  35. process = self.mox.CreateMockAnything()
  36. process.returncode = 0
  37. gclient_utils.Popen(
  38. args, cwd='bar',
  39. stderr=None,
  40. stdout=gclient_utils.subprocess.PIPE).AndReturn(process)
  41. process.communicate().AndReturn(['bleh', 'foo'])
  42. self.mox.ReplayAll()
  43. gclient_utils.CheckCall(args, cwd='bar')
  44. def testCheckCallFailure(self):
  45. args = ['boo', 'foo', 'bar']
  46. process = self.mox.CreateMockAnything()
  47. process.returncode = 42
  48. gclient_utils.Popen(
  49. args,
  50. stderr=None,
  51. stdout=gclient_utils.subprocess.PIPE).AndReturn(process)
  52. process.communicate().AndReturn(['bleh', 'foo'])
  53. self.mox.ReplayAll()
  54. try:
  55. gclient_utils.CheckCall(args)
  56. self.fail()
  57. except gclient_utils.CheckCallError, e:
  58. self.assertEqual(e.command, args)
  59. self.assertEqual(e.cwd, None)
  60. self.assertEqual(e.returncode, 42)
  61. self.assertEqual(e.stdout, 'bleh')
  62. self.assertEqual(e.stderr, 'foo')
  63. class CheckCallAndFilterTestCase(GclientUtilBase):
  64. class ProcessIdMock(object):
  65. def __init__(self, test_string):
  66. self.stdout = StringIO.StringIO(test_string)
  67. def wait(self):
  68. pass
  69. def _inner(self, args, test_string):
  70. cwd = 'bleh'
  71. gclient_utils.sys.stdout.write(
  72. '\n________ running \'boo foo bar\' in \'bleh\'\n')
  73. for i in test_string:
  74. gclient_utils.sys.stdout.write(i)
  75. gclient_utils.Popen(
  76. args,
  77. cwd=cwd,
  78. stdout=gclient_utils.subprocess.PIPE,
  79. stderr=gclient_utils.subprocess.STDOUT,
  80. bufsize=0).AndReturn(self.ProcessIdMock(test_string))
  81. self.mox.ReplayAll()
  82. compiled_pattern = gclient_utils.re.compile(r'a(.*)b')
  83. line_list = []
  84. capture_list = []
  85. def FilterLines(line):
  86. line_list.append(line)
  87. assert isinstance(line, str), type(line)
  88. match = compiled_pattern.search(line)
  89. if match:
  90. capture_list.append(match.group(1))
  91. gclient_utils.CheckCallAndFilterAndHeader(
  92. args, cwd=cwd, always=True, filter_fn=FilterLines)
  93. self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb'])
  94. self.assertEquals(capture_list, ['cc', 'dd'])
  95. def testCheckCallAndFilter(self):
  96. args = ['boo', 'foo', 'bar']
  97. test_string = 'ahah\naccb\nallo\naddb\n'
  98. self._inner(args, test_string)
  99. self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n'
  100. 'ahah\naccb\nallo\naddb\n\n'
  101. '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb'
  102. '\n')
  103. def testNoLF(self):
  104. # Exactly as testCheckCallAndFilterAndHeader without trailing \n
  105. args = ['boo', 'foo', 'bar']
  106. test_string = 'ahah\naccb\nallo\naddb'
  107. self._inner(args, test_string)
  108. self.checkstdout('\n________ running \'boo foo bar\' in \'bleh\'\n'
  109. 'ahah\naccb\nallo\naddb\n'
  110. '________ running \'boo foo bar\' in \'bleh\'\nahah\naccb\nallo\naddb')
  111. class SplitUrlRevisionTestCase(GclientUtilBase):
  112. def testSSHUrl(self):
  113. url = "ssh://test@example.com/test.git"
  114. rev = "ac345e52dc"
  115. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  116. self.assertEquals(out_rev, None)
  117. self.assertEquals(out_url, url)
  118. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  119. self.assertEquals(out_rev, rev)
  120. self.assertEquals(out_url, url)
  121. url = "ssh://example.com/test.git"
  122. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  123. self.assertEquals(out_rev, None)
  124. self.assertEquals(out_url, url)
  125. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  126. self.assertEquals(out_rev, rev)
  127. self.assertEquals(out_url, url)
  128. url = "ssh://example.com/git/test.git"
  129. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  130. self.assertEquals(out_rev, None)
  131. self.assertEquals(out_url, url)
  132. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  133. self.assertEquals(out_rev, rev)
  134. self.assertEquals(out_url, url)
  135. rev = "test-stable"
  136. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  137. self.assertEquals(out_rev, rev)
  138. self.assertEquals(out_url, url)
  139. url = "ssh://user-name@example.com/~/test.git"
  140. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  141. self.assertEquals(out_rev, None)
  142. self.assertEquals(out_url, url)
  143. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  144. self.assertEquals(out_rev, rev)
  145. self.assertEquals(out_url, url)
  146. url = "ssh://user-name@example.com/~username/test.git"
  147. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  148. self.assertEquals(out_rev, None)
  149. self.assertEquals(out_url, url)
  150. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  151. self.assertEquals(out_rev, rev)
  152. self.assertEquals(out_url, url)
  153. def testSVNUrl(self):
  154. url = "svn://example.com/test"
  155. rev = "ac345e52dc"
  156. out_url, out_rev = gclient_utils.SplitUrlRevision(url)
  157. self.assertEquals(out_rev, None)
  158. self.assertEquals(out_url, url)
  159. out_url, out_rev = gclient_utils.SplitUrlRevision("%s@%s" % (url, rev))
  160. self.assertEquals(out_rev, rev)
  161. self.assertEquals(out_url, url)
  162. if __name__ == '__main__':
  163. import unittest
  164. unittest.main()
  165. # vim: ts=2:sw=2:tw=80:et: