Make versions.ts testable and add basic tests

This commit is contained in:
Michael B. Gale
2026-07-14 15:48:57 +01:00
parent a464bf19e9
commit ae48798f3b
3 changed files with 90 additions and 20 deletions

View File

@@ -24,7 +24,11 @@ import { parseArgs } from "node:util";
import { type ApiClient, getApiClient } from "./api-client";
import * as changelog from "./changelog";
import { DryRunOption, REPO_ROOT } from "./config";
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
import {
getCurrentVersion,
replaceVersionInPackageJson,
withPackageJson,
} from "./versions";
/**
* NB: This exact commit message is used to find commits for reverting during backports.
@@ -636,10 +640,20 @@ export async function prepareNewBranch(
// Migrate the package version number.
console.log(`Setting version number to '${version}' in package.json`);
const currentPkgVersion = getCurrentVersion();
if (currentPkgVersion) {
replaceVersionInPackageJson(options, currentPkgVersion, version);
}
withPackageJson((content) => {
const currentPkgVersion = getCurrentVersion(content);
if (currentPkgVersion) {
return {
content: replaceVersionInPackageJson(
currentPkgVersion,
version,
content,
),
value: currentPkgVersion,
};
}
return { value: currentPkgVersion };
}, options);
runGit(["add", "package.json"], {
dryRun: options.dryRun,
});
@@ -733,7 +747,9 @@ async function main(): Promise<void> {
"",
);
const currentVersion = getCurrentVersion();
const currentVersion = withPackageJson((content) => {
return { value: getCurrentVersion(content) };
}, options);
if (!currentVersion) {
throw new Error("Failed to read current version from package.json");

44
pr-checks/versions.test.ts Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env npx tsx
/**
* Tests for `versions.ts`.
*/
import * as assert from "node:assert/strict";
import { describe, it } from "node:test";
import { getCurrentVersion, replaceVersionInPackageJson } from "./versions";
describe("getCurrentVersion", async () => {
await it("reads versions", async () => {
const result = getCurrentVersion(`{ "version": "1.23.4" }`);
assert.deepEqual(result, "1.23.4");
});
});
const packageJsonContents = `{
"name": "codeql",
"version": "1.23.4"
}
`;
const packageJsonContentsExpected = `{
"name": "codeql",
"version": "2.23.4"
}
`;
describe("replaceVersionInPackageJson", async () => {
await it("replaces versions", async () => {
const result = replaceVersionInPackageJson(
"1.23.4",
"2.23.4",
packageJsonContents,
);
assert.deepEqual(
result.split("\n"),
packageJsonContentsExpected.split("\n"),
);
assert.deepEqual(JSON.parse(result), { name: "codeql", version: "2.23.4" });
});
});

View File

@@ -2,11 +2,27 @@ import * as fs from "node:fs";
import { DryRunOption, PACKAGE_JSON } from "./config";
export function withPackageJson<T>(
transformer: (content: string) => { value: T; content?: string },
options: DryRunOption,
): T {
const content = fs.readFileSync(PACKAGE_JSON, "utf8");
const result = transformer(content);
if (result.content !== undefined) {
if (!options.dryRun) {
fs.writeFileSync(PACKAGE_JSON, result.content, "utf8");
} else {
console.info(`[DRY RUN] Would have written an updated package.json`);
}
}
return result.value;
}
/** Reads the current version from `package.json`. */
export function getCurrentVersion(): string | undefined {
const pkg: { version: string } = JSON.parse(
fs.readFileSync(PACKAGE_JSON, "utf8"),
);
export function getCurrentVersion(content: string): string | undefined {
const pkg: { version: string } = JSON.parse(content);
return pkg.version;
}
@@ -17,11 +33,11 @@ export function getCurrentVersion(): string | undefined {
* replace the version in package.json textually.
*/
export function replaceVersionInPackageJson(
options: DryRunOption,
prevVersion: string,
newVersion: string,
): void {
const lines = fs.readFileSync(PACKAGE_JSON, "utf8").split("\n");
content: string,
): string {
const lines = content.split("\n");
let prevLineIsCodeql = false;
const output: string[] = [];
@@ -34,11 +50,5 @@ export function replaceVersionInPackageJson(
prevLineIsCodeql = line.includes('"name": "codeql",');
}
if (!options.dryRun) {
fs.writeFileSync(PACKAGE_JSON, `${output.join("\n")}\n`, "utf8");
} else {
console.info(
`[DRY RUN] Would have replaced '${prevVersion}' with '${newVersion}' in package.json`,
);
}
return output.join("\n");
}