trychange_unittest.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 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. """Unit tests for trychange.py."""
  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 testing_support.super_mox import SuperMoxTestBase
  11. import subprocess2
  12. import trychange
  13. class TryChangeTestsBase(SuperMoxTestBase):
  14. """Setups and tear downs the mocks but doesn't test anything as-is."""
  15. def setUp(self):
  16. SuperMoxTestBase.setUp(self)
  17. self.mox.StubOutWithMock(subprocess2, 'communicate')
  18. self.mox.StubOutWithMock(trychange, 'RunGit')
  19. self.mox.StubOutWithMock(trychange.scm.GIT, 'Capture')
  20. self.mox.StubOutWithMock(trychange.scm.GIT, 'GenerateDiff')
  21. self.mox.StubOutWithMock(trychange.scm.GIT, 'GetCheckoutRoot')
  22. self.mox.StubOutWithMock(trychange.scm.GIT, 'GetEmail')
  23. self.mox.StubOutWithMock(trychange.scm.GIT, 'GetPatchName')
  24. self.mox.StubOutWithMock(trychange.scm.GIT, 'GetUpstreamBranch')
  25. self.mox.StubOutWithMock(trychange.scm.SVN, 'GenerateDiff')
  26. self.mox.StubOutWithMock(trychange.scm.SVN, 'GetCheckoutRoot')
  27. self.mox.StubOutWithMock(trychange.scm.SVN, 'GetEmail')
  28. self.fake_root = self.Dir()
  29. self.expected_files = ['foo.txt', 'bar.txt']
  30. self.options = trychange.optparse.Values()
  31. self.options.files = self.expected_files
  32. self.options.diff = None
  33. self.options.name = None
  34. self.options.email = None
  35. self.options.exclude = []
  36. class TryChangeUnittest(TryChangeTestsBase):
  37. """General trychange.py tests."""
  38. def testMembersChanged(self):
  39. members = [
  40. 'DieWithError', 'EPILOG', 'Escape', 'GIT', 'GIT_PATCH_DIR_BASENAME',
  41. 'GetMungedDiff', 'GuessVCS', 'GIT_BRANCH_FILE',
  42. 'HELP_STRING', 'Error', 'InvalidScript', 'NoTryServerAccess',
  43. 'OptionParser', 'PrintSuccess',
  44. 'RunCommand', 'RunGit', 'SCM', 'SVN', 'TryChange', 'USAGE', 'contextlib',
  45. 'breakpad',
  46. 'datetime', 'errno', 'fix_encoding', 'gcl', 'gclient_utils',
  47. 'gerrit_util', 'gen_parser',
  48. 'getpass', 'itertools', 'json', 'logging', 'optparse', 'os', 'posixpath',
  49. 're', 'scm', 'shutil', 'subprocess2', 'sys', 'tempfile', 'urllib',
  50. 'urllib2', 'urlparse']
  51. # If this test fails, you should add the relevant test.
  52. self.compareMembers(trychange, members)
  53. class TryChangeSimpleTest(unittest.TestCase):
  54. # Doesn't require supermox to run.
  55. def test_flags(self):
  56. cmd = [
  57. '--bot', 'bot1,bot2',
  58. '--testfilter', 'test1',
  59. '--testfilter', 'test2',
  60. '--user', 'joe',
  61. '--email', 'joe@example.com',
  62. ]
  63. options, args = trychange.gen_parser(None).parse_args(cmd)
  64. self.assertEquals([], args)
  65. # pylint: disable=W0212
  66. bot_spec = trychange._ParseBotList(options.bot, options.testfilter)
  67. if options.testfilter:
  68. bot_spec = trychange._ApplyTestFilter(options.testfilter, bot_spec)
  69. values = trychange._ParseSendChangeOptions(bot_spec, options)
  70. self.assertEquals(
  71. [
  72. ('user', 'joe'),
  73. ('name', None),
  74. ('email', 'joe@example.com'),
  75. ('bot', 'bot1:test1,test2'),
  76. ('bot', 'bot2:test1,test2'),
  77. ],
  78. values)
  79. def test_flags_bad_combination(self):
  80. cmd = [
  81. '--bot', 'bot1:test1',
  82. '--testfilter', 'test2',
  83. ]
  84. options, args = trychange.gen_parser(None).parse_args(cmd)
  85. self.assertEquals([], args)
  86. try:
  87. # pylint: disable=W0212
  88. trychange._ParseBotList(options.bot, options.testfilter)
  89. self.fail()
  90. except ValueError:
  91. pass
  92. class SVNUnittest(TryChangeTestsBase):
  93. """trychange.SVN tests."""
  94. def testMembersChanged(self):
  95. members = [
  96. 'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting',
  97. 'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples',
  98. ]
  99. # If this test fails, you should add the relevant test.
  100. self.compareMembers(trychange.SVN, members)
  101. def testBasic(self):
  102. # pylint: disable=E1103
  103. trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root)
  104. trychange.scm.SVN.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root)
  105. trychange.scm.SVN.GenerateDiff(['foo.txt', 'bar.txt'],
  106. self.fake_root,
  107. full_move=True,
  108. revision=None).AndReturn('A diff')
  109. trychange.scm.SVN.GetEmail(self.fake_root).AndReturn('georges@example.com')
  110. self.mox.ReplayAll()
  111. svn = trychange.SVN(self.options, self.fake_root, self.options.files)
  112. self.assertEqual(svn.GetFileNames(), self.expected_files)
  113. self.assertEqual(svn.checkout_root, self.fake_root)
  114. self.assertEqual(svn.GenerateDiff(), 'A diff')
  115. class GITUnittest(TryChangeTestsBase):
  116. """trychange.GIT tests."""
  117. def testMembersChanged(self):
  118. members = [
  119. 'AutomagicalSettings', 'CaptureStatus', 'GetCodeReviewSetting',
  120. 'ReadRootFile', 'GenerateDiff', 'GetFileNames', 'files', 'file_tuples',
  121. ]
  122. # If this test fails, you should add the relevant test.
  123. self.compareMembers(trychange.GIT, members)
  124. def testBasic(self):
  125. # pylint: disable=E1103
  126. trychange.os.path.abspath(self.fake_root).AndReturn(self.fake_root)
  127. trychange.scm.GIT.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root)
  128. trychange.scm.GIT.GetUpstreamBranch(self.fake_root).AndReturn('somewhere')
  129. trychange.RunGit(['diff-index', 'HEAD'])
  130. trychange.scm.GIT.GenerateDiff(self.fake_root,
  131. full_move=True,
  132. files=['foo.txt', 'bar.txt'],
  133. branch='somewhere').AndReturn('A diff')
  134. trychange.scm.GIT.GetPatchName(self.fake_root).AndReturn('bleh-1233')
  135. trychange.scm.GIT.GetEmail(self.fake_root).AndReturn('georges@example.com')
  136. self.mox.ReplayAll()
  137. git = trychange.GIT(self.options, self.fake_root, self.options.files)
  138. self.assertEqual(git.GetFileNames(), self.expected_files)
  139. self.assertEqual(git.checkout_root, self.fake_root)
  140. self.assertEqual(git.GenerateDiff(), 'A diff')
  141. if __name__ == '__main__':
  142. unittest.main()