mirror of
https://github.com/qybaihe/bla.git
synced 2026-11-20 08:20:59 +08:00
chore(release): refuse non-semver package.json versions in sync-version (#205)
This commit is contained in:
@@ -10,6 +10,17 @@ const lockPath = resolve(root, "package-lock.json");
|
||||
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
||||
const version = String(pkg.version);
|
||||
|
||||
// Refuse to propagate a malformed version: the regex rewrites below would
|
||||
// silently corrupt identity.ts, SEAL labels, badges, and marketplace manifests.
|
||||
const SEMVER_PATTERN =
|
||||
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?$/;
|
||||
if (!SEMVER_PATTERN.test(version)) {
|
||||
console.error(
|
||||
`sync-version: package.json version "${version}" is not valid semver; refusing to sync any files.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const identityText = readFileSync(identityPath, "utf8");
|
||||
const updated = identityText.replace(
|
||||
/version:\s*"[^"]+"/,
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
const repoRoot = resolveRepoRoot();
|
||||
const scriptSource = readFileSync(
|
||||
join(repoRoot, "scripts", "sync-version.mjs"),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
function resolveRepoRoot(): string {
|
||||
// tests/ sits next to scripts/ at the repository root.
|
||||
return dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
}
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function makeSandbox(version: string): string {
|
||||
const root = mkdtempSync(join(tmpdir(), "bla-sync-version-"));
|
||||
tempDirs.push(root);
|
||||
mkdirSync(join(root, "scripts"), { recursive: true });
|
||||
mkdirSync(join(root, "src"), { recursive: true });
|
||||
writeFileSync(join(root, "scripts", "sync-version.mjs"), scriptSource, "utf8");
|
||||
writeFileSync(
|
||||
join(root, "package.json"),
|
||||
`${JSON.stringify({ name: "fixture", version }, null, 2)}\n`,
|
||||
"utf8",
|
||||
);
|
||||
writeFileSync(
|
||||
join(root, "src", "identity.ts"),
|
||||
`export const BLA_IDENTITY = {\n version: "0.0.0",\n};\n`,
|
||||
"utf8",
|
||||
);
|
||||
return root;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("sync-version.mjs semver guard", () => {
|
||||
it("refuses a non-semver package.json version without touching identity.ts", () => {
|
||||
const root = makeSandbox("not-a-version");
|
||||
const identityPath = join(root, "src", "identity.ts");
|
||||
const before = readFileSync(identityPath, "utf8");
|
||||
const run = spawnSync(process.execPath, [join(root, "scripts", "sync-version.mjs")], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
expect(run.status).toBe(1);
|
||||
expect(`${run.stderr}${run.stdout}`).toContain("not valid semver");
|
||||
expect(readFileSync(identityPath, "utf8")).toBe(before);
|
||||
});
|
||||
|
||||
it("syncs a valid semver version as before", () => {
|
||||
const root = makeSandbox("9.9.9");
|
||||
const identityPath = join(root, "src", "identity.ts");
|
||||
const run = spawnSync(process.execPath, [join(root, "scripts", "sync-version.mjs")], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
expect(run.status).toBe(0);
|
||||
expect(readFileSync(identityPath, "utf8")).toContain('version: "9.9.9"');
|
||||
});
|
||||
|
||||
it("accepts prerelease and build-metadata versions", () => {
|
||||
const root = makeSandbox("4.0.0-nightly.2+sha.abc");
|
||||
const run = spawnSync(process.execPath, [join(root, "scripts", "sync-version.mjs")], {
|
||||
encoding: "utf8",
|
||||
});
|
||||
expect(run.status).toBe(0);
|
||||
expect(readFileSync(join(root, "src", "identity.ts"), "utf8")).toContain(
|
||||
'version: "4.0.0-nightly.2+sha.abc"',
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user