Semver check: a malformed version is refused by name, and refusals are pinned never to repeat input contents

A malformed --version fell into the tag-reading catch and was reported as unreadable
tags. It is now validated first and refused without repeating the value. A canary test
runs seven refusals (measured break, unreadable built DLL, unreadable baseline blob,
missing entry, skipped version, breaking under too small a bump) with a canary in the
changelog and the assemblies and asserts it never appears in the output.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-02 21:27:03 +02:00
commit e7786ffac2
2 changed files with 66 additions and 0 deletions

View file

@ -351,6 +351,12 @@ function main() {
process.exitCode = 2;
return;
}
// Checked before anything reads tags: a malformed version is its own refusal, and the
// value is not repeated back.
if (!VERSION_PATTERN.test(version.trim())) {
refuse(`${packageName}: --version is not a MAJOR.MINOR.PATCH version`);
return;
}
if (!existsSync(changelog)) {
refuse(`${packageName}: changelog not found at ${changelog}`);
return;

View file

@ -750,3 +750,63 @@ test("evaluateRelease: lists at most five measured breaks, and says nothing more
}).reason;
assert.match(agreed, /a "### Breaking" section and 1 measured public API break\(s\)/);
});
test("CLI: a malformed version is refused by name, without repeating it, with or without --first-release", () => {
withTempGitRepo((dir) => {
execFileSync("git", ["commit", "--allow-empty", "-q", "-m", "init"], { cwd: dir });
execFileSync("git", ["tag", "widget-v1.0.0"], { cwd: dir });
const changelogPath = join(dir, "CHANGELOG.md");
writeFileSync(changelogPath, changelogFor("1.1.0"));
for (const extra of [[], ["--first-release"]]) {
const output = runCheckerExpectFailure(dir, ["--package", "widget", "--changelog", changelogPath, "--version", "1.1.x-canary", "--tag-prefix", "widget", ...extra]);
assert.match(output, /--version is not a MAJOR\.MINOR\.PATCH version/);
assert.doesNotMatch(output, /canary/);
assert.doesNotMatch(output, /could not be read/);
}
});
});
test("refusals never repeat what the inputs contain", () => {
const canary = "CANARY-5f3a9c-never-echoed";
const scratch = mkdtempSync(join(tmpdir(), "cultlib-semver-canary-"));
try {
const junk = join(scratch, "GameCult.Widget.dll");
writeFileSync(junk, `MZ not an assembly ${canary}`);
const canaryChangelog = (dir, version, { breaking = false } = {}) => {
const path = join(dir, "CHANGELOG.md");
writeFileSync(path, changelogFor(version, { breaking }).replace("- something changed", `- something changed ${canary}`) + `\n## [0.0.1]\n\n- ${canary}\n`);
return path;
};
const refuse = (dir, args) => {
const output = runCheckerExpectFailure(dir, args);
assert.doesNotMatch(output, new RegExp(canary), `the refusal repeated an input: ${output}`);
return output;
};
withReleasedBaseline({ baseline: { "GameCult.Widget.dll": oldWidget() } }, (dir) => {
const measured = (version, built, options) => {
const args = measuredArgs(dir, version, built, options);
args[args.indexOf("--changelog") + 1] = canaryChangelog(dir, version, options);
return args;
};
// a measured break, with the canary in the changelog it must be declared in
assert.match(refuse(dir, measured("1.1.0", [trimmedWidget()])), /Gone/);
// the tool cannot read the built assembly
assert.match(refuse(dir, measured("1.0.1", [junk])), /could not be measured/);
// no entry for the version, a skipped version, a breaking entry under too small a bump
const noEntry = measured("1.0.1", [oldWidget()]);
noEntry[noEntry.indexOf("--version") + 1] = "1.0.2";
assert.match(refuse(dir, noEntry), /no "## \[1\.0\.2\]" entry/);
const skip = measured("1.0.9", [oldWidget()]);
assert.match(refuse(dir, skip), /skips ahead/);
assert.match(refuse(dir, measured("1.0.1", [oldWidget()], { breaking: true })), /needs a major bump/);
});
// the tag's own blob is unreadable
withReleasedBaseline({ baseline: { "GameCult.Widget.dll": junk } }, (dir) => {
const args = measuredArgs(dir, "1.0.1", [oldWidget()]);
args[args.indexOf("--changelog") + 1] = canaryChangelog(dir, "1.0.1");
assert.match(refuse(dir, args), /could not be measured/);
});
} finally {
rmSync(scratch, { recursive: true, force: true });
}
});