-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathfail.js
120 lines (102 loc) · 5.13 KB
/
fail.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
/**
* @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 util = require("./lib/util")
const delay = util.delay
const removeResult = util.removeResult
const runAll = util.runAll
const runPar = util.runPar
const runSeq = util.runSeq
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Throws an assertion error if a given promise comes to be fulfilled.
*
* @param {Promise} p - A promise to check.
* @returns {Promise} A promise which is checked.
*/
function shouldFail(p) {
return p.then(
() => assert(false, "should fail"),
() => null // OK!
)
}
//------------------------------------------------------------------------------
// Test
//------------------------------------------------------------------------------
describe("[fail] it should fail", () => {
before(() => process.chdir("test-workspace"))
after(() => process.chdir(".."))
beforeEach(removeResult)
afterEach(() => delay(1000))
describe("if an invalid option exists.", () => {
it("npm-run-all command", () => shouldFail(runAll(["--invalid"])))
it("run-s command", () => shouldFail(runSeq(["--parallel"])))
it("run-p command", () => shouldFail(runPar(["--sequential"])))
it("npm-run-all command with --race without --parallel", () => shouldFail(runAll(["--race"])))
it("npm-run-all command with --r without --parallel", () => shouldFail(runAll(["--r"])))
it("run-s command with --race", () => shouldFail(runSeq(["--race"])))
it("run-s command with --r", () => shouldFail(runSeq(["--r"])))
})
describe("if invalid `options.taskList` is given.", () => {
it("Node API", () => shouldFail(nodeApi("test-task:append a", { taskList: { invalid: 0 } })))
})
describe("if unknown tasks are given:", () => {
it("Node API", () => shouldFail(nodeApi("unknown-task")))
it("npm-run-all command", () => shouldFail(runAll(["unknown-task"])))
it("run-s command", () => shouldFail(runSeq(["unknown-task"])))
it("run-p command", () => shouldFail(runPar(["unknown-task"])))
})
describe("if unknown tasks are given (2):", () => {
it("Node API", () => shouldFail(nodeApi(["test-task:append:a", "unknown-task"])))
it("npm-run-all command", () => shouldFail(runAll(["test-task:append:a", "unknown-task"])))
it("run-s command", () => shouldFail(runSeq(["test-task:append:a", "unknown-task"])))
it("run-p command", () => shouldFail(runPar(["test-task:append:a", "unknown-task"])))
})
describe("if package.json is not found:", () => {
before(() => process.chdir("no-package-json"))
after(() => process.chdir(".."))
it("Node API", () => shouldFail(nodeApi(["test-task:append:a"])))
it("npm-run-all command", () => shouldFail(runAll(["test-task:append:a"])))
it("run-s command", () => shouldFail(runSeq(["test-task:append:a"])))
it("run-p command", () => shouldFail(runPar(["test-task:append:a"])))
})
describe("if package.json does not have scripts field:", () => {
before(() => process.chdir("no-scripts"))
after(() => process.chdir(".."))
it("Node API", () => shouldFail(nodeApi(["test-task:append:a"])))
it("npm-run-all command", () => shouldFail(runAll(["test-task:append:a"])))
it("run-s command", () => shouldFail(runSeq(["test-task:append:a"])))
it("run-p command", () => shouldFail(runPar(["test-task:append:a"])))
})
describe("if tasks exited with non-zero code:", () => {
it("Node API", () => shouldFail(nodeApi("test-task:error")))
it("npm-run-all command", () => shouldFail(runAll(["test-task:error"])))
it("run-s command", () => shouldFail(runSeq(["test-task:error"])))
it("run-p command", () => shouldFail(runPar(["test-task:error"])))
})
describe("if tasks exited via a signal:", () => {
it("Node API", () => shouldFail(nodeApi("test-task:abort")))
it("npm-run-all command", () => shouldFail(runAll(["test-task:abort"])))
it("run-s command", () => shouldFail(runSeq(["test-task:abort"])))
it("run-p command", () => shouldFail(runPar(["test-task:abort"])))
it("with correct exit code", () => nodeApi("test-task:abort").then(() =>
assert(false, "should fail")
).catch(err => {
// In NodeJS versions > 6, the child process correctly sends back
// the signal + code of null. In NodeJS versions <= 6, the child
// process does not set the signal, and sets the code to 1.
const code = Number(process.version.match(/^v(\d+)/)[1]) > 6 ? 134 : 1
assert(err.code === code, "should have correct exit code")
}))
})
})