A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

149 righe
4.7 KiB

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