This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
/
test.js
180 lines (166 loc) · 5.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*!
* async-exec-cmd <https://github.com/tunnckoCore/async-exec-cmd>
*
* Copyright (c) 2015 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var test = require('assertit')
var cmd = require('./index')
test('should throw when only function is given', function (done) {
function fixture () {
cmd(function _cb (err) {
/* istanbul ignore next */
err = err || ''
})
}
test.throws(fixture, Error)
done()
})
test('should throw "should have `callback` (non empty callback)"', function () {
test('when only object is given', function (done) {
function fixture () {
cmd({stdio: 'inherit'})
}
test.throws(fixture, Error)
done()
})
test('when array is given', function (done) {
function fixture () {
cmd(['--save', 'bluebird'])
}
test.throws(fixture, Error)
done()
})
test('when array and object are given', function (done) {
function fixture () {
cmd(['--save', 'bluebird'], {stdio: 'inherit'})
}
test.throws(fixture, Error)
done()
})
test('when callback with empty body', function (done) {
function fixture () {
cmd('npm install --save is-glob', function _cb () {})
}
test.throws(fixture, Error)
done()
})
})
test('should have Error in callback', function () {
test('when process could not be spawned', function (done) {
cmd('jkdshfkj4hkjh435', function _cb (err) {
test.ok(err instanceof Error)
done()
})
})
})
test('should have TypeError in callback', function () {
test('when object and callback given', function (done) {
cmd({stdio: 'inherit'}, function _cb (err) {
test.ok(err instanceof TypeError)
done()
})
})
test('when array and callback are given', function (done) {
cmd(['--save', 'bluebird'], function _cb (err) {
test.ok(err instanceof TypeError)
done()
})
})
test('when array, object and callback are given', function (done) {
cmd(['--save', 'bluebird'], {stdio: 'inherit'}, function _cb (err) {
test.ok(err instanceof TypeError)
done()
})
})
test('when object, array and callback are given', function (done) {
cmd({stdio: 'inherit'}, ['--save', 'bluebird'], function _cb (err) {
test.ok(err instanceof TypeError)
done()
})
})
})
test('should return `stream` and have `.kill` method when executed', function (done) {
var cp = cmd('npm version', function _cb (err) {
/* istanbul ignore next */
err = err || ''
})
test.equal(typeof cp.kill, 'function')
done()
})
test('should have empty res and err null when opts.stdio is inherit', function (done) {
cmd('npm install --save is-glob', {stdio: 'inherit'}, function _cb (err, res, code) {
test.equal(err, null)
test.equal(code, 0) // status code
test.equal(typeof res, 'string') // response
test.equal(res.length, 0)
done()
})
})
function cmdErrorTest (err, res, code, buf, done) {
test.equal(res, undefined)
test.equal(buf, undefined)
test.equal(code, 1)
test.equal(err.status, 1)
test.equal(err.name, 'CommandError')
done()
}
test('should have CommandError in callback when', function () {
test('asyncExecCmd("npm", cb)', function (done) {
cmd('npm', function _cb (err, res, code, buf) {
cmdErrorTest(err, res, code, buf, done)
})
})
test('asyncExecCmd("npm", opts, cb)', function (done) {
cmd('npm', {stdio: [null, null, null]}, function _cb (err, res, code, buf) {
cmdErrorTest(err, res, code, buf, done)
})
})
})
test('should accept single command and arguments in array', function () {
test("asyncExecCmd('npm', ['i', '--save', 'is-glob'], cb)", function (done) {
cmd('npm', ['install', '--save', 'is-glob'],
function _cb (err, res, code) {
test.equal(err, null)
test.equal(code, 0)
test.equal(typeof res, 'string')
test.ok(res.length > 0)
done()
})
})
test("asyncExecCmd('npm', ['uni', '--save', 'is-glob'], opts, cb)", function (done) {
cmd('npm', ['uninstall', '--save', 'is-glob'], {stdio: [null, null, null]},
function _cb (err, res, code) {
test.equal(err, null)
test.equal(code, 0)
test.equal(typeof res, 'string')
test.ok(res.length > 0)
done()
})
})
})
test('should accept multiple words in `cmd` and `args` in array', function () {
test('should recieve CommandError if unknown subcommand given', function (done) {
cmd('npm unknown', function _cb (err, res, code, buffer) {
test.equal(res, undefined)
test.equal(buffer, undefined)
test.equal(code, 1)
test.equal(err.status, 1)
test.equal(err.name, 'CommandError')
done()
})
})
test('should work when asyncExecCmd("npm install", ["node-foo", "node-bar"], opts, cb)', function (done) {
var opts = {stdio: [null, null, null]}
var args = ['node-foo', 'node-bar']
cmd('npm install', args, opts, function _cb (err, res, code) {
test.equal(err, null)
test.equal(code, 0)
test.equal(typeof res, 'string')
test.ok(res.length > 0)
done()
})
})
})