-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathaddOwnerAsAllContributor.test.ts
104 lines (85 loc) · 2.68 KB
/
addOwnerAsAllContributor.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
import { describe, expect, it, vi } from "vitest";
import { addOwnerAsAllContributor } from "./addOwnerAsAllContributor.js";
import { formatJson } from "./writing/creation/formatters/formatJson.js";
const mock$ = vi.fn();
vi.mock("execa", () => ({
get $() {
return mock$;
},
}));
const mockWriteFile = vi.fn();
vi.mock("node:fs/promises", () => ({
get writeFile() {
return mockWriteFile;
},
}));
const mockReadFileAsJson = vi.fn();
vi.mock("../shared/readFileAsJson.js", () => ({
get readFileAsJson() {
return mockReadFileAsJson;
},
}));
const mockOwner = "TestOwner";
vi.mock("../shared/getGitHubUserAsAllContributor", () => ({
getGitHubUserAsAllContributor: () => mockOwner,
}));
describe("addOwnerAsAllContributor", () => {
it("throws an error when the .all-contributorsrc fails to read", async () => {
mock$.mockResolvedValueOnce({
stdout: JSON.stringify({ login: "user" }),
});
mockReadFileAsJson.mockResolvedValue("invalid");
await expect(async () => {
await addOwnerAsAllContributor({ owner: mockOwner });
}).rejects.toMatchInlineSnapshot(
'[Error: Invalid .all-contributorsrc: "invalid"]',
);
});
it("throws an error when the .all-contributorsrc is missing expected properties", async () => {
mock$.mockResolvedValueOnce({
stdout: JSON.stringify({ login: "user" }),
});
mockReadFileAsJson.mockResolvedValue({});
await expect(async () => {
await addOwnerAsAllContributor({ owner: mockOwner });
}).rejects.toMatchInlineSnapshot(
"[Error: Invalid .all-contributorsrc: {}]",
);
});
it("sets the running user to tool when no prior contributions exist", async () => {
mock$.mockResolvedValueOnce({
stdout: JSON.stringify({ login: mockOwner }),
});
mockReadFileAsJson.mockResolvedValue({
contributors: [],
});
await addOwnerAsAllContributor({ owner: mockOwner });
expect(mockWriteFile).toHaveBeenCalledWith(
"./.all-contributorsrc",
await formatJson({
contributors: [{ contributions: ["tool"], login: mockOwner }],
}),
);
});
it("resets JoshuaKGoldberg to just tool and adds in the running user when both exist", async () => {
mock$.mockResolvedValueOnce({
stdout: JSON.stringify({ login: mockOwner }),
});
mockReadFileAsJson.mockResolvedValue({
contributors: [
{ contributions: ["bug", "fix"], login: mockOwner },
{ contributions: ["bug", "fix"], login: "JoshuaKGoldberg" },
],
});
await addOwnerAsAllContributor({ owner: mockOwner });
expect(mockWriteFile).toHaveBeenCalledWith(
"./.all-contributorsrc",
await formatJson({
contributors: [
{ contributions: ["bug", "fix", "tool"], login: mockOwner },
{ contributions: ["tool"], login: "JoshuaKGoldberg" },
],
}),
);
});
});