From 1c49e4dda0674f8daae3ad397a0ef975cb257dc8 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Mon, 22 Dec 2014 16:31:40 -0800 Subject: [PATCH] Use Hubot.Message directly with Hubot >= 2.11.0 Hubot 2.11.0 now exports Message directly, so we can stop pulling out TextMessage's superclass. We still need the workaround when using older versions of Hubot. --- package.json | 2 +- src/message.coffee | 11 +++++++---- test/message.coffee | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 1c8a9d74..1349cb09 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "mocha": "~1.13.0", "grunt-contrib-watch": "~0.5.3", "grunt-shell": "~0.5.0", - "hubot": "~2.10" + "hubot": "~2.11" }, "main": "./index", "engines": { diff --git a/src/message.coffee b/src/message.coffee index 1036e059..99961df7 100644 --- a/src/message.coffee +++ b/src/message.coffee @@ -1,4 +1,10 @@ -{TextMessage} = require 'hubot' +{Message, TextMessage} = require 'hubot' + +# Hubot only started exporting Message in 2.11.0. Previous version do not export +# this class. In order to remain compatible with older versions, we can pull the +# Message class from TextMessage superclass. +if not Message + Message = TextMessage.__super__.constructor class SlackTextMessage extends TextMessage # Represents a TextMessage created from the Slack adapter @@ -10,9 +16,6 @@ class SlackTextMessage extends TextMessage constructor: (@user, @text, @rawText, @rawMessage) -> super @user, @text, @rawMessage.ts -# For some reason Hubot doesn't export Message, but that's what we want to extend. -# As a workaround, let's grab TextMessage's superclass -Message = TextMessage.__super__.constructor class SlackRawMessage extends Message # Represents Slack messages that are not suitable to treat as text messages. # These are hidden messages, or messages that have no text / attachments. diff --git a/test/message.coffee b/test/message.coffee index 8810780d..2a20406b 100644 --- a/test/message.coffee +++ b/test/message.coffee @@ -132,3 +132,38 @@ describe 'Receiving a Slack message', -> @stubs.robot.received.should.have.length 1 msg = @stubs.robot.received[0] msg.should.be.an.instanceOf SlackRawMessage + + describe 'should handle SlackRawMessage inheritance properly when Hubot', -> + # this is a bit of a wacky one + # We need to muck with the require() machinery + # To that end, we're going to save off the modules we need to tweak, so we can + # remove the cache and reload from disk + beforeEach -> + mods = (require.resolve name for name in ['hubot', '../src/message']) + @saved = [] + # ensure the modules are loaded + require path for path in mods # ensure the modules are loaded + @saved = (require.cache[path] for path in mods) # grab the modules + delete require.cache[path] for path in mods # remove the modules from the require cache + + afterEach -> + # restore the saved modules + for mod in @saved + require.cache[mod.filename] = mod + delete @saved + + it 'does not export Message', -> + delete require('hubot').Message # remove hubot.Message if it exists + {SlackRawMessage: rawMessage} = require '../src/message' + rawMessage::constructor.__super__.constructor.name.should.equal 'Message' + + it 'does export Message', -> + if not require('hubot').Message + # create a placeholder class to use here + # We're not actually running any code from Message during the evaluation + # of src/message.coffee so we don't need the real class. + # note: using JavaScript escape because CoffeeScript doesn't allow shadowing otherwise + `function Message() {}` + require('hubot').Message = Message + {SlackRawMessage: rawMessage} = require '../src/message' + rawMessage::constructor.__super__.constructor.name.should.equal 'Message'