irc_command_unittest.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (c) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import os
  29. import unittest2 as unittest
  30. from webkitpy.common.system.outputcapture import OutputCapture
  31. from webkitpy.tool.bot.irc_command import *
  32. from webkitpy.tool.mocktool import MockTool
  33. from webkitpy.common.net.web_mock import MockWeb
  34. from webkitpy.common.system.executive_mock import MockExecutive
  35. from webkitpy.common.system.filesystem_mock import MockFileSystem
  36. class IRCCommandTest(unittest.TestCase):
  37. def test_whois(self):
  38. whois = Whois()
  39. self.assertEqual("tom: Usage: whois SEARCH_STRING",
  40. whois.execute("tom", [], None, None))
  41. self.assertEqual('tom: Adam Barth is "Adam Barth" <abarth@webkit.org> (:abarth) (r). Why do you ask?',
  42. whois.execute("tom", ["Adam", "Barth"], None, None))
  43. self.assertEqual("tom: Sorry, I don't know any contributors matching 'unknown@example.com'.",
  44. whois.execute("tom", ["unknown@example.com"], None, None))
  45. self.assertEqual('tom: tonyg@chromium.org is "Tony Gentilcore" <tonyg@chromium.org> (:tonyg-cr) (r). Why do you ask?',
  46. whois.execute("tom", ["tonyg@chromium.org"], None, None))
  47. self.assertEqual('tom: TonyG@Chromium.org is "Tony Gentilcore" <tonyg@chromium.org> (:tonyg-cr) (r). Why do you ask?',
  48. whois.execute("tom", ["TonyG@Chromium.org"], None, None))
  49. self.assertEqual('tom: rniwa is "Ryosuke Niwa" <rniwa@webkit.org> (:rniwa) (r). Why do you ask?',
  50. whois.execute("tom", ["rniwa"], None, None))
  51. self.assertEqual('tom: lopez is "Xan Lopez" <xan.lopez@gmail.com> (:xan) (r). Why do you ask?',
  52. whois.execute("tom", ["lopez"], None, None))
  53. self.assertEqual(u'tom: Osztrogon\u00e1c is "Csaba Osztrogon\u00e1c" <ossy@webkit.org> (:ossy) (r). Why do you ask?',
  54. whois.execute("tom", [u'Osztrogon\u00e1c'], None, None))
  55. self.assertEqual('tom: "Vicki Murley" <vicki@apple.com> hasn\'t told me their nick. Boo hoo :-(',
  56. whois.execute("tom", ["vicki@apple.com"], None, None))
  57. self.assertEqual('tom: I\'m not sure who you mean? "Gavin Peters" <gavinp@chromium.org> (:gavinp) (c) or "Gavin Barraclough" <barraclough@apple.com> (:gbarra) (r) could be \'Gavin\'.',
  58. whois.execute("tom", ["Gavin"], None, None))
  59. self.assertEqual('tom: More than 5 contributors match \'david\', could you be more specific?',
  60. whois.execute("tom", ["david"], None, None))
  61. def test_create_bug(self):
  62. create_bug = CreateBug()
  63. self.assertEqual("tom: Usage: create-bug BUG_TITLE",
  64. create_bug.execute("tom", [], None, None))
  65. example_args = ["sherrif-bot", "should", "have", "a", "create-bug", "command"]
  66. tool = MockTool()
  67. # MockBugzilla has a create_bug, but it logs to stderr, this avoids any logging.
  68. tool.bugs.create_bug = lambda a, b, cc=None, assignee=None: 50004
  69. self.assertEqual("tom: Created bug: http://example.com/50004",
  70. create_bug.execute("tom", example_args, tool, None))
  71. def mock_create_bug(title, description, cc=None, assignee=None):
  72. raise Exception("Exception from bugzilla!")
  73. tool.bugs.create_bug = mock_create_bug
  74. self.assertEqual("tom: Failed to create bug:\nException from bugzilla!",
  75. create_bug.execute("tom", example_args, tool, None))
  76. def test_rollout_updates_working_copy(self):
  77. rollout = Rollout()
  78. tool = MockTool()
  79. tool.executive = MockExecutive(should_log=True)
  80. expected_logs = "MOCK run_and_throw_if_fail: ['mock-update-webkit'], cwd=/mock-checkout\n"
  81. OutputCapture().assert_outputs(self, rollout._update_working_copy, [tool], expected_logs=expected_logs)
  82. def test_rollout(self):
  83. rollout = Rollout()
  84. self.assertEqual(([1234], "testing foo"),
  85. rollout._parse_args(["1234", "testing", "foo"]))
  86. self.assertEqual(([554], "testing foo"),
  87. rollout._parse_args(["r554", "testing", "foo"]))
  88. self.assertEqual(([556, 792], "testing foo"),
  89. rollout._parse_args(["r556", "792", "testing", "foo"]))
  90. self.assertEqual(([128, 256], "testing foo"),
  91. rollout._parse_args(["r128,r256", "testing", "foo"]))
  92. self.assertEqual(([512, 1024, 2048], "testing foo"),
  93. rollout._parse_args(["512,", "1024,2048", "testing", "foo"]))
  94. # Test invalid argument parsing:
  95. self.assertEqual((None, None), rollout._parse_args([]))
  96. self.assertEqual((None, None), rollout._parse_args(["--bar", "1234"]))
  97. # Invalid arguments result in the USAGE message.
  98. self.assertEqual("tom: Usage: rollout SVN_REVISION [SVN_REVISIONS] REASON",
  99. rollout.execute("tom", [], None, None))
  100. tool = MockTool()
  101. tool.filesystem.files["/mock-checkout/test/file/one"] = ""
  102. tool.filesystem.files["/mock-checkout/test/file/two"] = ""
  103. self.assertEqual("Failed to apply reverse diff for file(s): test/file/one, test/file/two",
  104. rollout._check_diff_failure("""
  105. Preparing rollout for bug 123456.
  106. Updating working directory
  107. Failed to apply reverse diff for revision 123456 because of the following conflicts:
  108. test/file/one
  109. test/file/two
  110. Failed to apply reverse diff for revision 123456 because of the following conflicts:
  111. test/file/one
  112. test/file/two
  113. Updating OpenSource
  114. Current branch master is up to date.
  115. """, tool))
  116. self.assertEqual(None, rollout._check_diff_failure("""
  117. Preparing rollout for bug 123456.
  118. Updating working directory
  119. Some other error report involving file paths:
  120. test/file/one
  121. test/file/two
  122. Updating OpenSource
  123. Current branch master is up to date.
  124. """, tool))
  125. # FIXME: We need a better way to test IRCCommands which call tool.irc().post()