Skip to content

Commit d694648

Browse files
committed
Make changelog.ts more testable and add tests
1 parent 583bf3e commit d694648

3 files changed

Lines changed: 105 additions & 34 deletions

File tree

‎pr-checks/changelog.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env npx tsx
2+
3+
/**
4+
* Tests for `changelog.ts`.
5+
*/
6+
7+
import * as assert from "node:assert/strict";
8+
import { describe, it } from "node:test";
9+
10+
import {
11+
EMPTY_CHANGELOG,
12+
getReleaseDateString,
13+
processChangelogForBackports,
14+
setVersionAndDate,
15+
} from "./changelog";
16+
17+
const testDate = new Date(2026, 7, 14);
18+
19+
describe("getReleaseDateString", async () => {
20+
await it("formats dates as expected", async () => {
21+
assert.equal(getReleaseDateString(testDate), "14 Aug 2026");
22+
});
23+
});
24+
25+
const emptyChangelogExpected = `# CodeQL Action Changelog
26+
27+
## 9.99.9 - 14 Aug 2026
28+
29+
No user facing changes.
30+
31+
`;
32+
33+
describe("setVersionAndDate", async () => {
34+
await it("replaces the placeholder", async () => {
35+
const result = setVersionAndDate("9.99.9", EMPTY_CHANGELOG, testDate);
36+
assert.equal(result, emptyChangelogExpected);
37+
});
38+
});
39+
40+
const testChangelog = `# CodeQL Action Changelog
41+
42+
## 4.12.3 - 14 Aug 2026
43+
44+
No user facing changes.
45+
`;
46+
47+
const testChangelogResult: string = `# CodeQL Action Changelog
48+
49+
## 3.12.3 - 14 Aug 2026
50+
51+
No user facing changes.
52+
`;
53+
54+
describe("processChangelogForBackports", async () => {
55+
await it("replaces major versions", async () => {
56+
const result = processChangelogForBackports("4", "3", testChangelog);
57+
58+
assert.deepEqual(result.split("\n"), testChangelogResult.split("\n"));
59+
});
60+
});

‎pr-checks/changelog.ts‎

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,65 @@ No user facing changes.
1111
1212
`;
1313

14-
/** Returns today's date formatted as `DD Mon YYYY`. */
15-
export function getTodayString(): string {
16-
const today = new Date();
14+
/** Returns `date` formatted as `DD Mon YYYY`. */
15+
export function getReleaseDateString(today: Date = new Date()): string {
1716
return today.toLocaleDateString("en-GB", {
1817
day: "2-digit",
1918
month: "short",
2019
year: "numeric",
2120
});
2221
}
2322

24-
/**
25-
* Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
26-
* and today's date.
27-
*/
28-
export function setVersionAndDate(
29-
options: DryRunOption,
30-
version: string,
23+
export interface OpenChangelogOptions {
24+
initChangelog?: boolean;
25+
}
26+
27+
export function withChangelog(
28+
transformer: (contents: string) => string,
29+
options: DryRunOption & OpenChangelogOptions,
3130
): void {
3231
let content: string;
3332

34-
if (fs.existsSync(CHANGELOG_FILE)) {
35-
content = fs.readFileSync(CHANGELOG_FILE, "utf8");
36-
} else {
33+
if (options.initChangelog && !fs.existsSync(CHANGELOG_FILE)) {
3734
content = EMPTY_CHANGELOG;
35+
} else {
36+
content = fs.readFileSync(CHANGELOG_FILE, "utf8");
3837
}
3938

40-
const versionAndDate = `${version} - ${getTodayString()}`;
41-
const newContent = content.replace("[UNRELEASED]", versionAndDate);
42-
4339
if (!options.dryRun) {
44-
fs.writeFileSync(CHANGELOG_FILE, newContent, "utf8");
40+
fs.writeFileSync(CHANGELOG_FILE, transformer(content), "utf8");
4541
} else {
46-
console.info(
47-
`[DRY RUN] Would have replaced '[UNRELEASED]' in '${CHANGELOG_FILE}' with '${versionAndDate}'.`,
48-
);
42+
console.info(`[DRY RUN] Would have written updated changelog.`);
4943
}
5044
}
5145

46+
/**
47+
* Updates the `[UNRELEASED]` marker in `CHANGELOG.md` with the given version
48+
* and today's date.
49+
*/
50+
export function setVersionAndDate(
51+
version: string,
52+
content: string,
53+
date: Date = new Date(),
54+
): string {
55+
const versionAndDate = `${version} - ${getReleaseDateString(date)}`;
56+
return content.replace("[UNRELEASED]", versionAndDate);
57+
}
58+
5259
/**
5360
* Processes changelog entries for a backport, converting version references
5461
* from the source major version to the target major version and filtering
5562
* entries that only apply to newer versions.
5663
*/
5764
export function processChangelogForBackports(
58-
options: DryRunOption,
5965
sourceBranchMajorVersion: string,
6066
targetBranchMajorVersion: string,
61-
): void {
62-
const content = fs.readFileSync(CHANGELOG_FILE, "utf8");
67+
content: string,
68+
): string {
6369
const lines = content.split("\n");
70+
71+
// Changelog entries can use the following format to indicate
72+
// that they only apply to newer versions
6473
const someVersionsOnlyRegex = /\[v(\d+)\+ only\]/;
6574

6675
let output = "";
@@ -86,6 +95,7 @@ export function processChangelogForBackports(
8695
}
8796

8897
// Process remaining lines.
98+
// `foundContent` tracks whether we hit two headings in a row
8999
let foundContent = false;
90100
output += "\n";
91101

@@ -121,11 +131,5 @@ export function processChangelogForBackports(
121131
}
122132
}
123133

124-
if (!options.dryRun) {
125-
fs.writeFileSync(CHANGELOG_FILE, output, "utf8");
126-
} else {
127-
console.info(
128-
`[DRY RUN] Would have written updated changelog to replace 'v${sourceBranchMajorVersion}' with 'v${targetBranchMajorVersion}'.`,
129-
);
130-
}
134+
return output;
131135
}

‎pr-checks/update-release-branch.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,14 @@ export async function prepareNewBranch(
682682
console.log(
683683
`Migrating changelog notes from v${sourceBranchMajorVersion} to v${targetBranchMajorVersion}`,
684684
);
685-
changelog.processChangelogForBackports(
685+
changelog.withChangelog(
686+
(contents) =>
687+
changelog.processChangelogForBackports(
688+
sourceBranchMajorVersion,
689+
targetBranchMajorVersion,
690+
contents,
691+
),
686692
options,
687-
sourceBranchMajorVersion,
688-
targetBranchMajorVersion,
689693
);
690694

691695
runGit(["add", "CHANGELOG.md"], {
@@ -706,7 +710,10 @@ export async function prepareNewBranch(
706710
);
707711

708712
console.log("Updating changelog");
709-
changelog.setVersionAndDate(options, version);
713+
changelog.withChangelog(
714+
(contents) => changelog.setVersionAndDate(version, contents),
715+
{ ...options, initChangelog: true },
716+
);
710717

711718
runGit(["add", "CHANGELOG.md"], {
712719
dryRun: options.dryRun,

0 commit comments

Comments
 (0)