forked from lerna/lerna
/
import-command.test.js
364 lines (279 loc) · 14.4 KB
/
import-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
"use strict";
jest.mock("@lerna/prompt");
const execa = require("execa");
const fs = require("fs-extra");
const path = require("path");
const pathExists = require("path-exists");
// mocked or stubbed modules
const PromptUtilities = require("@lerna/prompt");
// helpers
const initNamedFixture = require("@lerna-test/init-named-fixture")(__dirname);
const initFixture = require("@lerna-test/init-fixture")(__dirname);
const gitAdd = require("@lerna-test/git-add");
const gitCommit = require("@lerna-test/git-commit");
const updateLernaConfig = require("@lerna-test/update-lerna-config");
// file under test
const lernaImport = require("@lerna-test/command-runner")(require("../command"));
// assertion helpers
const lastCommitInDir = cwd => execa.stdout("git", ["log", "-1", "--format=%s"], { cwd });
describe("ImportCommand", () => {
PromptUtilities.confirm.mockResolvedValue(true);
describe("import", () => {
const initBasicFixtures = () =>
Promise.all([initFixture("basic"), initFixture("external", "Init external commit")]);
it("creates a module in packages location with imported commit history", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const packageJson = path.join(testDir, "packages", path.basename(externalDir), "package.json");
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(packageJson)).toBe(true);
});
it("imports a repo with conflicted merge commits when run with --flatten", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const branchName = "conflict_branch";
const conflictedFileName = "conflicted-file.txt";
const conflictedFile = path.join(externalDir, conflictedFileName);
const newFilePath = path.join(testDir, "packages", path.basename(externalDir), conflictedFileName);
await fs.writeFile(conflictedFile, "initial content");
await gitAdd(externalDir, conflictedFileName);
await gitCommit(externalDir, "Initial content written");
await execa("git", ["checkout", "-b", branchName], { cwd: externalDir });
await fs.writeFile(conflictedFile, "branch content");
await gitAdd(externalDir, conflictedFileName);
await gitCommit(externalDir, "branch content written");
await execa("git", ["checkout", "master"], { cwd: externalDir });
await fs.writeFile(conflictedFile, "master content");
await gitAdd(externalDir, conflictedFileName);
await gitCommit(externalDir, "master content written");
try {
await execa("git", ["merge", branchName], { cwd: externalDir });
} catch (e) {
// skip
}
await fs.writeFile(conflictedFile, "merged content");
await gitAdd(externalDir, conflictedFileName);
await gitCommit(externalDir, "Branch merged");
await lernaImport(testDir)(externalDir, "--flatten");
expect(await lastCommitInDir(testDir)).toBe("Branch merged");
expect(await pathExists(newFilePath)).toBe(true);
});
it("imports a repo into the root directory when packages are located there", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("root-packages"),
initNamedFixture("myapp-foo", "external", "myapp-foo init commit"),
]);
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(testDir)).toBe("myapp-foo init commit");
expect(await pathExists(path.join(testDir, "myapp-foo/old-file"))).toBe(true);
expect(await pathExists(path.join(testDir, "myapp-foo/package.json"))).toBe(true);
});
it("supports moved files within the external repo", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const newFilePath = path.join(testDir, "packages", path.basename(externalDir), "new-file");
await execa("git", ["mv", "old-file", "new-file"], { cwd: externalDir });
await gitCommit(externalDir, "Moved old-file to new-file");
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(testDir)).toBe("Moved old-file to new-file");
expect(await pathExists(newFilePath)).toBe(true);
});
it("supports filepaths that have spaces within the external repo", async () =>
Promise.all(
// running the same test with and without --flatten
[true, false].map(async shouldFlatten => {
const [testDir, externalDir] = await Promise.all([
initFixture("basic"),
initFixture("files-with-spaces", "Init external commit"),
]);
const newPackagePath = path.join(testDir, "packages", path.basename(externalDir));
const newFilePath = path.join(newPackagePath, "file with spaces");
const newDeepFilePath = path.resolve(newPackagePath, "subfolder b/file");
if (shouldFlatten) {
await lernaImport(testDir)(externalDir, "--flatten");
} else {
await lernaImport(testDir)(externalDir);
}
expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(newFilePath)).toBe(true);
expect(await pathExists(newDeepFilePath)).toBe(true);
})
));
it("supports filepaths that have non-ascii char within the external repo", async () =>
Promise.all(
// running the same test with and without --flatten
[true, false].map(async shouldFlatten => {
const [testDir, externalDir] = await Promise.all([
initFixture("basic"),
initFixture("files-with-non-ascii-char", "Init external commit"),
]);
const newPackagePath = path.join(testDir, "packages", path.basename(externalDir));
await fs.copy(path.join(externalDir, "檔案"), path.join(externalDir, "檔案-copy"));
await gitAdd(externalDir, "檔案-copy");
await gitCommit(externalDir, "copy");
await fs.move(path.join(externalDir, "檔案"), path.join(externalDir, "檔案-rename"));
await gitAdd(externalDir, "檔案", "檔案-rename");
await gitCommit(externalDir, "rename");
const copyFilePath = path.join(newPackagePath, "檔案-copy");
const renameFilePath = path.join(newPackagePath, "檔案-rename");
if (shouldFlatten) {
await lernaImport(testDir)(externalDir, "--flatten");
} else {
await lernaImport(testDir)(externalDir);
}
expect(await lastCommitInDir(testDir)).toBe("rename");
expect(await pathExists(copyFilePath)).toBe(true);
expect(await pathExists(renameFilePath)).toBe(true);
})
));
it("skips empty patches with --flatten", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const filePath = path.join(externalDir, "file.txt");
await fs.writeFile(filePath, "non-empty content");
await gitAdd(externalDir, filePath);
await gitCommit(externalDir, "Non-empty commit");
await execa("git", ["commit", "--allow-empty", "-m", "Empty commit"], { cwd: externalDir });
await lernaImport(testDir)(externalDir, "--flatten");
expect(await lastCommitInDir(testDir)).toBe("Non-empty commit");
});
it("exits early when confirmation is rejected", async () => {
const [testDir, externalDir] = await initBasicFixtures();
PromptUtilities.confirm.mockResolvedValueOnce(false);
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(testDir)).toBe("Init commit");
});
it("preserves original committer and date with --preserve-commit", async () =>
Promise.all(
// running the same test with and without --preserve-commit
[true, false].map(async shouldPreserve => {
const [testDir, externalDir] = await initBasicFixtures();
const filePath = path.join(externalDir, "old-file");
let expectedEmail;
let expectedName;
await execa("git", ["config", "user.name", "'test-name'"], { cwd: externalDir });
await execa("git", ["config", "user.email", "'test-email@foo.bar'"], { cwd: externalDir });
await fs.writeFile(filePath, "non-empty content");
await gitAdd(externalDir, filePath);
await gitCommit(externalDir, "Non-empty commit");
if (shouldPreserve) {
await lernaImport(testDir)(externalDir, "--preserve-commit");
// original committer
expectedEmail = "test-email@foo.bar";
expectedName = "test-name";
} else {
await lernaImport(testDir)(externalDir);
// whatever the current git user is
expectedEmail = execa.sync("git", ["config", "user.email"], { cwd: testDir }).stdout;
expectedName = execa.sync("git", ["config", "user.name"], { cwd: testDir }).stdout;
}
expect(execa.sync("git", ["log", "-1", "--format=%cn <%ce>"], { cwd: testDir }).stdout).toBe(
`${expectedName} <${expectedEmail}>`
);
})
));
it("allows skipping confirmation prompt", async () => {
const [testDir, externalDir] = await initBasicFixtures();
await lernaImport(testDir)(externalDir, "--yes");
expect(PromptUtilities.confirm).not.toHaveBeenCalled();
});
it("errors without an argument", async () => {
const [testDir] = await initBasicFixtures();
const command = lernaImport(testDir)();
await expect(command).rejects.toThrow("Not enough non-option arguments: got 0, need at least 1");
});
it("errors when external directory is missing", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const missing = `${externalDir}_invalidSuffix`;
const command = lernaImport(testDir)(missing);
await expect(command).rejects.toThrow(`No repository found at "${missing}"`);
});
it("errors when external package.json is missing", async () => {
const [testDir, externalDir] = await initBasicFixtures();
await fs.unlink(path.join(externalDir, "package.json"));
const command = lernaImport(testDir)(externalDir);
await expect(command).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining("package.json"),
code: "MODULE_NOT_FOUND",
})
);
});
it("errors when external package.json has no name property", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const packageJson = path.join(externalDir, "package.json");
await fs.writeFile(packageJson, "{}");
const command = lernaImport(testDir)(externalDir);
await expect(command).rejects.toThrow(`No package name specified in "${packageJson}"`);
});
it("errors if target directory exists", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const targetDir = path.join(testDir, "packages", path.basename(externalDir));
const relativePath = path.relative(testDir, targetDir);
await fs.ensureDir(targetDir);
const command = lernaImport(testDir)(externalDir);
await expect(command).rejects.toThrow(`Target directory already exists "${relativePath}"`);
});
it("infers correct target directory given packages glob", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const targetDir = path.join(testDir, "pkg", path.basename(externalDir));
const relativePath = path.relative(testDir, targetDir);
await fs.ensureDir(targetDir);
await updateLernaConfig(testDir, {
packages: ["pkg/*"],
});
const command = lernaImport(testDir)(externalDir);
await expect(command).rejects.toThrow(`Target directory already exists "${relativePath}"`);
});
it("errors if repo has uncommitted changes", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const uncommittedFile = path.join(testDir, "uncommittedFile");
await fs.writeFile(uncommittedFile, "stuff");
await gitAdd(testDir, uncommittedFile);
const command = lernaImport(testDir)(externalDir);
await expect(command).rejects.toThrow("Local repository has un-committed changes");
});
it("does not remove custom subject prefixes in [brackets]", async () => {
const [testDir, externalDir] = await initBasicFixtures();
const newFilePath = path.join(testDir, "packages", path.basename(externalDir), "new-file");
await execa("git", ["mv", "old-file", "new-file"], { cwd: externalDir });
await gitCommit(externalDir, "[ISSUE-10] Moved old-file to new-file");
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(testDir)).toBe("[ISSUE-10] Moved old-file to new-file");
expect(await pathExists(newFilePath)).toBe(true);
});
});
describe("with non-root Lerna dir", () => {
// #1197
it("creates a module in packages location with imported commit history", async () => {
const [externalDir, rootDir] = await Promise.all([
initFixture("external", "Init external commit"),
initFixture("lerna-not-in-root"),
]);
const testDir = path.join(rootDir, "subdir");
const packageJson = path.join(testDir, "packages", path.basename(externalDir), "package.json");
await lernaImport(testDir)(externalDir);
expect(await lastCommitInDir(rootDir)).toBe("Init external commit");
expect(await pathExists(packageJson)).toBe(true);
});
});
describe("with multi-packages Lerna dir", () => {
it("creates a module in specified package directory", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("multi-packages"),
initFixture("external", "Init external commit"),
]);
const packageJson = path.join(testDir, "packages", path.basename(externalDir), "package.json");
await lernaImport(testDir)(externalDir, "--dest=packages");
expect(await lastCommitInDir(testDir)).toBe("Init external commit");
expect(await pathExists(packageJson)).toBe(true);
});
it("throws error when the package directory does not match with config", async () => {
const [testDir, externalDir] = await Promise.all([
initFixture("multi-packages"),
initFixture("external", "Init external commit"),
]);
const command = lernaImport(testDir)(externalDir, "--dest=components");
await expect(command).rejects.toThrow(
"--dest does not match with the package directories: core,packages"
);
});
});
});