Skip to content

Commit

Permalink
Merge pull request #36 from mtsmfm/arr_deps
Browse files Browse the repository at this point in the history
feat: Allow loading multiple scripts [close #34, close #35]
  • Loading branch information
mtsmfm authored Aug 20, 2016
2 parents 587cd17 + 77f1c95 commit 397ba6c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,24 @@ class Room extends Hubot.Adapter
class Helper
@Response = MockResponse

constructor: (scriptsPath) ->
@scriptsPath = Path.resolve(Path.dirname(module.parent.filename), scriptsPath)
constructor: (scriptsPaths) ->
if not Array.isArray(scriptsPaths)
scriptsPaths = [scriptsPaths]
@scriptsPaths = scriptsPaths

createRoom: (options={}) ->
robot = new MockRobot(options.httpd)

if 'response' of options
robot.Response = options.response

if Fs.statSync(@scriptsPath).isDirectory()
for file in Fs.readdirSync(@scriptsPath).sort()
robot.loadFile @scriptsPath, file
else
robot.loadFile Path.dirname(@scriptsPath), Path.basename(@scriptsPath)
for script in @scriptsPaths
script = Path.resolve(Path.dirname(module.parent.filename), script)
if Fs.statSync(script).isDirectory()
for file in Fs.readdirSync(script).sort()
robot.loadFile script, file
else
robot.loadFile Path.dirname(script), Path.basename(script)

robot.brain.emit 'loaded'

Expand Down
23 changes: 23 additions & 0 deletions test/load-multiple-scripts_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Helper = require('../src/index')
helper = new Helper(['./scripts/hello-world.coffee', './scripts/bye.coffee'])

co = require('co')
expect = require('chai').expect

describe 'hello-world', ->
beforeEach ->
@room = helper.createRoom(httpd: false)

context 'user says hi to hubot', ->
beforeEach ->
co =>
yield @room.user.say 'alice', '@hubot hi'
yield @room.user.say 'bob', '@hubot bye'

it 'should reply to user', ->
expect(@room.messages).to.eql [
['alice', '@hubot hi']
['hubot', '@alice hi']
['bob', '@hubot bye']
['hubot', '@bob bye']
]
5 changes: 5 additions & 0 deletions test/scripts/bye.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Description:
# Test script
module.exports = (robot) ->
robot.respond /bye$/i, (msg) ->
msg.reply 'bye'

0 comments on commit 397ba6c

Please sign in to comment.