test: 版本递增逻辑单元测试
This commit is contained in:
81
tests/scripts/release.test.ts
Normal file
81
tests/scripts/release.test.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
|
||||
function parseSemver(version: string): { major: number; minor: number; patch: number } {
|
||||
const parts = version.split(".");
|
||||
if (parts.length !== 3) {
|
||||
throw new Error(`无效的版本号格式: ${version}`);
|
||||
}
|
||||
const [major, minor, patch] = parts.map((p) => {
|
||||
const n = Number(p);
|
||||
if (Number.isNaN(n) || !Number.isInteger(n) || n < 0) {
|
||||
throw new Error(`无效的版本号格式: ${version}`);
|
||||
}
|
||||
return n;
|
||||
});
|
||||
return { major: major!, minor: minor!, patch: patch! };
|
||||
}
|
||||
|
||||
function bumpVersion(current: string, type: "major" | "minor" | "patch"): string {
|
||||
const semver = parseSemver(current);
|
||||
switch (type) {
|
||||
case "major":
|
||||
return `${semver.major + 1}.0.0`;
|
||||
case "minor":
|
||||
return `${semver.major}.${semver.minor + 1}.0`;
|
||||
case "patch":
|
||||
return `${semver.major}.${semver.minor}.${semver.patch + 1}`;
|
||||
}
|
||||
}
|
||||
|
||||
describe("parseSemver", () => {
|
||||
it("解析标准版本号 1.2.3", () => {
|
||||
expect(parseSemver("1.2.3")).toEqual({ major: 1, minor: 2, patch: 3 });
|
||||
});
|
||||
it("解析 0.0.0", () => {
|
||||
expect(parseSemver("0.0.0")).toEqual({ major: 0, minor: 0, patch: 0 });
|
||||
});
|
||||
it("解析大版本号 99.999.1", () => {
|
||||
expect(parseSemver("99.999.1")).toEqual({ major: 99, minor: 999, patch: 1 });
|
||||
});
|
||||
it("非三位格式抛出异常", () => {
|
||||
expect(() => parseSemver("1.2")).toThrow("无效的版本号格式");
|
||||
expect(() => parseSemver("1.2.3.4")).toThrow("无效的版本号格式");
|
||||
});
|
||||
it("非数字格式抛出异常", () => {
|
||||
expect(() => parseSemver("a.b.c")).toThrow("无效的版本号格式");
|
||||
expect(() => parseSemver("1.2.3-beta")).toThrow("无效的版本号格式");
|
||||
});
|
||||
it("负数抛出异常", () => {
|
||||
expect(() => parseSemver("-1.2.3")).toThrow("无效的版本号格式");
|
||||
});
|
||||
it("小数抛出异常", () => {
|
||||
expect(() => parseSemver("1.5.3.2")).toThrow("无效的版本号格式");
|
||||
});
|
||||
});
|
||||
|
||||
describe("bumpVersion", () => {
|
||||
it("major 递增:0.1.0 → 1.0.0", () => {
|
||||
expect(bumpVersion("0.1.0", "major")).toBe("1.0.0");
|
||||
});
|
||||
it("minor 递增:0.1.0 → 0.2.0", () => {
|
||||
expect(bumpVersion("0.1.0", "minor")).toBe("0.2.0");
|
||||
});
|
||||
it("patch 递增:0.1.0 → 0.1.1", () => {
|
||||
expect(bumpVersion("0.1.0", "patch")).toBe("0.1.1");
|
||||
});
|
||||
it("major 递增低位归零:1.5.3 → 2.0.0", () => {
|
||||
expect(bumpVersion("1.5.3", "major")).toBe("2.0.0");
|
||||
});
|
||||
it("minor 递增 patch 归零:1.5.3 → 1.6.0", () => {
|
||||
expect(bumpVersion("1.5.3", "minor")).toBe("1.6.0");
|
||||
});
|
||||
it("patch 递增不影响高位:1.5.3 → 1.5.4", () => {
|
||||
expect(bumpVersion("1.5.3", "patch")).toBe("1.5.4");
|
||||
});
|
||||
it("大数字递增正确", () => {
|
||||
expect(bumpVersion("99.999.1", "minor")).toBe("99.1000.0");
|
||||
});
|
||||
it("无效版本号抛出异常", () => {
|
||||
expect(() => bumpVersion("invalid", "minor")).toThrow("无效的版本号格式");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user