-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgit.test.ts
More file actions
410 lines (366 loc) · 11.1 KB
/
Copy pathgit.test.ts
File metadata and controls
410 lines (366 loc) · 11.1 KB
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
import fs from "fs";
import path from "path";
import {
ENV,
REPO,
ROOT_TEMP_DIRECTORY,
ROOT_TEST_BRANCH_PREFIX,
log,
} from "./env";
import { execFile } from "child_process";
import { getOctokit } from "@actions/github";
import { commitChangesFromRepo } from "../../git";
import { getRefTreeQuery } from "../../github/graphql/queries";
import { deleteBranches, waitForGitHubToBeReady } from "./util";
import git from "isomorphic-git";
const octokit = getOctokit(ENV.GITHUB_TOKEN);
const TEST_BRANCH_PREFIX = `${ROOT_TEST_BRANCH_PREFIX}-git`;
const expectBranchHasFile = async ({
branch,
path,
oid,
}: {
branch: string;
path: string;
oid: string | null;
}) => {
if (oid === null) {
expect(() =>
getRefTreeQuery(octokit, {
...REPO,
ref: `refs/heads/${branch}`,
path,
}),
).rejects.toThrow("Could not resolve file for path");
return;
}
const ref = (
await getRefTreeQuery(octokit, {
...REPO,
ref: `refs/heads/${branch}`,
path,
})
).repository?.ref?.target;
if (!ref) {
throw new Error("Unexpected missing ref");
}
if ("tree" in ref) {
expect(ref.file?.oid ?? null).toEqual(oid);
} else {
throw new Error("Expected ref to have a tree");
}
};
const expectParentHasOid = async ({
branch,
oid,
}: {
branch: string;
oid: string;
}) => {
const commit = (
await getRefTreeQuery(octokit, {
...REPO,
ref: `refs/heads/${branch}`,
path: "README.md",
})
).repository?.ref?.target;
if (!commit || !("parents" in commit)) {
throw new Error("Expected commit to have a parent");
}
expect(commit.parents.nodes).toEqual([{ oid }]);
};
const makeFileChanges = async (
repoDirectory: string,
changegroup:
| "standard"
| "with-ignored-symlink"
| "with-included-valid-symlink"
| "with-included-invalid-symlink",
) => {
// Update an existing file
await fs.promises.writeFile(
path.join(repoDirectory, "LICENSE"),
"This is a new license",
);
// Remove a file
await fs.promises.rm(path.join(repoDirectory, "package.json"));
// Remove a file nested in a directory
await fs.promises.rm(path.join(repoDirectory, "src", "index.ts"));
// Add a new file
await fs.promises.writeFile(
path.join(repoDirectory, "new-file.txt"),
"This is a new file",
);
// Add a new file nested in a directory
await fs.promises.mkdir(path.join(repoDirectory, "nested"), {
recursive: true,
});
await fs.promises.writeFile(
path.join(repoDirectory, "nested", "nested-file.txt"),
"This is a nested file",
);
// Add files that should be ignored
await fs.promises.writeFile(
path.join(repoDirectory, ".env"),
"This file should be ignored",
);
await fs.promises.mkdir(path.join(repoDirectory, "coverage", "foo"), {
recursive: true,
});
await fs.promises.writeFile(
path.join(repoDirectory, "coverage", "foo", "bar"),
"This file should be ignored",
);
if (changegroup === "with-ignored-symlink") {
// node_modules is ignored in this repo
await fs.promises.mkdir(path.join(repoDirectory, "node_modules"), {
recursive: true,
});
await fs.promises.symlink(
path.join(repoDirectory, "non-existent"),
path.join(repoDirectory, "node_modules", "nested"),
);
}
if (changegroup === "with-included-valid-symlink") {
await fs.promises.mkdir(path.join(repoDirectory, "some-dir"), {
recursive: true,
});
await fs.promises.symlink(
path.join(repoDirectory, "README.md"),
path.join(repoDirectory, "some-dir", "nested"),
);
}
if (changegroup === "with-included-invalid-symlink") {
await fs.promises.mkdir(path.join(repoDirectory, "some-dir"), {
recursive: true,
});
await fs.promises.symlink(
path.join(repoDirectory, "non-existent"),
path.join(repoDirectory, "some-dir", "nested"),
);
}
};
const makeFileChangeAssertions = async (branch: string) => {
// Expect the deleted files to not exist
await expectBranchHasFile({ branch, path: "package.json", oid: null });
await expectBranchHasFile({ branch, path: "src/index.ts", oid: null });
// Expect updated file to have new oid
await expectBranchHasFile({
branch,
path: "LICENSE",
oid: "8dd03bb8a1d83212f3667bd2eb8b92746120ab8f",
});
// Expect new files to have correct oid
await expectBranchHasFile({
branch,
path: "new-file.txt",
oid: "be5b944ff55ca7569cc2ae34c35b5bda8cd5d37e",
});
await expectBranchHasFile({
branch,
path: "nested/nested-file.txt",
oid: "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3",
});
// Expect ignored files to not exist
await expectBranchHasFile({ branch, path: ".env", oid: null });
await expectBranchHasFile({
branch,
path: "coverage/foo/bar",
oid: null,
});
};
describe("git", () => {
const branches: string[] = [];
// Set timeout to 1 minute
jest.setTimeout(60 * 1000);
describe("commitChangesFromRepo", () => {
const testDir = path.join(ROOT_TEMP_DIRECTORY, "commitChangesFromRepo");
for (const group of ["standard", "with-ignored-symlink"] as const) {
it(`should correctly commit all changes for group: ${group}`, async () => {
const branch = `${TEST_BRANCH_PREFIX}-multiple-changes-${group}`;
branches.push(branch);
await fs.promises.mkdir(testDir, { recursive: true });
const repoDirectory = path.join(testDir, `repo-1-${group}`);
// Clone the git repo locally using the git cli and child-process
await new Promise<void>((resolve, reject) => {
const p = execFile(
"git",
["clone", process.cwd(), `repo-1-${group}`],
{ cwd: testDir },
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
);
p.stdout?.pipe(process.stdout);
p.stderr?.pipe(process.stderr);
});
await makeFileChanges(repoDirectory, group);
// Push the changes
await commitChangesFromRepo({
octokit,
...REPO,
branch,
message: {
headline: "Test commit",
body: "This is a test commit",
},
repoDirectory,
log,
});
await waitForGitHubToBeReady();
await makeFileChangeAssertions(branch);
// Expect the OID to be the HEAD commit
const oid =
(
await git.log({
fs,
dir: repoDirectory,
ref: "HEAD",
depth: 1,
})
)[0]?.oid ?? "NO_OID";
await expectParentHasOid({ branch, oid });
});
}
describe(`should throw appropriate error when symlink is present`, () => {
it(`and file does not exist`, async () => {
const branch = `${TEST_BRANCH_PREFIX}-invalid-symlink-error`;
branches.push(branch);
await fs.promises.mkdir(testDir, { recursive: true });
const repoDirectory = path.join(testDir, `repo-invalid-symlink`);
// Clone the git repo locally using the git cli and child-process
await new Promise<void>((resolve, reject) => {
const p = execFile(
"git",
["clone", process.cwd(), `repo-invalid-symlink`],
{ cwd: testDir },
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
);
p.stdout?.pipe(process.stdout);
p.stderr?.pipe(process.stderr);
});
await makeFileChanges(repoDirectory, "with-included-invalid-symlink");
// Push the changes
await expect(() =>
commitChangesFromRepo({
octokit,
...REPO,
branch,
message: {
headline: "Test commit",
body: "This is a test commit",
},
repoDirectory,
log,
}),
).rejects.toThrow(
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
);
});
it(`and file exists`, async () => {
const branch = `${TEST_BRANCH_PREFIX}-valid-symlink-error`;
branches.push(branch);
await fs.promises.mkdir(testDir, { recursive: true });
const repoDirectory = path.join(testDir, `repo-valid-symlink`);
// Clone the git repo locally using the git cli and child-process
await new Promise<void>((resolve, reject) => {
const p = execFile(
"git",
["clone", process.cwd(), `repo-valid-symlink`],
{ cwd: testDir },
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
);
p.stdout?.pipe(process.stdout);
p.stderr?.pipe(process.stderr);
});
await makeFileChanges(repoDirectory, "with-included-valid-symlink");
// Push the changes
await expect(() =>
commitChangesFromRepo({
octokit,
...REPO,
branch,
message: {
headline: "Test commit",
body: "This is a test commit",
},
repoDirectory,
log,
}),
).rejects.toThrow(
"Unexpected symlink at some-dir/nested, GitHub API only supports files and directories. You may need to add this file to .gitignore",
);
});
});
it("should correctly be able to base changes off specific commit", async () => {
const branch = `${TEST_BRANCH_PREFIX}-specific-base`;
branches.push(branch);
await fs.promises.mkdir(testDir, { recursive: true });
const repoDirectory = path.join(testDir, "repo-2");
// Clone the git repo locally usig the git cli and child-process
await new Promise<void>((resolve, reject) => {
const p = execFile(
"git",
["clone", process.cwd(), "repo-2"],
{ cwd: testDir },
(error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
);
p.stdout?.pipe(process.stdout);
p.stderr?.pipe(process.stderr);
});
makeFileChanges(repoDirectory, "standard");
// Determine the previous commit hash
const gitLog = await git.log({
fs,
dir: repoDirectory,
ref: "HEAD",
depth: 2,
});
const oid = gitLog[1]?.oid ?? "";
// Push the changes
await commitChangesFromRepo({
octokit,
...REPO,
branch,
message: {
headline: "Test commit",
body: "This is a test commit",
},
repoDirectory,
log,
base: {
commit: oid,
},
});
await waitForGitHubToBeReady();
await makeFileChangeAssertions(branch);
await expectParentHasOid({ branch, oid });
});
});
afterAll(async () => {
console.info("Cleaning up test branches");
await deleteBranches(octokit, branches);
});
});