From 9cc0a96f6893f63663c0b44d511275ffeb4720fd Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Tue, 9 Jun 2026 17:47:40 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E7=89=88=E6=9C=AC=E9=80=92=E5=A2=9E?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/scripts/release.test.ts | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/scripts/release.test.ts diff --git a/tests/scripts/release.test.ts b/tests/scripts/release.test.ts new file mode 100644 index 0000000..08e7d67 --- /dev/null +++ b/tests/scripts/release.test.ts @@ -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("无效的版本号格式"); + }); +});