Skip to content

Commit ae48798

Browse files
committed
Make versions.ts testable and add basic tests
1 parent a464bf1 commit ae48798

3 files changed

Lines changed: 90 additions & 20 deletions

File tree

pr-checks/update-release-branch.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import { parseArgs } from "node:util";
2424
import { type ApiClient, getApiClient } from "./api-client";
2525
import * as changelog from "./changelog";
2626
import { DryRunOption, REPO_ROOT } from "./config";
27-
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
27+
import {
28+
getCurrentVersion,
29+
replaceVersionInPackageJson,
30+
withPackageJson,
31+
} from "./versions";
2832

2933
/**
3034
* NB: This exact commit message is used to find commits for reverting during backports.
@@ -636,10 +640,20 @@ export async function prepareNewBranch(
636640

637641
// Migrate the package version number.
638642
console.log(`Setting version number to '${version}' in package.json`);
639-
const currentPkgVersion = getCurrentVersion();
640-
if (currentPkgVersion) {
641-
replaceVersionInPackageJson(options, currentPkgVersion, version);
642-
}
643+
withPackageJson((content) => {
644+
const currentPkgVersion = getCurrentVersion(content);
645+
if (currentPkgVersion) {
646+
return {
647+
content: replaceVersionInPackageJson(
648+
currentPkgVersion,
649+
version,
650+
content,
651+
),
652+
value: currentPkgVersion,
653+
};
654+
}
655+
return { value: currentPkgVersion };
656+
}, options);
643657
runGit(["add", "package.json"], {
644658
dryRun: options.dryRun,
645659
});
@@ -733,7 +747,9 @@ async function main(): Promise<void> {
733747
"",
734748
);
735749

736-
const currentVersion = getCurrentVersion();
750+
const currentVersion = withPackageJson((content) => {
751+
return { value: getCurrentVersion(content) };
752+
}, options);
737753

738754
if (!currentVersion) {
739755
throw new Error("Failed to read current version from package.json");

pr-checks/versions.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env npx tsx
2+
3+
/**
4+
* Tests for `versions.ts`.
5+
*/
6+
7+
import * as assert from "node:assert/strict";
8+
import { describe, it } from "node:test";
9+
10+
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
11+
12+
describe("getCurrentVersion", async () => {
13+
await it("reads versions", async () => {
14+
const result = getCurrentVersion(`{ "version": "1.23.4" }`);
15+
assert.deepEqual(result, "1.23.4");
16+
});
17+
});
18+
19+
const packageJsonContents = `{
20+
"name": "codeql",
21+
"version": "1.23.4"
22+
}
23+
`;
24+
25+
const packageJsonContentsExpected = `{
26+
"name": "codeql",
27+
"version": "2.23.4"
28+
}
29+
`;
30+
31+
describe("replaceVersionInPackageJson", async () => {
32+
await it("replaces versions", async () => {
33+
const result = replaceVersionInPackageJson(
34+
"1.23.4",
35+
"2.23.4",
36+
packageJsonContents,
37+
);
38+
assert.deepEqual(
39+
result.split("\n"),
40+
packageJsonContentsExpected.split("\n"),
41+
);
42+
assert.deepEqual(JSON.parse(result), { name: "codeql", version: "2.23.4" });
43+
});
44+
});

pr-checks/versions.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import * as fs from "node:fs";
22

33
import { DryRunOption, PACKAGE_JSON } from "./config";
44

5+
export function withPackageJson<T>(
6+
transformer: (content: string) => { value: T; content?: string },
7+
options: DryRunOption,
8+
): T {
9+
const content = fs.readFileSync(PACKAGE_JSON, "utf8");
10+
const result = transformer(content);
11+
12+
if (result.content !== undefined) {
13+
if (!options.dryRun) {
14+
fs.writeFileSync(PACKAGE_JSON, result.content, "utf8");
15+
} else {
16+
console.info(`[DRY RUN] Would have written an updated package.json`);
17+
}
18+
}
19+
20+
return result.value;
21+
}
22+
523
/** Reads the current version from `package.json`. */
6-
export function getCurrentVersion(): string | undefined {
7-
const pkg: { version: string } = JSON.parse(
8-
fs.readFileSync(PACKAGE_JSON, "utf8"),
9-
);
24+
export function getCurrentVersion(content: string): string | undefined {
25+
const pkg: { version: string } = JSON.parse(content);
1026
return pkg.version;
1127
}
1228

@@ -17,11 +33,11 @@ export function getCurrentVersion(): string | undefined {
1733
* replace the version in package.json textually.
1834
*/
1935
export function replaceVersionInPackageJson(
20-
options: DryRunOption,
2136
prevVersion: string,
2237
newVersion: string,
23-
): void {
24-
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
38+
content: string,
39+
): string {
40+
const lines = content.split("\n");
2541
let prevLineIsCodeql = false;
2642
const output: string[] = [];
2743

@@ -34,11 +50,5 @@ export function replaceVersionInPackageJson(
3450
prevLineIsCodeql = line.includes('"name": "codeql",');
3551
}
3652

37-
if (!options.dryRun) {
38-
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
39-
} else {
40-
console.info(
41-
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
42-
);
43-
}
53+
return output.join("\n");
4454
}

0 commit comments

Comments
 (0)