A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.7 KiB

13 年之前
13 年之前
13 年之前
13 年之前
13 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Copyright (C) 2009-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. """
  21. EarwigBot's Unit Tests
  22. This __init__ file provides some support code for unit tests.
  23. Test cases:
  24. -- CommandTestCase provides setUp() for creating a fake connection, plus
  25. some other helpful methods for testing IRC commands.
  26. Fake objects:
  27. -- FakeBot implements Bot, using the Fake* equivalents of all objects
  28. whenever possible.
  29. -- FakeBotConfig implements BotConfig with silent logging.
  30. -- FakeIRCConnection implements IRCConnection, using an internal string
  31. buffer for data instead of sending it over a socket.
  32. """
  33. import logging
  34. import re
  35. from os import path
  36. from threading import Lock
  37. from unittest import TestCase
  38. from earwigbot.bot import Bot
  39. from earwigbot.commands import CommandManager
  40. from earwigbot.config import BotConfig
  41. from earwigbot.irc import Data, IRCConnection
  42. from earwigbot.tasks import TaskManager
  43. from earwigbot.wiki import SitesDB
  44. class CommandTestCase(TestCase):
  45. re_sender = re.compile(":(.*?)!(.*?)@(.*?)\Z")
  46. def setUp(self, command):
  47. self.bot = FakeBot(path.dirname(__file__))
  48. self.command = command(self.bot)
  49. self.command.connection = self.connection = self.bot.frontend
  50. def get_single(self):
  51. data = self.connection._get().split("\n")
  52. line = data.pop(0)
  53. for remaining in data[1:]:
  54. self.connection.send(remaining)
  55. return line
  56. def assertSent(self, msg):
  57. line = self.get_single()
  58. self.assertEqual(line, msg)
  59. def assertSentIn(self, msgs):
  60. line = self.get_single()
  61. self.assertIn(line, msgs)
  62. def assertSaid(self, msg):
  63. self.assertSent(f"PRIVMSG #channel :{msg}")
  64. def assertSaidIn(self, msgs):
  65. msgs = [f"PRIVMSG #channel :{msg}" for msg in msgs]
  66. self.assertSentIn(msgs)
  67. def assertReply(self, msg):
  68. self.assertSaid(f"\x02Foo\x0f: {msg}")
  69. def assertReplyIn(self, msgs):
  70. msgs = [f"\x02Foo\x0f: {msg}" for msg in msgs]
  71. self.assertSaidIn(msgs)
  72. def maker(self, line, chan, msg=None):
  73. data = Data(line)
  74. data.nick, data.ident, data.host = self.re_sender.findall(line[0])[0]
  75. if msg is not None:
  76. data.msg = msg
  77. data.chan = chan
  78. data.parse_args()
  79. return data
  80. def make_msg(self, command, *args):
  81. line = f":Foo!bar@example.com PRIVMSG #channel :!{command}"
  82. line = line.strip().split()
  83. line.extend(args)
  84. return self.maker(line, line[2], " ".join(line[3:])[1:])
  85. def make_join(self):
  86. line = ":Foo!bar@example.com JOIN :#channel".strip().split()
  87. return self.maker(line, line[2][1:])
  88. class FakeBot(Bot):
  89. def __init__(self, root_dir):
  90. self.config = FakeBotConfig(root_dir)
  91. self.logger = logging.getLogger("earwigbot")
  92. self.commands = CommandManager(self)
  93. self.tasks = TaskManager(self)
  94. self.wiki = SitesDB(self)
  95. self.frontend = FakeIRCConnection(self)
  96. self.watcher = FakeIRCConnection(self)
  97. self.component_lock = Lock()
  98. self._keep_looping = True
  99. class FakeBotConfig(BotConfig):
  100. def _setup_logging(self):
  101. logger = logging.getLogger("earwigbot")
  102. logger.addHandler(logging.NullHandler())
  103. class FakeIRCConnection(IRCConnection):
  104. def __init__(self, bot):
  105. self.bot = bot
  106. self._is_running = False
  107. self._connect()
  108. def _connect(self):
  109. self._buffer = ""
  110. def _close(self):
  111. self._buffer = ""
  112. def _get(self, size=4096):
  113. data, self._buffer = self._buffer, ""
  114. return data
  115. def _send(self, msg):
  116. self._buffer += msg + "\n"