-
Notifications
You must be signed in to change notification settings - Fork 494
/
index.test.ts
546 lines (461 loc) 路 18.9 KB
/
index.test.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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import path from "path";
import fixtures from "fixturez";
import spawn from "spawndamnit";
import fileUrl from "file-url";
import {
getCommitsThatAddFiles,
getChangedFilesSince,
add,
commit,
tag,
getDivergedCommit,
getChangedPackagesSinceRef,
getChangedChangesetFilesSinceRef,
getAllTags,
tagExists,
} from "./";
const f = fixtures(__dirname);
async function getCurrentCommit(cwd: string) {
const cmd = await spawn("git", ["rev-parse", "HEAD"], { cwd });
return cmd.stdout.toString().trim();
}
async function getCurrentCommitShort(cwd: string) {
const cmd = await spawn("git", ["rev-parse", "--short", "HEAD"], { cwd });
return cmd.stdout.toString().trim();
}
async function getCommitCount(cwd: string) {
const cmd = await spawn("git", ["rev-list", "--count", "HEAD"], { cwd });
return parseInt(cmd.stdout.toString(), 10);
}
describe("git", () => {
let cwd: string;
beforeEach(async () => {
cwd = f.copy("with-git");
await spawn("git", ["init"], { cwd });
// so that this works regardless of what the default branch of git init is and for git versions that don't support --initial-branch(like our CI)
{
const { stdout } = await spawn(
"git",
["rev-parse", "--abbrev-ref", "HEAD"],
{ cwd }
);
if (stdout.toString("utf8").trim() !== "main") {
await spawn("git", ["checkout", "-b", "main"], { cwd });
}
}
await spawn("git", ["config", "user.email", "x@y.z"], { cwd });
await spawn("git", ["config", "user.name", "xyz"], { cwd });
await spawn("git", ["config", "commit.gpgSign", "false"], { cwd });
await spawn("git", ["config", "tag.gpgSign", "false"], { cwd });
await spawn("git", ["config", "tag.forceSignAnnotataled", "false"], {
cwd,
});
});
describe("getDivergedCommit", () => {
it("should return same commit when branches have not diverged", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
const firstSha = await getCurrentCommit(cwd);
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB package.json", cwd);
const secondSha = await getCurrentCommit(cwd);
const divergedSha = await getDivergedCommit(cwd, "main");
expect(firstSha).not.toBe(secondSha);
expect(divergedSha).toBe(secondSha);
});
it("should find commit where branch diverged", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
// This is the first commit. We branch (diverge) from here.
const mainSha = await getCurrentCommit(cwd);
// Create a new branch, and add a commit to it.
await spawn("git", ["checkout", "-b", "my-branch"], { cwd });
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB package.json", cwd);
// Now, get the latest commit from our new branch.
const branchSha = await getCurrentCommit(cwd);
// Finally, get the divergent commit.
const divergedSha = await getDivergedCommit(cwd, "main");
expect(mainSha).not.toBe(branchSha);
expect(divergedSha).toBe(mainSha);
});
});
describe("add", () => {
it("should add a file to the staging area", async () => {
await add("packages/pkg-a/package.json", cwd);
const gitCmd = await spawn("git", ["diff", "--name-only", "--cached"], {
cwd,
});
const stagedFiles = gitCmd.stdout
.toString()
.split("\n")
.filter((a) => a);
expect(stagedFiles).toHaveLength(1);
expect(stagedFiles[0]).toEqual("packages/pkg-a/package.json");
});
it("should add multiple files to the staging area", async () => {
await add("package.json", cwd);
await add("packages/pkg-a/package.json", cwd);
await add("packages/pkg-b/package.json", cwd);
const gitCmd = await spawn("git", ["diff", "--name-only", "--cached"], {
cwd,
});
const stagedFiles = gitCmd.stdout
.toString()
.split("\n")
.filter((a) => a);
expect(stagedFiles).toHaveLength(3);
expect(stagedFiles[0]).toEqual("package.json");
expect(stagedFiles[1]).toEqual("packages/pkg-a/package.json");
expect(stagedFiles[2]).toEqual("packages/pkg-b/package.json");
});
it("should add a directory", async () => {
await add("packages", cwd);
const gitCmd = await spawn("git", ["diff", "--name-only", "--cached"], {
cwd,
});
const stagedFiles = gitCmd.stdout
.toString()
.split("\n")
.filter((a) => a);
expect(stagedFiles).toEqual([
"packages/package.json",
"packages/pkg-a/index.js",
"packages/pkg-a/package.json",
"packages/pkg-b/index.js",
"packages/pkg-b/package.json",
]);
});
});
describe("commit", () => {
it("should commit a file", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
const gitCmd = await spawn("git", ["log", "-1", "--pretty=%B"], {
cwd,
});
const commitMessage = gitCmd.stdout.toString().trim();
expect(commitMessage).toEqual("added packageA package.json");
});
});
describe("getAllTags", () => {
beforeEach(async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
});
it("should retrieve all git tags", async () => {
await tag("test_tag", cwd);
await tag("test_tag2", cwd);
const tags = await getAllTags(cwd);
expect(tags).toContain("test_tag");
expect(tags).toContain("test_tag2");
});
});
describe("tag", () => {
beforeEach(async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
});
it("should create a tag for the current head", async () => {
const head = await spawn("git", ["rev-parse", "HEAD"], { cwd });
await tag("tag_message", cwd);
// Gets the hash of the commit the tag is referring to, not the hash of the tag itself
const tagRef = await spawn(
"git",
["rev-list", "-n", "1", "tag_message"],
{ cwd }
);
expect(tagRef).toEqual(head);
});
it("should create a tag, make a new commit, then create a second tag", async () => {
const initialHead = await spawn("git", ["rev-parse", "HEAD"], {
cwd,
});
await tag("tag_message", cwd);
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB package.json", cwd);
const newHead = await spawn("git", ["rev-parse", "HEAD"], { cwd });
await tag("new_tag", cwd);
// Gets the hash of the commit the tag is referring to, not the hash of the tag itself
const firstTagRef = await spawn(
"git",
["rev-list", "-n", "1", "tag_message"],
{ cwd }
);
const secondTagRef = await spawn(
"git",
["rev-list", "-n", "1", "new_tag"],
{ cwd }
);
expect(firstTagRef).toEqual(initialHead);
expect(secondTagRef).toEqual(newHead);
});
});
describe("tagExists", () => {
it("returns false when no tag exists", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
expect(await tagExists("tag_which_doesn't_exist", cwd)).toBe(false);
});
it("returns true when tag exists", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
await tag("tag_message", cwd);
expect(await tagExists("tag_message", cwd)).toBe(true);
});
});
describe("getCommitsThatAddFiles", () => {
it("should commit a file and get the hash of that commit", async () => {
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB package.json", cwd);
const headSha = await getCurrentCommitShort(cwd);
const commitHash = await getCommitsThatAddFiles(
["packages/pkg-b/package.json"],
cwd
);
expect(commitHash).toEqual([headSha]);
});
describe("with shallow clone", () => {
// We will add these three well-known files
// over multiple commits, then test looking up
// the commits at which they were added.
const file1 = ".changeset/quick-lions-devour.md";
const file2 = "packages/pkg-a/index.js";
const file3 = "packages/pkg-b/index.js";
// Roughly how many commits will the deepening algorithm
// deepen each time? We use this to set up test data to
// check that the deepens the clone but doesn't need to *fully* unshallow
// the clone.
const shallowCloneDeepeningAmount = 50;
/**
* Creates a number of empty commits; this is useful to ensure
* that a particular commit doesn't make it into a shallow clone.
*/
async function createDummyCommits(count: number) {
for (let i = 0; i < count; i++) {
await commit("dummy commit", cwd);
}
}
async function addFileAndCommit(file: string) {
await add(file, cwd);
await commit(`add file ${file}`, cwd);
const commitSha = await getCurrentCommitShort(cwd);
return commitSha;
}
async function createShallowClone(depth: number): Promise<string> {
// Make a 1-commit-deep shallow clone of this repo
const cloneDir = f.temp();
await spawn(
"git",
// Note: a file:// URL is needed in order to make a shallow clone of
// a local repo
["clone", "--depth", depth.toString(), fileUrl(cwd), "."],
{
cwd: cloneDir,
}
);
return cloneDir;
}
it("reads the SHA of a file-add without deepening if commit already included in the shallow clone", async () => {
// We create a repo that we shallow-clone;
// the commit we're going to scan for is the latest commit,
// so will be in the shallow clone immediately without deepening
await createDummyCommits(10);
const originalCommit = await addFileAndCommit(file1);
const clone = await createShallowClone(5);
// This file was added in the head commit, so will definitely be in our
// 1-commit clone.
const commits = await getCommitsThatAddFiles([file1], clone);
expect(commits).toEqual([originalCommit]);
// We should not need to have deepened the clone for this
expect(await getCommitCount(clone)).toEqual(5);
});
it("reads the SHA of a file-add even if not already included in the shallow clone", async () => {
// We're going to create a repo where the commit we're looking for isn't
// in the shallow clone, so we'll need to deepen it to locate it.
await createDummyCommits((shallowCloneDeepeningAmount * 2) / 3);
const originalCommit = await addFileAndCommit(file2);
await createDummyCommits((shallowCloneDeepeningAmount * 2) / 3);
const clone = await createShallowClone(5);
// Finding this commit will require deepening the clone until it appears.
const commit = (await getCommitsThatAddFiles([file2], clone))[0];
expect(commit).toEqual(originalCommit);
// It should not have completely unshallowed the clone; just enough.
const originalRepoDepth = await getCommitCount(cwd);
expect(await getCommitCount(clone)).toBeGreaterThan(5);
expect(await getCommitCount(clone)).toBeLessThan(originalRepoDepth);
});
it("reads the SHA of a file-add even if the first commit of a repo", async () => {
// Finding this commit will require deepening the clone right to the start
// of the repo history, and coping with a commit that has no parent.
const originalCommit = await addFileAndCommit(file3);
await createDummyCommits(shallowCloneDeepeningAmount * 2);
const clone = await createShallowClone(5);
// Finding this commit will require fully deepening the repo
const commit = (await getCommitsThatAddFiles([file3], clone))[0];
expect(commit).toEqual(originalCommit);
// We should have fully deepened
const originalRepoDepth = await getCommitCount(cwd);
expect(await getCommitCount(clone)).toEqual(originalRepoDepth);
});
it("can return SHAs for multiple files including return blanks for missing files", async () => {
// We want to ensure that we can retrieve SHAs for multiple files at the same time,
// and also that requesting missing files doesn't affect the location of commits
// for the files that succeed.
await createDummyCommits(shallowCloneDeepeningAmount);
const originalCommit1 = await addFileAndCommit(file1);
await createDummyCommits(shallowCloneDeepeningAmount);
const originalCommit2 = await addFileAndCommit(file2);
const nonExistentFile = "this-file-does-not-exist";
const clone = await createShallowClone(5);
const commits = await getCommitsThatAddFiles(
[file1, nonExistentFile, file2],
clone
);
expect(commits).toEqual([originalCommit1, undefined, originalCommit2]);
});
});
});
describe("getChangedFilesSince", () => {
beforeEach(async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
});
it("should be empty if no changes (partial path)", async () => {
const head = await spawn("git", ["rev-parse", "HEAD"], { cwd });
const changedFiles = await getChangedFilesSince({
ref: head.stdout.toString().trim(),
cwd,
fullPath: false,
});
expect(changedFiles).toHaveLength(0);
});
it("should be empty if no changes (full path)", async () => {
const head = await spawn("git", ["rev-parse", "HEAD"], { cwd });
const changedFiles = await getChangedFilesSince({
ref: head.stdout.toString().trim(),
cwd,
fullPath: true,
});
expect(changedFiles).toHaveLength(0);
});
it("should get list of files that have been committed", async () => {
const firstRef = await spawn("git", ["rev-parse", "HEAD"], { cwd });
await add("packages/pkg-a/index.js", cwd);
await commit("added packageA index", cwd);
const secondRef = await spawn("git", ["rev-parse", "HEAD"], { cwd });
await add("packages/pkg-b/index.js", cwd);
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB files", cwd);
const filesChangedSinceFirstRef = await getChangedFilesSince({
ref: firstRef.stdout.toString().trim(),
cwd,
});
expect(filesChangedSinceFirstRef[0]).toEqual("packages/pkg-a/index.js");
expect(filesChangedSinceFirstRef[1]).toEqual("packages/pkg-b/index.js");
expect(filesChangedSinceFirstRef[2]).toEqual(
"packages/pkg-b/package.json"
);
const filesChangedSinceSecondRef = await getChangedFilesSince({
ref: secondRef.stdout.toString().trim(),
cwd,
});
expect(filesChangedSinceSecondRef[0]).toEqual("packages/pkg-b/index.js");
expect(filesChangedSinceSecondRef[1]).toEqual(
"packages/pkg-b/package.json"
);
});
it("should get correct full paths of changed files irrespective of cwd", async () => {
const ref = await spawn("git", ["rev-parse", "HEAD"], { cwd });
await add("packages/pkg-a/index.js", cwd);
await commit("Add packageA index", cwd);
await add("packages/pkg-b/index.js", cwd);
await commit("Added packageB index", cwd);
const filesChangedSinceRef = await getChangedFilesSince({
ref: ref.stdout.toString().trim(),
cwd,
fullPath: true,
});
expect(filesChangedSinceRef[0]).toBe(
path.resolve(cwd, "packages/pkg-a/index.js")
);
expect(filesChangedSinceRef[1]).toBe(
path.resolve(cwd, "packages/pkg-b/index.js")
);
const filesChangedSinceRef2 = await getChangedFilesSince({
ref: ref.stdout.toString().trim(),
cwd: path.resolve(cwd, "packages"),
fullPath: true,
});
expect(filesChangedSinceRef2[0]).toBe(
path.resolve(cwd, "packages/pkg-a/index.js")
);
expect(filesChangedSinceRef2[1]).toBe(
path.resolve(cwd, "packages/pkg-b/index.js")
);
});
});
describe("getChangedPackagesSinceRef", () => {
beforeEach(async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
});
it("should return an empty list if no packages have changed", async () => {
await spawn("git", ["checkout", "-b", "new-branch"], { cwd });
const changedPackages = await getChangedPackagesSinceRef({
cwd,
ref: "main",
});
expect(changedPackages).toHaveLength(0);
});
it("should check changed packages on a branch against base branch", async () => {
await spawn("git", ["checkout", "-b", "new-branch"], { cwd });
await add("packages/pkg-a/index.js", cwd);
await commit("added packageA index", cwd);
await add("packages/pkg-b/index.js", cwd);
await add("packages/pkg-b/package.json", cwd);
await commit("added packageB files", cwd);
const changedPackages = await getChangedPackagesSinceRef({
cwd,
ref: "main",
});
expect(changedPackages).toHaveLength(2);
expect(changedPackages[0].packageJson.name).toEqual("pkg-a");
expect(changedPackages[1].packageJson.name).toEqual("pkg-b");
});
});
describe("getChangedChangesetFilesSinceRef", () => {
it("should be empty if no changeset files have been added", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
const files = await getChangedChangesetFilesSinceRef({
cwd,
ref: "main",
});
expect(files).toHaveLength(0);
});
it("should get the relative path to the changeset file", async () => {
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
await add(".changeset", cwd);
const files = await getChangedChangesetFilesSinceRef({
cwd,
ref: "main",
});
expect(files).toHaveLength(2);
expect(files[1]).toEqual(".changeset/quick-lions-devour.md");
});
it("should work on a ref that isn't the base branch", async () => {
await spawn("git", ["checkout", "-b", "some-branch"], { cwd });
await add("packages/pkg-a/package.json", cwd);
await commit("added packageA package.json", cwd);
await add(".changeset", cwd);
const files = await getChangedChangesetFilesSinceRef({
cwd,
ref: "some-branch",
});
expect(files).toHaveLength(2);
expect(files[1]).toEqual(".changeset/quick-lions-devour.md");
});
});
});