补 tools/dev/bump_version.py 单元测试(+10 用例)
- parse_semver 纯函数测试(含边界/异常) - compute_next 版本号递增测试 - 集成测试:连续 bump 验证 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f7e8da57a
commit
a6dee879e7
1 file changed
+111
@@ -0,0 +1,111 @@
|
||||
"""tools/dev 模块单元测试。
|
||||
|
||||
覆盖:
|
||||
- bump_version.py: parse_semver, compute_next 纯函数
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
# 添加 tools 到 path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "tools" / "dev"))
|
||||
|
||||
|
||||
class TestParseSemver:
|
||||
"""parse_semver 纯函数测试。"""
|
||||
|
||||
def test_parse_with_v_prefix(self):
|
||||
from bump_version import parse_semver
|
||||
|
||||
assert parse_semver("V3.3.3") == (3, 3, 3)
|
||||
assert parse_semver("V1.0.0") == (1, 0, 0)
|
||||
assert parse_semver("V10.20.30") == (10, 20, 30)
|
||||
|
||||
def test_parse_with_lowercase_v(self):
|
||||
from bump_version import parse_semver
|
||||
|
||||
assert parse_semver("v3.3.3") == (3, 3, 3)
|
||||
assert parse_semver("v1.2.3") == (1, 2, 3)
|
||||
|
||||
def test_parse_without_prefix(self):
|
||||
from bump_version import parse_semver
|
||||
|
||||
assert parse_semver("3.3.3") == (3, 3, 3)
|
||||
assert parse_semver("0.0.1") == (0, 0, 1)
|
||||
|
||||
def test_parse_invalid_format_raises(self):
|
||||
from bump_version import parse_semver
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
parse_semver("3.3") # 缺少 patch
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
parse_semver("3.3.3.4") # 多余部分
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
parse_semver("abc") # 非数字
|
||||
|
||||
def test_parse_non_numeric_raises(self):
|
||||
from bump_version import parse_semver
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
parse_semver("V3.x.3")
|
||||
|
||||
|
||||
class TestComputeNext:
|
||||
"""compute_next 纯函数测试。"""
|
||||
|
||||
def test_patch_bump(self):
|
||||
from bump_version import compute_next
|
||||
|
||||
assert compute_next("V3.3.3", "patch") == "V3.3.4"
|
||||
assert compute_next("V1.0.0", "patch") == "V1.0.1"
|
||||
assert compute_next("V0.0.9", "patch") == "V0.0.10"
|
||||
|
||||
def test_minor_bump(self):
|
||||
from bump_version import compute_next
|
||||
|
||||
assert compute_next("V3.3.3", "minor") == "V3.4.0"
|
||||
assert compute_next("V1.0.5", "minor") == "V1.1.0"
|
||||
assert compute_next("V0.9.9", "minor") == "V0.10.0"
|
||||
|
||||
def test_major_bump(self):
|
||||
from bump_version import compute_next
|
||||
|
||||
assert compute_next("V3.3.3", "major") == "V4.0.0"
|
||||
assert compute_next("V0.1.2", "major") == "V1.0.0"
|
||||
assert compute_next("V9.9.9", "major") == "V10.0.0"
|
||||
|
||||
def test_unknown_level_raises(self):
|
||||
from bump_version import compute_next
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
compute_next("V3.3.3", "unknown")
|
||||
|
||||
with pytest.raises(SystemExit):
|
||||
compute_next("V3.3.3", "")
|
||||
|
||||
|
||||
class TestVersionIntegration:
|
||||
"""版本号处理集成测试。"""
|
||||
|
||||
def test_round_trip(self):
|
||||
from bump_version import compute_next, parse_semver
|
||||
|
||||
# 从 V1.0.0 开始,连续 bump
|
||||
v = "V1.0.0"
|
||||
v = compute_next(v, "patch")
|
||||
assert v == "V1.0.1"
|
||||
assert parse_semver(v) == (1, 0, 1)
|
||||
|
||||
v = compute_next(v, "minor")
|
||||
assert v == "V1.1.0"
|
||||
assert parse_semver(v) == (1, 1, 0)
|
||||
|
||||
v = compute_next(v, "major")
|
||||
assert v == "V2.0.0"
|
||||
assert parse_semver(v) == (2, 0, 0)
|
||||
Reference in new issue
Block a user