UNPKG

7.1 kBtext/coffeescriptView Raw
1_ = require('lodash')
2sinon = require('sinon')
3chai = require('chai')
4chai.use(require('sinon-chai'))
5expect = chai.expect
6cliManager = require('../lib/capitano')
7utils = require('../lib/utils')
8
9describe 'Capitano:', ->
10
11 beforeEach ->
12 cliManager.state.commands = []
13 cliManager.state.globalOptions = []
14 cliManager.state.permissions = {}
15
16 describe '#permission()', ->
17
18 it 'should throw an error if no name', ->
19 expect ->
20 cliManager.permission()
21 .to.throw('Missing permission name')
22
23 it 'should throw an error if name is not a string', ->
24 expect ->
25 cliManager.permission([ 'permissions' ])
26 .to.throw('Invalid permission name')
27
28 it 'should throw an error if no function', ->
29 expect ->
30 cliManager.permission('hello')
31 .to.throw('Missing permission function')
32
33 it 'should throw an error if function is not a function', ->
34 expect ->
35 cliManager.permission('name', [ 'function' ])
36 .to.throw('Invalid permission function')
37
38 it 'should add a permissions', ->
39 expect(cliManager.state.permissions.user).to.not.exist
40 cliManager.permission('user', _.noop)
41 expect(cliManager.state.permissions.user).to.exist
42 expect(cliManager.state.permissions.user).to.equal(_.noop)
43
44 describe '#command()', ->
45
46 it 'should add a command', ->
47 expect(cliManager.state.commands).to.have.length(0)
48
49 cliManager.command
50 signature: 'hello <name>'
51 action: _.noop
52
53 expect(cliManager.state.commands).to.have.length(1)
54 command = cliManager.state.commands[0]
55 expect(command.signature.toString()).to.equal('hello <name>')
56
57 it 'should add a command with options', ->
58 expect ->
59 cliManager.command
60 signature: 'hello <name>'
61 action: _.noop
62 options: [
63 {
64 signature: 'quiet'
65 boolean: true
66 }
67 {
68 signature: 'yes'
69 boolean: true
70 }
71 ]
72 .to.not.throw(Error)
73
74 commandOptions = cliManager.state.commands[0].options
75 expect(commandOptions[0].signature.toString()).to.equal('quiet')
76 expect(commandOptions[1].signature.toString()).to.equal('yes')
77
78 # Caused by command() modifying the original object
79 it 'should allow using the same option object multiple times', ->
80 yesOption =
81 signature: 'yes'
82 boolean: true
83
84 cliManager.command
85 signature: 'foo <bar>'
86 action: _.noop
87 options: [ yesOption ]
88
89 expect ->
90 cliManager.command
91 signature: 'hello <name>'
92 action: _.noop
93 options: [ yesOption ]
94 .to.not.throw(Error)
95
96 describe '#option()', ->
97
98 it 'should add an option', ->
99 expect(cliManager.state.globalOptions).to.have.length(0)
100
101 cliManager.globalOption
102 signature: 'quiet'
103 boolean: true
104
105 expect(cliManager.state.globalOptions).to.have.length(1)
106 option = cliManager.state.globalOptions[0]
107 expect(option.signature.toString()).to.equal('quiet')
108
109 describe '#execute()', ->
110
111 it 'should execute a command', (done) ->
112 spy = sinon.spy()
113
114 cliManager.command
115 signature: 'hello <name>'
116 action: spy
117
118 cliManager.execute command: 'hello John', (error) ->
119 expect(error).to.not.exist
120 expect(spy).to.have.been.calledOnce
121 expect(spy).to.have.been.calledWith(name: 'John')
122 done()
123
124 it 'should call commandNotFound if command not found', ->
125 commandNotFoundStub = sinon.stub(cliManager.defaults.actions, 'commandNotFound')
126 cliManager.execute(command: 'not valid command')
127 expect(commandNotFoundStub).to.have.been.called
128 expect(commandNotFoundStub).to.have.been.calledWith('not valid command')
129 commandNotFoundStub.restore()
130
131 it 'should return an error if there was an error executing the command', (done) ->
132 commandNotFoundStub = sinon.stub(cliManager.defaults.actions, 'commandNotFound')
133
134 cliManager.command
135 signature: 'hello <name>'
136 action: _.noop
137
138 cliManager.execute command: 'hello', (error) ->
139 expect(error).to.be.an.instanceof(Error)
140 expect(error.message).to.equal('Missing name')
141 commandNotFoundStub.restore()
142 done()
143
144 it 'should pass an execution error to the default handler', (done) ->
145 actionError = new Error('action error')
146
147 cliManager.command
148 signature: 'hello'
149 action: (params, options, callback) ->
150 return callback(actionError)
151
152 cliManager.execute command: 'hello', (error) ->
153 expect(error).to.deep.equal(actionError)
154 done()
155
156 it 'should pass an async execution error to the default handler', (done) ->
157 actionError = new Error('action error')
158
159 cliManager.command
160 signature: 'hello'
161 action: (params, options, callback) ->
162 setTimeout ->
163 return callback(actionError)
164 , 1
165
166 cliManager.execute command: 'hello', (error) ->
167 expect(error).to.deep.equal(actionError)
168 done()
169
170 it 'should not throw an error if missing callback', ->
171 cliManager.command
172 signature: 'hello'
173 action: (params, options, callback) ->
174 return callback(actionError)
175
176 expect ->
177 cliManager.execute(command: 'hello')
178 .to.not.throw(Error)
179
180 describe 'given a stdin command', ->
181
182 describe 'if parameter was passed', ->
183
184 it 'should execute correctly', (done) ->
185 spy = sinon.spy()
186
187 cliManager.command
188 signature: 'hello <|name>'
189 action: spy
190
191 cliManager.execute command: 'hello John', (error) ->
192 expect(error).to.not.exist
193 expect(spy).to.have.been.calledOnce
194 expect(spy).to.have.been.calledWith(name: 'John')
195 done()
196
197 describe 'if stdin was passed', ->
198
199 beforeEach ->
200 @utilsGetStdinStub = sinon.stub(utils, 'getStdin')
201 @utilsGetStdinStub.callsArgWithAsync(0, 'Jane')
202
203 afterEach ->
204 @utilsGetStdinStub.restore()
205
206 it 'should execute correctly', (done) ->
207 spy = sinon.spy()
208
209 cliManager.command
210 signature: 'hello <|name>'
211 action: spy
212
213 cliManager.execute command: 'hello', (error) ->
214 expect(error).to.not.exist
215 expect(spy).to.have.been.calledOnce
216 expect(spy).to.have.been.calledWith(name: 'Jane')
217 done()
218
219 describe '#run()', ->
220
221 it 'should parse and execute a command', (done) ->
222 spy = sinon.spy()
223
224 cliManager.command
225 signature: 'hello <name>'
226 action: spy
227
228 cliManager.run 'hello John', (error) ->
229 expect(error).to.not.exist
230 expect(spy).to.have.been.calledOnce
231 expect(spy).to.have.been.calledWith(name: 'John')
232 done()
233
234 it 'should pass an option value starting with a number correctly', (done) ->
235 spy = sinon.spy()
236
237 cliManager.command
238 signature: 'hello'
239 action: spy
240 options: [
241 signature: 'application'
242 parameter: 'application'
243 ]
244
245 cliManager.run 'hello --application 10Jun2014', (error) ->
246 expect(error).to.not.exist
247 expect(spy).to.have.been.calledOnce
248 expect(spy).to.have.been.calledWith({}, application: '10Jun2014')
249 done()
250
251 it 'should pass any error to the callback', (done) ->
252 cliManager.command
253 signature: 'hello <name>'
254 action: (params, options, callback) ->
255 return callback(new Error())
256
257 cliManager.run 'hello', (error) ->
258 expect(error).to.be.an.instanceof(Error)
259 expect(error.message).to.equal('Missing name')
260 done()