-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
create-command.test.js
375 lines (265 loc) · 11.4 KB
/
create-command.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"use strict";
jest.mock("pacote");
const fs = require("fs-extra");
const path = require("path");
const execa = require("execa");
const slash = require("slash");
// mocked modules
const pacote = require("pacote");
// helpers
const initFixture = require("@lerna-test/helpers").initFixtureFactory(__dirname);
const { gitAdd } = require("@lerna-test/helpers");
// file under test
const lernaCreate = require("@lerna-test/helpers").commandRunner(require("../command"));
// stabilize commit SHA
expect.addSnapshotSerializer(require("@lerna-test/helpers/serializers/serialize-git-sha"));
// assertion helpers
const addRemote = (cwd, remote = "origin", url = "git@github.com:test/test.git") =>
execa("git", ["remote", "add", remote, url], { cwd });
const diffStaged = (cwd, ...args) =>
execa("git", ["diff", "--cached", ...args], { cwd }).then((result) => result.stdout);
const initRemoteFixture = (fixtureName) =>
initFixture(fixtureName).then((cwd) => addRemote(cwd).then(() => cwd));
const gitLsOthers = (cwd, ...args) =>
execa("git", ["ls-files", "--others", "--exclude-standard", ...args], { cwd }).then(
(result) => result.stdout
);
const listUntracked = async (cwd) => {
const list = await gitLsOthers(cwd, "-z");
return list.split("\0").map((fp) => slash(fp));
};
const manifestCreated = async (cwd) => {
const file = await gitLsOthers(cwd, "--", "**/package.json");
return fs.readJSON(path.join(cwd, file));
};
describe("CreateCommand", () => {
pacote.manifest.mockImplementation(() => Promise.resolve({ version: "1.0.0-mocked" }));
// preserve value from @lerna-test/helpers/npm/set-npm-userconfig
const userconfig = process.env.npm_config_userconfig;
afterEach(() => {
// some tests delete or mangle this
if (process.env.npm_config_userconfig !== userconfig) {
process.env.npm_config_userconfig = userconfig;
}
});
it("requires a name argument", async () => {
const cwd = await initFixture("basic");
const command = lernaCreate(cwd)();
await expect(command).rejects.toThrow("Not enough non-option arguments");
});
it("throws when adding a git dependency", async () => {
const cwd = await initRemoteFixture("basic");
const command = lernaCreate(cwd)(
"git-pkg",
"--dependencies",
"git+ssh://git@notgithub.com/user/foo#semver:^1.2.3"
);
await expect(command).rejects.toThrow("Do not use git dependencies");
});
it("creates a stub package", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("my-pkg");
await gitAdd(cwd, ".");
const result = await diffStaged(cwd);
expect(result).toMatchSnapshot();
});
it("creates a stub package with a scoped name", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("@my-org/my-pkg");
const result = await listUntracked(cwd);
expect(result).toContain("packages/my-pkg/lib/my-pkg.js");
});
it("creates a stub package with transpiled output", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("my-pkg", "--es-module");
await gitAdd(cwd, ".");
const result = await diffStaged(cwd);
expect(result).toMatchSnapshot();
});
it("creates a stub cli", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("my-cli", "--bin");
await gitAdd(cwd, ".");
if (process.platform === "win32") {
// windows sucks at file permissions
await gitAdd(cwd, "--chmod", "+x", "--", path.normalize("packages/my-cli/bin/my-cli"));
}
const result = await diffStaged(cwd);
expect(result).toMatchSnapshot();
// yargs is automatically added when CLI is stubbed
expect(pacote.manifest).toHaveBeenLastCalledWith(
expect.objectContaining({
name: "yargs",
type: "tag",
fetchSpec: "latest",
}),
expect.objectContaining({
// an npm-conf snapshot
registry: "https://registry.npmjs.org/",
})
);
});
it("creates a stub cli with a custom name", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("my-cli", "--bin", "yay");
const result = await listUntracked(cwd);
expect(result).toContain("packages/my-cli/bin/yay");
});
it("creates a stub cli with transpiled output", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("my-es-cli", "--bin", "--es-module");
await gitAdd(cwd, ".");
if (process.platform === "win32") {
// windows sucks at file permissions
await gitAdd(cwd, "--chmod", "+x", "--", path.normalize("packages/my-es-cli/bin/my-es-cli"));
}
const result = await diffStaged(cwd);
expect(result).toMatchSnapshot();
});
it("defaults user name and email to git config", async () => {
const cwd = await initRemoteFixture("basic");
const name = "Git McGitterson";
const email = "test@git-fallback.com";
// overwrite test defaults so it's really obvious
await execa("git", ["config", "user.name", name], { cwd });
await execa("git", ["config", "user.email", email], { cwd });
// ignore test defaults as well as ~/.npmrc
process.env.npm_config_userconfig = "/dev/null";
await lernaCreate(cwd)("git-fallback");
expect(await manifestCreated(cwd)).toHaveProperty("author", `${name} <${email}>`);
});
it("overrides init-license with --license", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("license-override", "--license", "MIT");
expect(await manifestCreated(cwd)).toHaveProperty("license", "MIT");
});
it("sets private:true with --private", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("private-pkg", "--private");
expect(await manifestCreated(cwd)).toHaveProperty("private", true);
});
it("defaults to npm_config_init_version when independent", async () => {
const cwd = await initRemoteFixture("independent");
process.env.npm_config_init_version = "100.0.0";
await lernaCreate(cwd)("indy-pkg");
delete process.env.npm_config_init_version;
expect(await manifestCreated(cwd)).toHaveProperty("version", "100.0.0");
});
it("allows choice of package location", async () => {
const cwd = await initRemoteFixture("custom-location");
await lernaCreate(cwd)("custom-pkg", "modules");
const result = await listUntracked(cwd);
expect(result).toContain("modules/custom-pkg/package.json");
});
it("adds local dependencies", async () => {
const cwd = await initRemoteFixture("independent");
await lernaCreate(cwd)("foo-pkg", "--dependencies", "sibling-pkg");
expect(await manifestCreated(cwd)).toHaveProperty("dependencies", {
"sibling-pkg": "^2.0.0",
});
});
it("adds local dependency as relative file specifier when others exist", async () => {
const cwd = await initRemoteFixture("relative-file-spec");
await lernaCreate(cwd)("foo-pkg", "--dependencies", "sibling-pkg");
expect(await manifestCreated(cwd)).toHaveProperty("dependencies", {
"sibling-pkg": "file:../sibling-pkg",
});
});
it("reuses existing external dependency version", async () => {
const cwd = await initRemoteFixture("independent");
await lernaCreate(cwd)("foo-pkg", "--dependencies", "pify");
expect(await manifestCreated(cwd)).toHaveProperty("dependencies", {
pify: "^2.3.0",
});
});
it("supports external dependency version specifier", async () => {
const cwd = await initRemoteFixture("independent");
await lernaCreate(cwd)("foo-pkg", "--dependencies", "bar@1.0.0", "baz@^2.0.0", "qux@~3.0.0");
expect(await manifestCreated(cwd)).toHaveProperty("dependencies", {
bar: "1.0.0",
baz: "^2.0.0",
qux: "~3.0.0",
});
});
it("respects npm_config_save_exact", async () => {
const cwd = await initRemoteFixture("independent");
process.env.npm_config_save_exact = "true";
await lernaCreate(cwd)("foo-pkg", "--dependencies", "sibling-pkg");
delete process.env.npm_config_save_exact;
expect(await manifestCreated(cwd)).toHaveProperty("dependencies", {
"sibling-pkg": "2.0.0",
});
});
it("defaults homepage to a subpath of root homepage when it exists", async () => {
const cwd = await initRemoteFixture("independent");
const rootManifest = path.join(cwd, "package.json");
const json = await fs.readJSON(rootManifest);
json.homepage = "https://github.com/test/test";
await fs.writeJSON(rootManifest, json);
await lernaCreate(cwd)("foo-pkg");
expect(await manifestCreated(cwd)).toHaveProperty(
"homepage",
"https://github.com/test/test/tree/main/packages/foo-pkg#readme"
);
});
it("appends to pathname of non-github root homepage", async () => {
const cwd = await initRemoteFixture("independent");
const rootManifest = path.join(cwd, "package.json");
const json = await fs.readJSON(rootManifest);
json.homepage = "https://bitbucket.com/test/test";
await fs.writeJSON(rootManifest, json);
await lernaCreate(cwd)("foo-pkg");
expect(await manifestCreated(cwd)).toHaveProperty(
"homepage",
// no doubt wrong, but just illustrative of condition
"https://bitbucket.com/test/test/packages/foo-pkg"
);
});
it("does not mutate explicit --homepage pathname", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("foo-pkg", "--homepage", "http://google.com/");
expect(await manifestCreated(cwd)).toHaveProperty("homepage", "http://google.com/");
});
it("defaults schemeless homepage to http://", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("foo-pkg", "--homepage", "google.com");
expect(await manifestCreated(cwd)).toHaveProperty("homepage", "http://google.com/");
});
it("overrides default publishConfig.access with --access=restricted", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("@foo/pkg", "--access", "restricted");
expect(await manifestCreated(cwd)).toHaveProperty("publishConfig", {
access: "restricted",
});
});
it("sets non-public publishConfig.registry with --registry", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("@foo/pkg", "--registry", "http://my-private-registry.com/");
expect(await manifestCreated(cwd)).toHaveProperty("publishConfig", {
registry: "http://my-private-registry.com/",
});
});
it("sets publishConfig.tag with --tag", async () => {
const cwd = await initRemoteFixture("basic");
await lernaCreate(cwd)("@foo/pkg", "--tag", "next");
expect(await manifestCreated(cwd)).toHaveProperty("publishConfig", {
access: "public",
tag: "next",
});
});
it("skips repository field when git remote is missing", async () => {
const cwd = await initFixture("basic");
await lernaCreate(cwd)("a-pkg");
expect(await manifestCreated(cwd)).not.toHaveProperty("repository");
});
it("adds type field when using esModule", async () => {
const cwd = await initFixture("basic");
await lernaCreate(cwd)("a-pkg", "--es-module");
expect(await manifestCreated(cwd)).toHaveProperty("type", "module");
});
it("skips type field when not using esModule", async () => {
const cwd = await initFixture("basic");
await lernaCreate(cwd)("a-pkg");
expect(await manifestCreated(cwd)).not.toHaveProperty("type");
});
});