/
auto-in-pr-ci.test.ts
249 lines (208 loc) · 7.53 KB
/
auto-in-pr-ci.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
import { Auto } from "../auto";
import SEMVER from "../semver";
import { dummyLog } from "../utils/logger";
import makeCommitFromMsg from "./make-commit-from-msg";
import endent from "endent";
import execPromise from "../utils/exec-promise";
const exec = jest.fn();
jest.mock("../utils/exec-promise");
// @ts-ignore
execPromise.mockImplementation(exec);
exec.mockResolvedValue("");
jest.mock("env-ci", () => () => ({
pr: 123,
build: 1,
isCi: true,
isPr: true,
branch: "ci-test",
}));
const defaults = {
owner: "foo",
repo: "bar",
};
process.env.GH_TOKEN = "XXX";
jest.mock("@octokit/rest", () => {
const Octokit = class MockOctokit {
static plugin = () => Octokit;
authenticate = () => undefined;
repos = {
get: jest.fn().mockReturnValue({}),
};
issues = {
listLabelsOnIssue: jest.fn().mockReturnValue({ data: [] }),
};
hook = {
error: () => undefined,
};
};
return { Octokit };
});
describe("canary in ci", () => {
test("calls the canary hook with the canary version", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const canary = jest.fn();
auto.hooks.canary.tap("test", canary);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
await auto.canary();
expect(canary).toHaveBeenCalledWith(
expect.objectContaining({
bump: SEMVER.patch,
canaryIdentifier: "canary.123.1",
})
);
});
test("comments on PR in CI", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap("test", () => "1.2.4-canary.123.1");
const version = await auto.canary({ pr: 123, build: 1 });
expect(addToPrBody).toHaveBeenCalled();
expect(version!.newVersion).toBe("1.2.4-canary.123.1");
});
test("should fail when canaries not implemented", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
// @ts-ignore
jest.spyOn(process, "exit").mockImplementationOnce(() => {});
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
await auto.canary({ pr: 123, build: 1, message: "false" });
expect(process.exit).toHaveBeenCalled();
});
test('should not comment when passed "false"', async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap(
"test",
({ canaryIdentifier }) => `1.2.4-${canaryIdentifier}`
);
await auto.canary({ pr: 123, build: 1, message: "false" });
expect(addToPrBody).not.toHaveBeenCalled();
});
test("can override pr and build", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap(
"test",
({ canaryIdentifier }) => `1.2.4-${canaryIdentifier}`
);
const version = await auto.canary({ pr: 456, build: 5 });
expect(version!.newVersion).toBe("1.2.4-canary.456.5");
});
});
describe("shipit in ci", () => {
test("should publish canary in PR", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
jest.spyOn(auto.git!, "addToPrBody").mockImplementation();
auto.release!.getCommitsInRelease = () => Promise.resolve([]);
auto.release!.getCommits = () => Promise.resolve([]);
const canary = jest.fn();
auto.hooks.canary.tap("test", canary);
await auto.shipit();
expect(canary).toHaveBeenCalledWith(
expect.objectContaining({
bump: SEMVER.patch,
canaryIdentifier: "canary.123.1",
})
);
});
});
describe("next in ci", () => {
test("should post comment with new version", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
jest.spyOn(console, "log").mockImplementation();
exec.mockResolvedValue("v1.0.0");
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
const prBody = jest.fn();
// @ts-ignore
auto.prBody = prBody;
auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.publish = () => Promise.resolve({ data: {} } as any);
auto.git!.getLatestTagInBranch = () => Promise.resolve("1.2.3");
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.generateReleaseNotes = () => Promise.resolve("notes");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const afterRelease = jest.fn();
auto.hooks.afterRelease.tap("test", afterRelease);
auto.hooks.next.tap("test", () => ["1.2.4-next.0"]);
jest.spyOn(auto.release!, "getCommits").mockImplementation();
await auto.next({});
expect(prBody).toHaveBeenCalledWith({
context: "prerelease-version",
pr: 123,
message: endent`
# Version
Published prerelease version: \`1.2.4-next.0\`
<details>
<summary>Changelog</summary>
notes
</details>
`,
});
});
test("should use labels on next branch PR", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
auto.logger = dummyLog();
await auto.loadConfig();
// @ts-ignore
auto.inPrereleaseBranch = () => true;
// @ts-ignore
jest.spyOn(console, "log").mockImplementation();
auto.git!.getLatestTagInBranch = () => Promise.resolve("1.4.0-next.0");
auto.release!.getSemverBump = () => Promise.resolve(SEMVER.patch);
auto.git!.getLabels = () => Promise.resolve([]);
expect(await auto.getVersion()).toBe(SEMVER.prepatch);
auto.git!.getLabels = () => Promise.resolve([SEMVER.major]);
expect(await auto.getVersion()).toBe(SEMVER.premajor);
});
});