-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathcommon.js
339 lines (290 loc) · 11.1 KB
/
common.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("power-assert")
const nodeApi = require("../lib")
const BufferStream = require("./lib/buffer-stream")
const util = require("./lib/util")
const result = util.result
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------
describe("[common]", () => {
before(() => process.chdir("test-workspace"))
after(() => process.chdir(".."))
beforeEach(removeResult)
describe("should print a help text if arguments are nothing.", () => {
it("npm-run-all command", async () => {
const buf = new BufferStream()
await runAll([], buf)
assert(/Usage:/.test(buf.value))
})
it("run-s command", async () => {
const buf = new BufferStream()
await runSeq([], buf)
assert(/Usage:/.test(buf.value))
})
it("run-p command", async () => {
const buf = new BufferStream()
await runPar([], buf)
assert(/Usage:/.test(buf.value))
})
})
describe("should print a help text if the first argument is --help (-h)", () => {
it("npm-run-all command (-h)", async () => {
const buf = new BufferStream()
await runAll(["-h"], buf)
assert(/Usage:/.test(buf.value))
})
it("run-s command (-h)", async () => {
const buf = new BufferStream()
await runSeq(["-h"], buf)
assert(/Usage:/.test(buf.value))
})
it("run-p command (-h)", async () => {
const buf = new BufferStream()
await runPar(["-h"], buf)
assert(/Usage:/.test(buf.value))
})
it("npm-run-all command (--help)", async () => {
const buf = new BufferStream()
await runAll(["--help"], buf)
assert(/Usage:/.test(buf.value))
})
it("run-s command (--help)", async () => {
const buf = new BufferStream()
await runSeq(["--help"], buf)
assert(/Usage:/.test(buf.value))
})
it("run-p command (--help)", async () => {
const buf = new BufferStream()
await runPar(["--help"], buf)
assert(/Usage:/.test(buf.value))
})
})
describe("should print a version number if the first argument is --version (-v)", () => {
it("npm-run-all command (-v)", async () => {
const buf = new BufferStream()
await runAll(["-v"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
it("run-s command (-v)", async () => {
const buf = new BufferStream()
await runSeq(["-v"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
it("run-p command (-v)", async () => {
const buf = new BufferStream()
await runPar(["-v"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
it("npm-run-all command (--version)", async () => {
const buf = new BufferStream()
await runAll(["--version"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
it("run-s command (--version)", async () => {
const buf = new BufferStream()
await runSeq(["--version"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
it("run-p command (--version)", async () => {
const buf = new BufferStream()
await runPar(["--version"], buf)
assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
})
})
describe("should do nothing if a task list is empty.", () => {
it("Node API", async () => {
await nodeApi(null)
assert(result() == null)
})
})
describe("should run a task by npm (check an environment variable):", () => {
it("Node API", async () => {
await nodeApi("test-task:package-config")
assert(result() === "OK")
})
it("npm-run-all command", async () => {
await runAll(["test-task:package-config"])
assert(result() === "OK")
})
it("run-s command", async () => {
await runSeq(["test-task:package-config"])
assert(result() === "OK")
})
it("run-p command", async () => {
await runPar(["test-task:package-config"])
assert(result() === "OK")
})
})
describe("stdin can be used in tasks:", () => {
it("Node API", async () => {
await nodeApi("test-task:stdin")
assert(result().trim() === "STDIN")
})
it("npm-run-all command", async () => {
await runAll(["test-task:stdin"])
assert(result().trim() === "STDIN")
})
it("run-s command", async () => {
await runSeq(["test-task:stdin"])
assert(result().trim() === "STDIN")
})
it("run-p command", async () => {
await runPar(["test-task:stdin"])
assert(result().trim() === "STDIN")
})
})
describe("stdout can be used in tasks:", () => {
it("Node API", async () => {
await nodeApi("test-task:stdout")
assert(result() === "STDOUT")
})
it("npm-run-all command", async () => {
await runAll(["test-task:stdout"])
assert(result() === "STDOUT")
})
it("run-s command", async () => {
await runSeq(["test-task:stdout"])
assert(result() === "STDOUT")
})
it("run-p command", async () => {
await runPar(["test-task:stdout"])
assert(result() === "STDOUT")
})
})
describe("stderr can be used in tasks:", () => {
it("Node API", async () => {
await nodeApi("test-task:stderr")
assert(result() === "STDERR")
})
it("npm-run-all command", async () => {
await runAll(["test-task:stderr"])
assert(result() === "STDERR")
})
it("run-s command", async () => {
await runSeq(["test-task:stderr"])
assert(result() === "STDERR")
})
it("run-p command", async () => {
await runPar(["test-task:stderr"])
assert(result() === "STDERR")
})
})
describe("should be able to use `restart` built-in task:", () => {
it("Node API", () => nodeApi("restart"))
it("npm-run-all command", () => runAll(["restart"]))
it("run-s command", () => runSeq(["restart"]))
it("run-p command", () => runPar(["restart"]))
})
describe("should be able to use `env` built-in task:", () => {
it("Node API", () => nodeApi("env"))
it("npm-run-all command", () => runAll(["env"]))
it("run-s command", () => runSeq(["env"]))
it("run-p command", () => runPar(["env"]))
})
if (process.platform === "win32") {
describe("issue14", () => {
it("Node API", () => nodeApi("test-task:issue14:win32"))
it("npm-run-all command", () => runAll(["test-task:issue14:win32"]))
it("run-s command", () => runSeq(["test-task:issue14:win32"]))
it("run-p command", () => runPar(["test-task:issue14:win32"]))
})
}
else {
describe("issue14", () => {
it("Node API", () => nodeApi("test-task:issue14:posix"))
it("npm-run-all command", () => runAll(["test-task:issue14:posix"]))
it("run-s command", () => runSeq(["test-task:issue14:posix"]))
it("run-p command", () => runPar(["test-task:issue14:posix"]))
})
}
describe("should not print log if silent option was given:", () => {
it("Node API", async () => {
const stdout = new BufferStream()
const stderr = new BufferStream()
try {
await nodeApi("test-task:error", { silent: true, stdout, stderr })
}
catch (_err) {
assert(stdout.value === "" && stderr.value === "")
return
}
assert(false, "Should fail.")
})
/**
* Strip unknown istanbul's warnings.
* @param {string} str - The string to be stripped.
* @returns {string} The stripped string.
*/
function stripIstanbulWarnings(str) {
return str.replace(/File \[.+?] ignored, nothing could be mapped\r?\n/, "")
}
it("npm-run-all command", async () => {
const stdout = new BufferStream()
const stderr = new BufferStream()
try {
await runAll(["--silent", "test-task:error"], stdout, stderr)
}
catch (_err) {
assert(stdout.value === "" && stripIstanbulWarnings(stderr.value) === "")
return
}
assert(false, "Should fail.")
})
it("run-s command", async () => {
const stdout = new BufferStream()
const stderr = new BufferStream()
try {
await runSeq(["--silent", "test-task:error"], stdout, stderr)
}
catch (_err) {
assert(stdout.value === "" && stripIstanbulWarnings(stderr.value) === "")
return
}
assert(false, "Should fail.")
})
it("run-p command", async () => {
const stdout = new BufferStream()
const stderr = new BufferStream()
try {
await runPar(["--silent", "test-task:error"], stdout, stderr)
}
catch (_err) {
assert(stdout.value === "" && stripIstanbulWarnings(stderr.value) === "")
return
}
assert(false, "Should fail.")
})
})
// https://github.com/mysticatea/npm-run-all/issues/105
describe("should not print MaxListenersExceededWarning when it runs 10 tasks:", () => {
const tasks = Array.from({ length: 10 }, () => "test-task:append:a")
it("npm-run-all command", async () => {
const buf = new BufferStream()
await runAll(tasks, null, buf)
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
})
it("run-s command", async () => {
const buf = new BufferStream()
await runSeq(tasks, null, buf)
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
})
it("run-p command", async () => {
const buf = new BufferStream()
await runPar(tasks, null, buf)
assert(buf.value.indexOf("MaxListenersExceededWarning") === -1)
})
})
})