Permalink
Browse files

Test router; Test error; Emit error event

  • Loading branch information...
1 parent c99bf9e commit 452c1a813ec08754e23b639019247ee8440eb428 @wdavidw committed Nov 29, 2011
Showing with 113 additions and 14 deletions.
  1. +6 −5 README.md
  2. +2 −1 lib/NullStream.coffee
  3. +3 −0 lib/Shell.coffee
  4. +6 −2 lib/plugins/error.coffee
  5. +54 −0 test/ErrorTest.coffee
  6. +6 −6 test/PluginHttpTest.coffee
  7. +29 −0 test/RouterTest.coffee
  8. +7 −0 test/ShellTest.coffee
View
@@ -101,12 +101,13 @@ If `app.configure` is called without specifying the environment as the first arg
By extending `EventEmitter`, the following events are emitted:
-- `"command"`, listen to all executed commands, provide the command name as first argument
-- `#{command}`, listen to a particular event
-- `"quit"`, called when the application is about to quit
-- `"exit"`, called when the process exit
+- `"command"` , listen to all executed commands, provide the command name as first argument.
+- `#{command}` , listen to a particular event.
+- `"quit"` , called when the application is about to quit.
+- `"error"` , called on error providing the error object as the first callback argument.
+- `"exit"` , called when the process exit.
-## Routes plugin
+## Router plugin
The functionalities are a full transcription of the ones present Express. Options passed during creation are:
@@ -9,7 +9,8 @@ module.exports = class NullStream extends events.EventEmitter
pipe: ->
# Writable Stream
writable: true
- write: ->
+ write: (data) ->
+ @emit 'data', data
end: ->
# Shared API
destroy: ->
View
@@ -100,12 +100,14 @@ module.exports = class Shell extends EventEmitter
next = (err) ->
layer = self.stack[ index++ ]
if not layer
+ return self.emit('error', err) if err
if command isnt ''
res.red 'Command failed to execute ' + command + ( if err then ': ' + err.message else '')
return res.prompt()
arity = layer.handle.length
if err
if arity is 4
+ self.emit('error', err)
layer.handle err, req, res, next
else
next err
@@ -192,3 +194,4 @@ module.exports = class Shell extends EventEmitter
@emit 'quit'
@interface().close()
@settings.stdin.destroy()
+ #@set 'stdin', null
@@ -3,12 +3,16 @@ module.exports = (settings) ->
# Validation
throw new Error 'No shell provided' if not settings.shell
shell = settings.shell
+ # Define empty error handler to avoir shell to trow error of no event
+ # handler are defined
+ shell.on 'error', ->
+ # Route
(err, req, res, next) ->
res.red(err.message).ln() if err.message
res.red(err.stack).ln() if err.stack
for k, v of err
continue if k is 'message'
- continue if k is 'stock'
+ continue if k is 'stack'
continue if typeof v is 'function'
- res.red(k).white(': ').red(v).ln()
+ res.magenta(k).white(': ').red(v).ln()
res.prompt()
View
@@ -0,0 +1,54 @@
+
+shell = require '..'
+assert = require 'assert'
+
+module.exports =
+ 'Test # plugin # throw # print error': (next) ->
+ stdout = new shell.NullStream
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test error'
+ stdin: new shell.NullStream
+ stdout: stdout
+ app.configure ->
+ app.use shell.http
+ app.use shell.router shell: app
+ app.use shell.error shell: app
+ app.cmd 'test error', (req, res) ->
+ assert.ok false
+ out = ''
+ stdout.on 'data', (data) ->
+ out += data
+ app.on 'quit', ->
+ assert.ok /AssertionError/.test out
+ next()
+ 'Test # plugin # throw # emit error': (next) ->
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test error'
+ stdin: new shell.NullStream
+ stdout: new shell.NullStream
+ app.configure ->
+ app.use shell.http
+ app.use shell.router shell: app
+ app.use shell.error shell: app
+ app.cmd 'test error', (req, res) ->
+ assert.ok false
+ app.on 'error', (err) ->
+ assert.eql err.name, 'AssertionError'
+ next()
+ 'Test # no plugin # next # emit error': (next) ->
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test error'
+ stdin: new shell.NullStream
+ stdout: new shell.NullStream
+ app.configure ->
+ app.use shell.http
+ app.use shell.router shell: app
+ app.cmd 'test error', (req, res, n) ->
+ n new Error 'My error'
+ app.on 'error', (err) ->
+ assert.eql err.message, 'My error'
+ next()
+
@@ -8,14 +8,14 @@ module.exports =
app = shell
workspace: "#{__dirname}/plugins_http"
command: null
+ stdin: new shell.NullStream
stdout: new shell.NullStream
- stderr: new shell.NullStream
app.configure ->
- app.use shell.history(shell: app)
- app.use shell.completer(shell: app)
+ #app.use shell.history(shell: app)
+ #app.use shell.completer(shell: app)
app.use shell.http
app.use shell.router shell: app
- app.use shell.error shell: app
+ #app.use shell.error shell: app
app.run 'http start'
setTimeout ->
http.get(
@@ -36,8 +36,8 @@ module.exports =
).on 'error', (e) ->
assert.ok e instanceof Error
next()
- , 200
+ , 300
).on 'error', (e) ->
assert.ifError e
next e
- , 200
+ , 300
@@ -0,0 +1,29 @@
+
+shell = require '..'
+assert = require 'assert'
+
+module.exports =
+ 'Test simple': (next) ->
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test simple'
+ stdin: new shell.NullStream
+ stdout: new shell.NullStream
+ app.configure ->
+ app.use shell.http
+ app.use shell.router shell: app
+ app.cmd 'test simple', (req, res) ->
+ next()
+ 'Test param': (next) ->
+ app = shell
+ workspace: "#{__dirname}/plugins_http"
+ command: 'test my_value'
+ stdin: new shell.NullStream
+ stdout: new shell.NullStream
+ app.configure ->
+ app.use shell.http
+ app.use shell.router shell: app
+ app.cmd 'test :my_param', (req, res) ->
+ assert.eql req.params.my_param, 'my_value'
+ next()
+
@@ -3,11 +3,18 @@ shell = require '../index'
assert = require 'assert'
module.exports =
+ ###
+ Note
+ version 0.4.x didn't hold currentprocess if `process.stdin`
+ was referenced, so `app.quit()` was not required
+ ###
'Shell # default constructor': (next) ->
# Test object
app = new shell { command: '' }
assert.ok app instanceof shell
+ app.quit()
# Test function
app = shell { command: '' }
assert.ok app instanceof shell
+ app.quit()
next()

0 comments on commit 452c1a8

Please sign in to comment.