-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
run-lifecycle.spec.ts
276 lines (232 loc) · 6.71 KB
/
run-lifecycle.spec.ts
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
"use strict";
jest.mock("npm-lifecycle", () => jest.fn(() => Promise.resolve()));
const log = require("npmlog");
const { loggingOutput } = require("@lerna-test/logging-output");
const runScript = require("npm-lifecycle");
const { npmConf } = require("../npm-conf");
const { Package } = require("../../package");
const { runLifecycle, createRunner } = require("../run-lifecycle");
describe("runLifecycle()", () => {
it("skips packages without scripts", async () => {
const pkg = {
name: "no-scripts",
};
await runLifecycle(pkg, "foo", new Map());
expect(runScript).not.toHaveBeenCalled();
});
it("skips packages without matching script", async () => {
const pkg = {
name: "missing-script",
scripts: {
test: "foo",
},
};
await runLifecycle(pkg, "bar", new Map());
expect(runScript).not.toHaveBeenCalled();
});
it("calls npm-lifecycle with prepared arguments", async () => {
const pkg = new Package(
{
name: "test-name",
version: "1.0.0-test",
scripts: {
preversion: "test",
},
engines: {
node: ">= 8.9.0",
},
},
"/test/location"
);
const stage = "preversion";
const opts = npmConf({ "custom-cli-flag": true });
await runLifecycle(pkg, stage, opts);
expect(runScript).toHaveBeenLastCalledWith(
expect.objectContaining({
name: pkg.name,
version: pkg.version,
engines: {
node: ">= 8.9.0",
},
_id: `${pkg.name}@${pkg.version}`,
}),
stage,
pkg.location,
expect.objectContaining({
config: expect.objectContaining({
"custom-cli-flag": true,
}),
dir: pkg.location,
failOk: false,
log: expect.any(Object),
unsafePerm: true,
})
);
});
it("camelCases dashed-options", async () => {
const pkg = {
name: "dashed-name",
version: "1.0.0-dashed",
location: "dashed-location",
scripts: {
"dashed-options": "test",
},
};
const dir = pkg.location;
const stage = "dashed-options";
const opts = {
"ignore-prepublish": true,
"ignore-scripts": false,
"node-options": "--a-thing",
"script-shell": "fish",
"scripts-prepend-node-path": true,
"unsafe-perm": false,
};
await runLifecycle(pkg, stage, opts);
expect(runScript).toHaveBeenLastCalledWith(expect.objectContaining(pkg), stage, dir, {
config: expect.objectContaining({
"node-options": "--a-thing",
"script-shell": "fish",
}),
dir,
failOk: false,
log,
nodeOptions: "--a-thing",
scriptShell: "fish",
scriptsPrependNodePath: true,
unsafePerm: false,
});
});
it("ignores prepublish when configured", async () => {
const pkg = {
name: "ignore-prepublish",
scripts: {
prepublish: "test",
},
};
const stage = "prepublish";
const opts = {
"ignore-prepublish": true,
};
await runLifecycle(pkg, stage, opts);
expect(runScript).not.toHaveBeenCalled();
});
it("ignores scripts when configured", async () => {
const pkg = {
name: "ignore-scripts",
scripts: {
ignored: "test",
},
};
const stage = "ignored";
const opts = {
"ignore-scripts": true,
};
await runLifecycle(pkg, stage, opts);
expect(runScript).not.toHaveBeenCalled();
});
it("omits circular opts", async () => {
const pkg = {
name: "circular-name",
version: "1.0.0-circular",
location: "circular-location",
scripts: {
prepack: "test",
},
};
const stage = "prepack";
const opts = {};
await runLifecycle(pkg, stage, opts);
const callOpts = runScript.mock.calls.pop().pop();
expect(callOpts).not.toHaveProperty("config.log");
expect(callOpts).not.toHaveProperty("config.logstream");
});
});
describe("createRunner", () => {
const runPackageLifecycle = createRunner({ "other-cli-flag": 0 });
it("creates partially-applied function with npm conf", async () => {
const pkg = {
name: "partially-applied",
version: "1.2.3",
location: "test",
scripts: { version: "echo yay" },
};
const stage = "version";
await runPackageLifecycle(pkg, stage);
expect(runScript).toHaveBeenLastCalledWith(
expect.any(Object),
stage,
pkg.location,
expect.objectContaining({
config: expect.objectContaining({
"other-cli-flag": 0,
}),
})
);
});
it("skips missing scripts block", async () => {
const pkg = {
name: "missing-scripts-block",
version: "1.0.0",
location: "test",
};
await runPackageLifecycle(pkg, "prepare");
expect(runScript).not.toHaveBeenCalled();
});
it("skips missing script", async () => {
const pkg = {
name: "missing-script",
version: "1.0.0",
location: "test",
scripts: { test: "echo foo" },
};
await runPackageLifecycle(pkg, "prepare");
expect(runScript).not.toHaveBeenCalled();
});
it("logs script error and re-throws", async () => {
runScript.mockImplementationOnce(({ scripts }, stage) => {
const err = new Error("boom");
// https://github.com/npm/npm-lifecycle/blob/d7a014f9393da76ee0ecf6701be84ed1ea613b83/index.js#L302
err.errno = 123;
err.script = scripts[stage];
return Promise.reject(err);
});
const pkg = {
name: "has-script-error",
version: "1.0.0",
location: "test",
scripts: { prepublishOnly: "exit 123" },
};
await expect(runPackageLifecycle(pkg, "prepublishOnly")).rejects.toThrow(
expect.objectContaining({
exitCode: 123,
script: "exit 123",
})
);
expect(process.exitCode).toBe(123);
const [errorLog] = loggingOutput("error");
expect(errorLog).toBe('"prepublishOnly" errored in "has-script-error", exiting 123');
});
it("defaults error exit code to 1", async () => {
runScript.mockImplementationOnce(({ scripts }, stage) => {
const err = new Error("kersplode");
// errno only gets added when a proc closes, not from error
err.script = scripts[stage];
return Promise.reject(err);
});
const pkg = {
name: "has-execution-error",
version: "1.0.0",
location: "test",
scripts: { prepack: "a-thing-that-ends-poorly" },
};
await expect(runPackageLifecycle(pkg, "prepack")).rejects.toThrow(
expect.objectContaining({
exitCode: 1,
script: "a-thing-that-ends-poorly",
})
);
const [errorLog] = loggingOutput("error");
expect(errorLog).toBe('"prepack" errored in "has-execution-error", exiting 1');
});
});