Repository navigation
Expand file tree
/
Copy pathcommitlintConfig.test.ts
More file actions
368 lines (330 loc) · 14.6 KB
/
Copy pathcommitlintConfig.test.ts
File metadata and controls
368 lines (330 loc) · 14.6 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
// SPDX-FileCopyrightText: 2026 Anton Wiklund and Solid Memo contributors
//
// SPDX-License-Identifier: MIT
import { readFileSync } from "node:fs";
import { join } from "node:path";
import lint from "@commitlint/lint";
import load from "@commitlint/load";
import { describe, expect, it } from "vitest";
/**
* commitlint.config.mjs, run as CI's commits job and `npm run lint:commits`
* run it, on messages like this project's (CONTRIBUTING.md#commit-messages):
* every commit says why and names its issue, and Renovate's updates keep
* every other rule.
*/
const ROOT = join(import.meta.dirname, "..");
const { RENOVATE_WHY, holdsChangelogLink } = (await import(
new URL("../commitlint.config.mjs", import.meta.url).href
)) as { RENOVATE_WHY: string; holdsChangelogLink(message: string): boolean };
const config = await load({}, { cwd: ROOT });
async function errors(message: string): Promise<string[]> {
const result = await lint(message, config.rules, {
parserOpts: config.parserPreset?.parserOpts as object,
plugins: config.plugins,
defaultIgnores: config.defaultIgnores,
ignores: config.ignores,
});
return result.errors.map(({ name }) => name);
}
const why = "A deck with a broken link leaves the learner nowhere.";
// 06aba864, as Renovate wrote it from commitBody in renovate.json5.
const renovateBody = [
"- vite 8.3.1 → 8.3.4 (patch, devDependencies) in package.json; source and release notes: https://github.com/vitejs/vite",
"",
"Why: renovate.json5 keeps every dependency current, within the versions it allows, so that fixes arrive while they are small; the update was merged only after CI passed on it.",
].join("\n");
describe("commitlint.config.mjs", () => {
it("passes a commit that says why and names its issue, in any form", async () => {
for (const footer of ["Refs: #72", "Closes: #87", "Closes #68", "Fixes #69", "Refs #69"]) {
expect(await errors(`feat(studio): check a draft's links\n\n${why}\n\n${footer}`)).toEqual(
[],
);
}
expect(await errors(`fix: keep the deck's title\n\n${why} Found in #12.`)).toEqual([]);
});
it("passes a security fix that names its advisory instead of an issue", async () => {
for (const footer of ["Refs: GHSA-fhp3-h74x-mhrr", "Fixes GHSA-2345-6789-cfgh"]) {
expect(
await errors(`fix(solid): keep a deck's documents its own\n\n${why}\n\n${footer}`),
).toEqual([]);
}
});
it("takes only a well-formed advisory id for one", async () => {
for (const footer of [
"Refs: GHSA-1234",
"Refs: GHSA-abcd-efgh-ijkl",
"Refs: XGHSA-fhp3-h74x-mhrr",
]) {
expect(await errors(`fix: keep the deck's title\n\n${why}\n\n${footer}`)).toEqual([
"references-or-renovate",
]);
}
});
it("fails a commit with no body", async () => {
expect(await errors("fix: keep the deck's title")).toContain("body-empty");
expect(await errors("fix: keep the deck's title\n\nRefs: #72")).toContain("body-empty");
});
it("fails a commit that names no issue, pull request or discussion", async () => {
expect(await errors(`fix: keep the deck's title\n\n${why}`)).toEqual([
"references-or-renovate",
]);
expect(await errors(`fix: keep the deck's title\n\n${why}\n\nRefs: none`)).toEqual([
"references-or-renovate",
]);
});
// semantic-release's own commit is made on main, never in a pull
// request, and its title alone made a commit count as a release.
it("fails a commit titled as semantic-release's release commit", async () => {
for (const header of [
"chore(release): 6.17.21",
"chore(release)!: 7.0.0",
"CHORE(Release): 6.17.21",
]) {
expect(await errors(`${header}\n\nBump the version.\n\nRefs: #1`)).toContain("not-a-release");
}
// The ignores read the header alone, so a body line like a merge's
// does not skip the commit.
for (const line of ["Merge branch x", "Merge tag x", "Merge x into y"]) {
expect(
await errors(`chore(release): 6.17.21\n\nBump the version.\n\n${line}\n\nRefs: #1`),
).toContain("not-a-release");
}
expect(await errors("Merge branch x\n\nchore(release): 6.17.21")).toEqual([]);
expect(await errors(`chore(releases): tidy the notes\n\n${why}\n\nRefs: #1`)).toEqual([]);
expect(await errors(`fix(release): chore(release) is refused\n\n${why}\n\nRefs: #1`)).toEqual(
[],
);
});
// GitHub skips CI on a push whose head commit's message holds one, and
// the deploy runs only after CI.
it("fails a commit that holds a keyword that skips CI, in any case, anywhere", async () => {
const keywords = [
"[skip ci]",
"[ci skip]",
"[no ci]",
"[skip actions]",
"[actions skip]",
"[Skip CI]",
"[ SKIP ACTIONS ]",
];
for (const keyword of keywords) {
expect(
await errors(`fix(web): tidy the deck list ${keyword}
${why}
Refs: #1`),
).toEqual(["no-skip-keyword"]);
expect(
await errors(`fix(web): tidy the deck list
${why} ${keyword}
Refs: #1`),
).toEqual(["no-skip-keyword"]);
}
for (const trailer of ["skip-checks: true", "Skip-Checks:true"]) {
expect(
await errors(`fix(web): tidy the deck list
${why}
Refs: #1
${trailer}`),
).toEqual(["no-skip-keyword"]);
}
// Even in a commit the other rules skip.
expect(await errors('Revert "fix: keep the title [skip ci]"')).toContain("no-skip-keyword");
expect(await errors("fixup! fix: keep the title\n\n[ci skip]")).toContain("no-skip-keyword");
// The words alone, outside the brackets or the trailer, skip nothing.
for (const words of ["skip ci on drafts", "[skip] ci", "the skip-checks: line"]) {
expect(
await errors(`fix(web): ${words}
${why}
Refs: #1`),
).toEqual([]);
}
});
// The release copies a commit's subject and its footer into CHANGELOG.md,
// whose generated entries are exempt from the linters only through
// directives in the file.
it("fails a commit that holds a directive for CHANGELOG.md's linters, anywhere", async () => {
const directives = [
"<!-- cspell:enable --> <!-- markdownlint-enable -->",
"<!-- markdownlint-enable -->",
"<!-- a note",
"a note -->",
"a note --!>",
"cspell:enable",
"CSpell::Enable",
"cspell: locale de",
"spell:locale de",
"spellchecker: flagWords the",
"spell-checker:enable",
"xcspell:enable",
"x_cspell: enable",
];
for (const directive of directives) {
expect(await errors(`fix(ui): tidy the footer ${directive}\n\n${why}\n\nCloses: #1`)).toEqual(
["no-changelog-directive"],
);
expect(await errors(`fix(ui): tidy the footer\n\n${why} ${directive}\n\nCloses: #1`)).toEqual(
["no-changelog-directive"],
);
expect(
await errors(
`fix(ui)!: tidy the footer\n\n${why}\n\nBREAKING CHANGE: ${directive}\nCloses: #1`,
),
).toEqual(["no-changelog-directive"]);
}
});
// The release's parser ends a line only at a line feed, so a character
// another reader takes for the end of one would let a message say one
// thing to it and another to the release.
it("fails a control character or a line separator other than a line feed", async () => {
const hidden = ["\u2028", "\u2029", "\u0085", "\r", "\x1e\x1f", "\0", "\x1b", "\x7f"];
for (const character of hidden) {
const breaking = `feat(app): change the deck page\n\n${why}${character}BREAKING CHANGE: x\n\nCloses: #12`;
expect(await errors(breaking), JSON.stringify(character)).toEqual(["no-hidden-break"]);
expect(await errors(`fix(app): tidy ${character} it\n\n${why}\n\nCloses: #12`)).toContain(
"no-hidden-break",
);
}
});
// The release splits git's output at this text, so a message that held
// it would be read as another.
it("fails the text the release splits git's output at", async () => {
for (const separator of ["==FIELD==", "==END=="]) {
const message = `feat(app): change the deck page\n\n${why}${separator}x\n\nCloses: #12`;
expect(await errors(message), separator).toEqual(["no-log-separator"]);
expect(await errors(`fix(app): tidy ${separator}\n\n${why}\n\nCloses: #12`)).toContain(
"no-log-separator",
);
}
expect(await errors(`fix(app): tidy ==FIELD and END==\n\n${why}\n\nCloses: #12`)).toEqual([]);
});
it("passes a tab, and a carriage return right before a line feed", async () => {
expect(await errors(`fix(app): tidy\r\n\r\n${why}\tYes.\r\n\r\nCloses: #12\r\n`)).toEqual([]);
});
it("passes what only looks like a directive", async () => {
for (const words of [
"a -> b <- c",
"the --force flag and <!- half a marker",
"keep the spelling",
"the misspell: rule",
"chore(cspell) words",
]) {
expect(await errors(`fix(ui): ${words}\n\n${why}\n\nCloses: #1`)).toEqual([]);
}
});
// CHANGELOG.md renders the header and a breaking change's footer as
// Markdown, and CI's link check reads every link and image there.
const links = [
"as [the Studio page](docs/no-such-page.md) says",
"as [the Studio page](docs/studio.md#no-such-heading) says",
"with ",
"as [the Studio page][studio] says",
"as [the Studio page][] says",
"as <file:docs/studio.md> says",
"as file:///docs/studio.md says",
'as <a href="docs/studio.md">the Studio page</a> says',
"as <IMG SRC=docs/no-such-image.png> shows",
'with <img srcset="docs/no-such-image.png">',
'as <blockquote cite="docs/studio.md">it</blockquote> says',
'with <object data="docs/no-such-file.pdf"></object>',
'with <video poster="docs/no-such-image.png"></video>',
'with <form action="docs/studio.md"></form>',
'as <a title=">" href="docs/studio.md">the Studio page</a> says',
"as <img/src=docs/no-such-image.png> shows",
"with [studio]: docs/studio.md",
];
it("fails a link or an image in the header", async () => {
for (const link of links) {
expect(await errors(`fix(studio): keep the order, ${link}\n\n${why}\n\nCloses: #1`)).toEqual([
"no-changelog-link",
]);
}
});
// The parser that commitlint and the release share drops `gpg:` lines
// and leading blank lines before it reads the header.
it("fails a link in the header the parser finds after gpg: or blank lines", async () => {
for (const before of ["gpg: Signature made today\n", "\n", "\n\n", "\n gpg: x\ngpg: y\n"]) {
expect(
await errors(
`${before}fix(studio): keep [the Studio page](docs/no-such-page.md) in order\n\n${why}\n\nCloses: #1`,
),
).toEqual(["no-changelog-link"]);
}
expect(holdsChangelogLink("gpg: [the page](docs/x.md)\n\n")).toBe(false);
});
it("fails a link or an image in a breaking change's footer, on any of its lines", async () => {
for (const footer of [
...links.map((link) => `BREAKING CHANGE: keep the order, ${link}`),
"BREAKING-CHANGE: keep the order, as [the Studio page] says\n[the Studio page]: docs/studio.md",
"BREAKING CHANGE: keep the order, as [studio] says\n\n [studio]: docs/studio.md",
"BREAKING CHANGE: keep the order, as [studio] says\n\t[studio]: docs/studio.md",
"BREAKING CHANGE: keep the order, as [studio] says\n> - [studio]: docs/studio.md",
"BREAKING CHANGE: keep the order, as [studio] says\n1. [studio\n]: docs/studio.md",
'BREAKING CHANGE: keep the order, as <a\nhref="docs/studio.md">the Studio page</a> says',
// HTML ends a tag's name only at a space, a `/` or a `>`.
'BREAKING CHANGE: keep the order\n<div>\n<x:a href="docs/no-such-page.md">the page</x:a>\n</div>',
'BREAKING CHANGE: keep the order\n<div>\n<a" href="docs/no-such-page.md">the page</a>\n</div>',
'BREAKING CHANGE: keep the order\n<div>\n<a_b href="docs/no-such-page.md">the page</a_b>\n</div>',
]) {
expect(
await errors(`fix(studio)!: keep the order\n\n${why}\n\n${footer}\nCloses: #1`),
).toEqual(["no-changelog-link"]);
}
});
it("passes a link in the body, which the release leaves out of CHANGELOG.md", async () => {
expect(
await errors(
`fix(studio): keep the order\n\n${why} See [the Studio page](docs/studio.md).\n[studio]: docs/studio.md\n\nCloses: #1`,
),
).toEqual([]);
});
it("passes brackets and parentheses that are no link", async () => {
for (const words of [
"keep a card's [front] and (back)",
"read arr[0] (the first)",
"keep the order (docs/studio.md)",
"keep the order, as [the Studio page] (docs/studio.md) says",
"name the file: docs/studio.md, in a <dialog>",
"keep x<y and z",
"keep a <br/> and a <br />",
]) {
expect(await errors(`fix(ui): ${words}\n\n${why}\n\nCloses: #1`)).toEqual([]);
expect(
await errors(`fix(ui)!: ${words}\n\n${why}\n\nBREAKING CHANGE: ${words}\nCloses: #1`),
).toEqual([]);
}
});
it("reads a long message of tags that never close in linear time", () => {
const timed = (runs: number) => {
const message = `fix(ui)!: keep the order\n\n${why}\n\nBREAKING CHANGE: ${"<a b".repeat(runs)}`;
const started = performance.now();
expect(holdsChangelogLink(message)).toBe(false);
expect(holdsChangelogLink(`${message}>`)).toBe(true);
return performance.now() - started;
};
timed(20_000);
// Ten times the input takes about ten times as long, where a check
// that read each tag again would take about a hundred times as long.
expect(timed(200_000)).toBeLessThan(40 * timed(20_000) + 50);
});
it("still skips a revert and a fixup, as commitlint does by default", async () => {
expect(await errors('Revert "fix: keep the deck\'s title"')).toEqual([]);
expect(await errors("fixup! fix: keep the deck's title")).toEqual([]);
expect(await errors("chore: 1.2.3")).toContain("body-empty");
});
it("passes a Renovate update, which cannot name its pull request", async () => {
expect(
await errors(`chore(deps): update dependency vite to v8.3.4\n\n${renovateBody}`),
).toEqual([]);
});
it("still checks a Renovate update's header", async () => {
expect(await errors(`Update dependency vite to v8.3.4\n\n${renovateBody}`)).toEqual(
expect.arrayContaining(["subject-empty", "type-empty"]),
);
});
it("takes Renovate's line from the commitBody that renovate.json5 writes", () => {
const renovate = readFileSync(join(ROOT, "renovate.json5"), "utf8");
const commitBody = /commitBody: "(.*)",$/m.exec(renovate)?.[1];
expect(commitBody).toContain(`\\n${RENOVATE_WHY}`);
expect(renovateBody.split("\n").at(-1)?.startsWith(RENOVATE_WHY)).toBe(true);
});
});