diff options
author | kongr45gpen <electrovesta@gmail.com> | 2014-04-28 00:16:14 +0300 |
---|---|---|
committer | kongr45gpen <electrovesta@gmail.com> | 2014-04-28 00:16:14 +0300 |
commit | b4c587246e389b72019f76515dc04e0e13b92eb6 (patch) | |
tree | 62d777f222b7925b66ec10635385b92560973f5e | |
parent | 8afc8e3105c89115c4327f83041bb96796c894ef (diff) | |
download | supybot_github-b4c587246e389b72019f76515dc04e0e13b92eb6.tar.gz supybot_github-b4c587246e389b72019f76515dc04e0e13b92eb6.tar.bz2 supybot_github-b4c587246e389b72019f76515dc04e0e13b92eb6.zip |
Add tests
-rw-r--r-- | local/handler/GithubHandler.py | 46 | ||||
-rw-r--r-- | local/handler/PushHandler.py | 1 | ||||
-rw-r--r-- | local/utility.py | 2 | ||||
-rw-r--r-- | plugin.py | 8 | ||||
-rw-r--r-- | samples/push-merge.json | 172 | ||||
-rw-r--r-- | test.py | 60 |
6 files changed, 262 insertions, 27 deletions
diff --git a/local/handler/GithubHandler.py b/local/handler/GithubHandler.py index ed27a98..ea9f7d4 100644 --- a/local/handler/GithubHandler.py +++ b/local/handler/GithubHandler.py @@ -31,7 +31,6 @@ import IssueCommentHandler class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_POST(s): """Respond to a POST request.""" - length = int(s.headers['Content-Length']) post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8')) data = json.loads(post_data['payload'][0]) @@ -84,30 +83,33 @@ class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): s.wfile.write("The password is wrong") return - for irc in world.ircs: - # Handle different event types - - msgs = [] - - if 'pages' in data: - msgs = WikiHandler.handle(irc, data) - elif 'state' in data: - msgs = StatusHandler.handle(irc, data) - elif 'commits' in data: - msgs = PushHandler.handle(irc, data) - elif 'issue' in data: - if 'comment' in data: - msgs = IssueCommentHandler.handle(irc, data) - else: - msgs = IssueHandler.handle(irc, data) + irc = {} + # Handle different event types + + msgs = [] + + if 'pages' in data: + msgs = WikiHandler.handle(irc, data) + elif 'state' in data: + msgs = StatusHandler.handle(irc, data) + elif 'commits' in data: + msgs = PushHandler.handle(irc, data) + elif 'issue' in data: + if 'comment' in data: + msgs = IssueCommentHandler.handle(irc, data) else: - msgs.append( ircmsgs.privmsg(channel, "Something happened")) + msgs = IssueHandler.handle(irc, data) + else: + msgs.append( ircmsgs.privmsg(channel, "Something happened")) - #msgs.append( ircmsgs.privmsg("#main", "%s" % ()) ) - for msg in msgs: - irc.queueMsg(ircmsgs.privmsg(channel, msg)) - saveMessages(msgs) + saveMessages(msgs) + + #msgs.append( ircmsgs.privmsg("#main", "%s" % ()) ) + if not world.testing: + for msg in msgs: + for irc in world.ircs: + irc.queueMsg(ircmsgs.privmsg(channel, msg)) def log_message(self, format, *args): log.info(format % args) diff --git a/local/handler/PushHandler.py b/local/handler/PushHandler.py index 3ce24f9..4ec927b 100644 --- a/local/handler/PushHandler.py +++ b/local/handler/PushHandler.py @@ -66,7 +66,6 @@ def handle(irc, data): regularCommitCount = len(data['commits']) - mergedCommitCount isMerge = True - if configValue("hidePush",None) == False and not branched: msgs.append( "%s @ %s: %s pushed %s %s (%s)%s" % ( ircutils.bold(ircutils.mircColor(branch, "blue")), diff --git a/local/utility.py b/local/utility.py index abb0915..2640e41 100644 --- a/local/utility.py +++ b/local/utility.py @@ -67,7 +67,7 @@ def getShortURL(longurl): def saveMessages(msgs): """ Saves the last messages so that the plugin can be easily tested """ - if world.testing: + if not world.testing: return globals.messageList = msgs @@ -52,6 +52,8 @@ import local.globals as globals import local.handler.GithubHandler as RequestHandler import local.utility as Utility +globals.init() + class Github(callbacks.Plugin): """Add the help for \"@plugin help Github\" here This should describe how to use this plugin.""" @@ -109,8 +111,10 @@ class Github(callbacks.Plugin): if order == 'last' or type == 'last': requestedNum = len(globals.messageList) - requestedNum + 1 - print globals.messageList - irc.reply(globals.messageList[requestedNum-1]) + try: + irc.reply(globals.messageList[requestedNum-1]) + except IndexError: + irc.error('No such message') # Debug function get = wrap(get, ['lowered', optional('lowered'), optional('text')]) if world.testing else False diff --git a/samples/push-merge.json b/samples/push-merge.json new file mode 100644 index 0000000..778f7c6 --- /dev/null +++ b/samples/push-merge.json @@ -0,0 +1,172 @@ +{ + "after": "f386345fa3c539f338051ae0d76ae4f4b4546bd4", + "base_ref": "refs/heads/newTestBranch", + "before": "cb12c8e51c8ef25e72331d5166c5e14e2c3a5909", + "commits": [ + { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": true, + "id": "5b1c7d19925f11b2a1854469d4099bb5720d386f", + "message": "This is just a regular commit", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:49:53 03:00", + "url": "https://github.com/username/test/commit/5b7c7d19925f11b2a1854469d4099bb5720d386f" + }, + { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": false, + "id": "5b7c7d19925f11b2a1854469d4099bb5720d386f", + "message": "MC1", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:49:53 03:00", + "url": "https://github.com/username/test/commit/5b7c7d19925f11b2a1854469d4099bb5720d386f" + }, + { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": false, + "id": "0153038fb82fae559485ded861e270180dd867bd", + "message": "MC2", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:49:58 03:00", + "url": "https://github.com/username/test/commit/0153038fb82fae559485ded861e270180dd867bd" + }, + { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": false, + "id": "f8a7b326b143ebcd3172abf5a2d2d84317b6f9e7", + "message": "MC3", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:50:04 03:00", + "url": "https://github.com/username/test/commit/f8a7b326b143ebcd3172abf5a2d2d84317b6f9e7" + }, + { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": false, + "id": "f386345fa3c539f338051ae0d76ae4f4b4546bd4", + "message": "MC4", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:50:15 03:00", + "url": "https://github.com/username/test/commit/f386345fa3c539f338051ae0d76ae4f4b4546bd4" + } + ], + "compare": "https://github.com/username/test/compare/cb12c8e51c8e...f386345fa3c5", + "created": false, + "deleted": false, + "forced": false, + "head_commit": { + "added": [], + "author": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "committer": { + "email": "electrovesta@gmail.com", + "name": "username", + "username": "username" + }, + "distinct": false, + "id": "f386345fa3c539f338051ae0d76ae4f4b4546bd4", + "message": "MC4", + "modified": [ + "ntest1.md" + ], + "removed": [], + "timestamp": "2014-04-26T14:50:15 03:00", + "url": "https://github.com/username/test/commit/f386345fa3c539f338051ae0d76ae4f4b4546bd4" + }, + "pusher": { + "email": "electrovesta@gmail.com", + "name": "username" + }, + "ref": "refs/heads/master", + "repository": { + "created_at": 1357130000, + "description": "You can safely ignore this repo :)", + "fork": false, + "forks": 0, + "has_downloads": true, + "has_issues": true, + "has_wiki": true, + "homepage": "", + "id": 7406434, + "master_branch": "master", + "name": "test", + "open_issues": 2, + "owner": { + "email": "electrovesta@gmail.com", + "name": "username" + }, + "private": false, + "pushed_at": 1398513389, + "size": 549, + "stargazers": 0, + "url": "https://github.com/username/test", + "watchers": 0 + } +}
\ No newline at end of file @@ -1,9 +1,67 @@ ### +from supybot.log import info from supybot.test import * -class GithubTestCase(PluginTestCase): +import re +import urllib + +class GithubTestCase(ChannelPluginTestCase): plugins = ('Github',) + port = 27230 + files = {} + config = { 'plugins.github.shortUrl': False, + 'plugins.github.hidePush': False + } + + def assertContains(self, query, expectedNeedle, flags=re.I, **kwargs): + regex = re.compile("((\x02)|(\x03))(?:\d{1,2}(?:,\d{1,2})?)?", re.UNICODE) + + m = self._feedMsg(query, **kwargs) + messageString = regex.sub('', m.args[1]) + if m is None: + raise TimeoutError, query + self.failUnless(re.search(expectedNeedle, messageString, flags), + '%r does not match %r' % (messageString, expectedNeedle)) + return m + + def setUp(self): + # Set a different port from the default one to make testing while a + # supybot-github instance is running possible - we don't use the config + # variable because supybot's test framework sets the value after the + # plugin has been loaded + conf.supybot.plugins.get("Github").get('port').setValue(self.port) + ChannelPluginTestCase.setUp(self) + + def sendRequest(self, file): + if file in self.files: + content = self.files[file] + else: + with open('samples/' + file + '.json', 'r') as content_file: + content = content_file.read() + self.files[file] = content + + res = urllib.urlopen('http://localhost:' + str(self.port), 'payload=' + content) + + + + def testMerge(self): + self.sendRequest('push-merge') + + # By default, merged commits should not be shown + self.assertError('get 5th message') + + self.assertContains('get first message', 'merged 4 commits') + self.assertContains('get first message', 'from newTestBranch') + self.assertContains('get first message', 'pushed 1 commit') + self.assertContains('get first message', 'to master') + + self.assertContains('get first message', 'https://github.com/username/test/compare/cb12c8e51c8e...f386345fa3c5') + + def testHeaders(self): + # self.assertError('headers ftp://ftp.cdrom.com/pub/linux') + # self.assertNotError('headers http://www.slashdot.org/') + return # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: |