forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.test.ts
300 lines (249 loc) · 7.79 KB
/
git.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
import { updateStatus } from "@cloudflare/cli";
import { mockSpinner } from "helpers/__tests__/mocks";
import { processArgument } from "helpers/args";
import { runCommand } from "helpers/command";
import { beforeEach, describe, expect, test, vi } from "vitest";
import {
getProductionBranch,
gitCommit,
initializeGit,
isGitConfigured,
isGitInstalled,
isInsideGitRepo,
offerGit,
} from "../git";
import { createTestContext } from "./helpers";
vi.mock("helpers/command");
vi.mock("helpers/args");
vi.mock("@cloudflare/cli/interactive");
vi.mock("@cloudflare/cli");
beforeEach(() => {
mockSpinner();
});
const mockGitInstalled = (isInstalled = true) => {
if (isInstalled) {
vi.mocked(runCommand).mockResolvedValueOnce(
"git version 2.20.2 (Apple Git-100)",
);
} else {
vi.mocked(runCommand).mockRejectedValueOnce(
new Error("zsh: command not found: git"),
);
}
};
const mockGitConfig = () => {
vi.mocked(runCommand).mockResolvedValueOnce("test user");
vi.mocked(runCommand).mockResolvedValueOnce("[email protected]");
};
const mockInsideGitRepo = (isInside: boolean) => {
if (isInside) {
vi.mocked(runCommand).mockResolvedValueOnce(
"On branch master\nnothing to commit, working tree clean",
);
} else {
vi.mocked(runCommand).mockRejectedValueOnce(
new Error(
"fatal: not a git repository (or any of the parent directories): .git",
),
);
}
};
const mockDefaultBranchName = (branch = "main") => {
vi.mocked(runCommand).mockResolvedValueOnce(branch);
};
describe("git helpers", () => {
describe("isGitConfigured", () => {
test("fully configured", async () => {
vi.mocked(runCommand).mockImplementation((cmd) =>
Promise.resolve(cmd.includes("email") ? "[email protected]" : "test user"),
);
await expect(isGitConfigured()).resolves.toBe(true);
});
test("no name", async () => {
vi.mocked(runCommand).mockImplementation((cmd) =>
Promise.resolve(cmd.includes("email") ? "[email protected]" : ""),
);
await expect(isGitConfigured()).resolves.toBe(false);
});
test("no email", async () => {
vi.mocked(runCommand).mockImplementation((cmd) =>
Promise.resolve(cmd.includes("name") ? "test user" : ""),
);
await expect(isGitConfigured()).resolves.toBe(false);
});
test("runCommand fails", async () => {
vi.mocked(runCommand).mockRejectedValue(new Error("git not found"));
await expect(isGitConfigured()).resolves.toBe(false);
});
});
describe("isGitInstalled", async () => {
test("installed", async () => {
mockGitInstalled(true);
await expect(isGitInstalled()).resolves.toBe(true);
});
test("not installed", async () => {
mockGitInstalled(false);
await expect(isGitInstalled()).resolves.toBe(false);
});
});
describe("isInsideGitRepo", async () => {
test("inside git repo", async () => {
mockInsideGitRepo(true);
await expect(isInsideGitRepo("")).resolves.toBe(true);
});
test("is not inside git repo", async () => {
mockInsideGitRepo(false);
await expect(isInsideGitRepo(".")).resolves.toBe(false);
});
});
describe("getProductionBranch", async () => {
test("happy path", async () => {
vi.mocked(runCommand).mockResolvedValueOnce("production");
await expect(getProductionBranch(".")).resolves.toBe("production");
});
test("error", async () => {
vi.mocked(runCommand).mockRejectedValueOnce(new Error());
await expect(getProductionBranch(".")).resolves.toBe("main");
});
});
describe("initializeGit", async () => {
test("happy path", async () => {
mockDefaultBranchName("production");
await initializeGit(".");
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["git", "init", "--initial-branch", "production"],
expect.any(Object),
);
});
test("error - fallback to default", async () => {
vi.mocked(runCommand).mockRejectedValueOnce(new Error());
await initializeGit(".");
expect(vi.mocked(runCommand)).toHaveBeenLastCalledWith(
["git", "init"],
expect.any(Object),
);
});
});
describe("offerGit", async () => {
test("happy path", async () => {
const ctx = createTestContext();
mockGitInstalled(true);
mockInsideGitRepo(false);
mockGitConfig();
mockDefaultBranchName();
// Mock user selecting true
vi.mocked(processArgument).mockResolvedValueOnce(true);
await offerGit(ctx);
expect(processArgument).toHaveBeenCalledOnce();
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["git", "init", "--initial-branch", "main"],
expect.any(Object),
);
expect(ctx.args.git).toBe(true);
});
test("git not installed", async () => {
const ctx = createTestContext();
mockGitInstalled(false);
await offerGit(ctx);
expect(updateStatus).toHaveBeenCalledWith(
expect.stringContaining("Continuing without git"),
);
expect(processArgument).not.toHaveBeenCalled();
expect(ctx.args.git).toBe(false);
});
test("git not configured", async () => {
const ctx = createTestContext();
mockGitInstalled(false);
vi.mocked(runCommand).mockRejectedValue(new Error("git not found"));
await offerGit(ctx);
expect(updateStatus).toHaveBeenCalledWith(
expect.stringContaining("Continuing without git"),
);
expect(processArgument).not.toHaveBeenCalled();
expect(ctx.args.git).toBe(false);
});
test("inside existing git repo", async () => {
const ctx = createTestContext("test", { projectName: "test" });
mockGitInstalled(true);
mockGitConfig();
mockInsideGitRepo(true);
await offerGit(ctx);
expect(processArgument).not.toHaveBeenCalledOnce();
expect(ctx.args.git).toBe(true);
});
test("user selects no git", async () => {
const ctx = createTestContext();
mockGitInstalled(true);
mockGitConfig();
mockInsideGitRepo(false);
mockDefaultBranchName();
// Mock user selecting true
vi.mocked(processArgument).mockResolvedValueOnce(false);
await offerGit(ctx);
expect(processArgument).toHaveBeenCalledOnce();
expect(vi.mocked(runCommand)).not.toHaveBeenCalledWith(
["git", "init", "--initial-branch", "main"],
expect.any(Object),
);
expect(ctx.args.git).toBe(false);
});
});
describe("gitCommit", async () => {
let spinner: ReturnType<typeof mockSpinner>;
beforeEach(() => {
spinner = mockSpinner();
// Mocks for `createCommitMessage`
mockGitInstalled(true);
mockInsideGitRepo(true);
});
test("happy path", async () => {
const ctx = createTestContext();
ctx.gitRepoAlreadyExisted = false;
mockGitInstalled(true);
mockInsideGitRepo(true);
await gitCommit(ctx);
expect(spinner.start).toHaveBeenCalledOnce();
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["git", "add", "."],
expect.any(Object),
);
expect(vi.mocked(runCommand)).toHaveBeenCalledWith(
["git", "commit", "-m", expect.any(String)],
expect.any(Object),
);
expect(spinner.stop).toHaveBeenCalledOnce();
});
test("git repo already existed (early exit)", async () => {
const ctx = createTestContext();
ctx.gitRepoAlreadyExisted = true;
await gitCommit(ctx);
expect(spinner.start).not.toHaveBeenCalled();
expect(spinner.stop).not.toHaveBeenCalled();
expect(vi.mocked(runCommand)).not.toHaveBeenCalledWith(
["git", "commit", "-m", expect.any(String)],
expect.any(Object),
);
});
const cases = [
[true, false],
[false, true],
[false, false],
];
test.each(cases)(
"early exit (git installed: %s, git initialized: %s)",
async (gitInstalled, gitInitialized) => {
const ctx = createTestContext();
ctx.gitRepoAlreadyExisted = true;
mockGitInstalled(gitInstalled);
mockInsideGitRepo(gitInitialized);
await gitCommit(ctx);
expect(spinner.start).not.toHaveBeenCalled();
expect(spinner.stop).not.toHaveBeenCalled();
expect(vi.mocked(runCommand)).not.toHaveBeenCalledWith(
["git", "commit", "-m", expect.any(String)],
expect.any(Object),
);
},
);
});
});