-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgit.test.ts
More file actions
266 lines (231 loc) · 8.08 KB
/
Copy pathgit.test.ts
File metadata and controls
266 lines (231 loc) · 8.08 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
import fs from "node:fs/promises";
import path from "node:path";
import { createFixture } from "fs-fixture";
import { exec } from "tinyexec";
import { describe, expect, it } from "vitest";
import { getFileChanges } from "../src/git.ts";
async function setupGit(cwd: string) {
await exec("git", ["init"], { nodeOptions: { cwd } });
await fs.appendFile(
path.join(cwd, ".git/config"),
`\
[user]
email = x@y.z
name = xyz
[commit]
gpgSign = false
[tag]
gpgSign = false
forceSignAnnotated = false`,
"utf8",
);
await exec("git", ["add", "."], { nodeOptions: { cwd } });
await exec("git", ["commit", "-m", "initial commit", "--allow-empty"], {
nodeOptions: { cwd },
});
}
describe("getFileChanges", () => {
it("should get changes since a specific ref", async () => {
await using fixture = await createFixture({
".gitignore": ".env\nignored",
"a.txt": "Hello, world!",
"b.txt": "Hello, world!",
});
await setupGit(fixture.path);
await fixture.writeFile("a.txt", "This is an updated file!");
await fixture.rm("b.txt");
await fixture.writeFile("c.txt", "This is a new file!");
await fixture.mkdir("nested");
await fixture.writeFile("nested/file.txt", "This is a nested file");
await fixture.mkdir("ignored");
await fixture.writeFile("ignored/file.txt", "This file should be ignored");
await fixture.writeFile(".env", "This file should be ignored");
const result = await getFileChanges(fixture.path, "HEAD");
expect(result).toEqual({
additions: [
{
path: "a.txt",
contents: await fixture.readFile("a.txt", "base64"),
},
{
path: "c.txt",
contents: await fixture.readFile("c.txt", "base64"),
},
{
path: "nested/file.txt",
contents: await fixture.readFile("nested/file.txt", "base64"),
},
],
deletions: [{ path: "b.txt" }],
});
});
it("should support branch refs", async () => {
await using fixture = await createFixture({
"a.txt": "Hello, world!",
});
await setupGit(fixture.path);
await exec("git", ["checkout", "-b", "new-branch"], {
nodeOptions: { cwd: fixture.path },
});
await fixture.writeFile("b.txt", "This is a new file!");
const result = await getFileChanges(fixture.path, "refs/heads/new-branch");
expect(result).toEqual({
additions: [
{
path: "b.txt",
contents: await fixture.readFile("b.txt", "base64"),
},
],
deletions: [],
});
});
it("should support tag refs", async () => {
await using fixture = await createFixture({
"a.txt": "Hello, world!",
});
await setupGit(fixture.path);
await exec("git", ["tag", "v1.0.0"], {
nodeOptions: { cwd: fixture.path },
});
await fixture.writeFile("b.txt", "This is a new file!");
const result = await getFileChanges(fixture.path, "refs/tags/v1.0.0");
expect(result).toEqual({
additions: [
{
path: "b.txt",
contents: await fixture.readFile("b.txt", "base64"),
},
],
deletions: [],
});
});
it("should support commit refs", async () => {
await using fixture = await createFixture({
"a.txt": "Hello, world!",
});
await setupGit(fixture.path);
const commitSha = (
await exec("git", ["rev-parse", "HEAD"], {
nodeOptions: { cwd: fixture.path },
})
).stdout.trim();
await fixture.writeFile("b.txt", "This is a new file!");
const result = await getFileChanges(fixture.path, commitSha);
expect(result).toEqual({
additions: [
{
path: "b.txt",
contents: await fixture.readFile("b.txt", "base64"),
},
],
deletions: [],
});
});
it("should filter files with filterFiles", async () => {
await using fixture = await createFixture({
"foo.txt": "Hello, world!",
"nested/foo.txt": "Hello, world!",
});
await setupGit(fixture.path);
await fixture.rm("foo.txt");
await fixture.rm("nested/foo.txt");
await fixture.writeFile("bar.txt", "This is a new file!");
await fixture.writeFile("nested/bar.txt", "This is a new file!");
const result = await getFileChanges(
fixture.path,
"HEAD",
// Only include top-level files
(file) => !file.includes("/"),
);
expect(result).toEqual({
additions: [
{
path: "bar.txt",
contents: await fixture.readFile("bar.txt", "base64"),
},
],
deletions: [{ path: "foo.txt" }],
});
});
it("should allow existing symlinks", async () => {
await using fixture = await createFixture({
"foo.txt": "Hello, world!",
"bar.txt": "Hello, world!",
});
await setupGit(fixture.path);
await fixture.mkdir("some-dir");
await fs.symlink(
fixture.getPath("foo.txt"),
fixture.getPath("some-dir/nested"),
);
await exec("git", ["add", "."], { nodeOptions: { cwd: fixture.path } });
await exec("git", ["commit", "-m", "Add symlink"], {
nodeOptions: { cwd: fixture.path },
});
// Since we committed, HEAD points to the last commit and there's no change since then
const result = await getFileChanges(fixture.path, "HEAD");
expect(result).toEqual({ additions: [], deletions: [] });
await fixture.rm("some-dir/nested");
await fs.symlink(
fixture.getPath("bar.txt"),
fixture.getPath("some-dir/nested"),
);
// We made symlink changes since the last commit, so this should error now
await expect(getFileChanges(fixture.path, "HEAD")).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 not error when symlink is present but ignored", async () => {
await using fixture = await createFixture({
"foo.txt": "Hello, world!",
});
await setupGit(fixture.path);
await fixture.writeFile(".gitignore", "some-dir");
await exec("git", ["add", "."], { nodeOptions: { cwd: fixture.path } });
await exec("git", ["commit", "-m", "Add gitignore"], {
nodeOptions: { cwd: fixture.path },
});
await fixture.mkdir("some-dir");
await fs.symlink(
fixture.getPath("foo.txt"),
fixture.getPath("some-dir/nested"),
);
const result = await getFileChanges(fixture.path, "HEAD");
expect(result).toEqual({ additions: [], deletions: [] });
});
it("should throw error when symlink is present with non-existent path", async () => {
await using fixture = await createFixture();
await setupGit(fixture.path);
await fixture.mkdir("some-dir");
await fs.symlink(
fixture.getPath("non-existent"),
fixture.getPath("some-dir/nested"),
);
await expect(getFileChanges(fixture.path, "HEAD")).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 throw error when symlink is present with existing path", async () => {
await using fixture = await createFixture({
"foo.txt": "Hello, world!",
});
await setupGit(fixture.path);
await fixture.mkdir("some-dir");
await fs.symlink(
fixture.getPath("foo.txt"),
fixture.getPath("some-dir/nested"),
);
await expect(getFileChanges(fixture.path, "HEAD")).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 throw error when executable file is present", async () => {
await using fixture = await createFixture();
await setupGit(fixture.path);
await fixture.writeFile("executable-file.sh", "#!/bin/bash\necho hello");
await fs.chmod(fixture.getPath("executable-file.sh"), 0o755);
await expect(getFileChanges(fixture.path, "HEAD")).rejects.toThrow(
"Unexpected executable file at executable-file.sh, GitHub API only supports non-executable files and directories. You may need to add this file to .gitignore",
);
});
});