Add test covering sending data
authorNeil <neil@dip.sun.ac.za>
Sun, 18 Nov 2012 13:59:27 +0000 (15:59 +0200)
committerNeil <neil@dip.sun.ac.za>
Sun, 18 Nov 2012 13:59:27 +0000 (15:59 +0200)
tests/testirker.py

index 059f6bdfc4282aa03326a15aa969e44969de6998..5352434f5ea40ebe40d808848fe72295a3c5d04f 100644 (file)
@@ -3,10 +3,10 @@
 # Copyright 2012 Neil Muller
 # GPL 2+ - see COPYING for details
 from unittest import TestLoader
+from SocketServer import TCPServer, StreamRequestHandler
+import threading
 
-from bzrlib import (
-    config,
-    )
+from bzrlib import config
 from bzrlib.bzrdir import BzrDir
 from bzrlib.tests import TestCaseInTempDir
 from bzrlib.plugins.bzrirker.irkerhook import IrkerSender
@@ -17,6 +17,8 @@ def test_suite():
 
 sample_config = ("[DEFAULT]\n"
         "irker_project=bzrirker\n"
+        "irker_server=localhost\n"
+        "irker_port=6677\n"
         "irker_channels=irc://chat.example.test/commits\n")
 
 no_project_config = ("[DEFAULT]\n"
@@ -28,6 +30,13 @@ colour_config = ("[DEFAULT]\n"
         "irker_channels=irc://chat.example.test/commits\n")
 
 
+class MyHandler(StreamRequestHandler):
+
+    def handle(self):
+        data = self.rfile.read()
+        self.server.data = data
+
+
 class TestConfigured(TestCaseInTempDir):
 
     def test_no_colours(self):
@@ -64,6 +73,18 @@ class TestConfigured(TestCaseInTempDir):
                 '\x02bzrirker:\x0f \x0303Sample <john@example.test>\x0f'
                 ' work * \x021\x0f /  \x02\x0f: foo bar ', irker._format())
 
+    def test_send(self):
+        irker = self.get_irker()
+        server = self.get_server()
+        irker.send()
+        while server.data is None:
+            pass
+        server.shutdown()
+        self.assertEqual(server.data,
+                '{"to": ["irc://chat.example.test/commits"]'
+                ', "privmsg": "bzrirker: Sample <john@example.test>'
+                ' work * 1 /  : foo bar "}\n')
+
     def get_irker(self, text=sample_config):
         my_config = config.MemoryStack(text)
         self.branch = BzrDir.create_branch_convenience('.')
@@ -78,3 +99,10 @@ class TestConfigured(TestCaseInTempDir):
         # We're not calling send, so do the setup stuff here
         irker._setup_revision_and_revno()
         return irker
+
+    def get_server(self):
+        server = TCPServer(('localhost', 6677), MyHandler)
+        server.data = None
+        thread = threading.Thread(target=server.serve_forever)
+        thread.start()
+        return server