|
| 1 | +"""Tests for SDK API breakage check script. |
| 2 | +
|
| 3 | +These tests verify the core policy logic without requiring griffe or network access. |
| 4 | +The functions are duplicated here to avoid import issues with the .github/scripts path. |
| 5 | +""" |
| 6 | + |
| 7 | +from packaging import version as pkg_version |
| 8 | + |
| 9 | + |
| 10 | +def _parse_version(v: str) -> pkg_version.Version: |
| 11 | + """Parse a version string using packaging (mirrors script implementation).""" |
| 12 | + return pkg_version.parse(v) |
| 13 | + |
| 14 | + |
| 15 | +def _check_version_bump(prev: str, new_version: str, total_breaks: int) -> int: |
| 16 | + """Check version bump policy (mirrors script implementation). |
| 17 | +
|
| 18 | + Policy: Breaking changes require at least a MINOR version bump. |
| 19 | + Returns 0 if policy satisfied, 1 if not. |
| 20 | + """ |
| 21 | + if total_breaks == 0: |
| 22 | + return 0 |
| 23 | + |
| 24 | + parsed_prev = _parse_version(prev) |
| 25 | + parsed_new = _parse_version(new_version) |
| 26 | + |
| 27 | + ok = (parsed_new.major > parsed_prev.major) or ( |
| 28 | + parsed_new.major == parsed_prev.major and parsed_new.minor > parsed_prev.minor |
| 29 | + ) |
| 30 | + |
| 31 | + return 0 if ok else 1 |
| 32 | + |
| 33 | + |
| 34 | +def test_parse_version_simple(): |
| 35 | + v = _parse_version("1.2.3") |
| 36 | + assert v.major == 1 |
| 37 | + assert v.minor == 2 |
| 38 | + assert v.micro == 3 |
| 39 | + |
| 40 | + |
| 41 | +def test_parse_version_prerelease(): |
| 42 | + v = _parse_version("1.2.3a1") |
| 43 | + assert v.major == 1 |
| 44 | + assert v.minor == 2 |
| 45 | + |
| 46 | + |
| 47 | +def test_no_breaks_passes(): |
| 48 | + """No breaking changes should always pass.""" |
| 49 | + assert _check_version_bump("1.0.0", "1.0.1", total_breaks=0) == 0 |
| 50 | + |
| 51 | + |
| 52 | +def test_minor_bump_with_breaks_passes(): |
| 53 | + """MINOR bump satisfies policy for breaking changes.""" |
| 54 | + assert _check_version_bump("1.0.0", "1.1.0", total_breaks=1) == 0 |
| 55 | + assert _check_version_bump("1.5.3", "1.6.0", total_breaks=5) == 0 |
| 56 | + |
| 57 | + |
| 58 | +def test_major_bump_with_breaks_passes(): |
| 59 | + """MAJOR bump also satisfies policy for breaking changes.""" |
| 60 | + assert _check_version_bump("1.0.0", "2.0.0", total_breaks=1) == 0 |
| 61 | + assert _check_version_bump("1.5.3", "2.0.0", total_breaks=10) == 0 |
| 62 | + |
| 63 | + |
| 64 | +def test_patch_bump_with_breaks_fails(): |
| 65 | + """PATCH bump should fail when there are breaking changes.""" |
| 66 | + assert _check_version_bump("1.0.0", "1.0.1", total_breaks=1) == 1 |
| 67 | + assert _check_version_bump("1.5.3", "1.5.4", total_breaks=1) == 1 |
| 68 | + |
| 69 | + |
| 70 | +def test_same_version_with_breaks_fails(): |
| 71 | + """Same version should fail when there are breaking changes.""" |
| 72 | + assert _check_version_bump("1.0.0", "1.0.0", total_breaks=1) == 1 |
| 73 | + |
| 74 | + |
| 75 | +def test_prerelease_versions(): |
| 76 | + """Pre-release versions should work correctly.""" |
| 77 | + # 1.1.0a1 has minor=1, so it satisfies minor bump from 1.0.0 |
| 78 | + assert _check_version_bump("1.0.0", "1.1.0a1", total_breaks=1) == 0 |
| 79 | + # 1.0.1a1 is still a patch bump |
| 80 | + assert _check_version_bump("1.0.0", "1.0.1a1", total_breaks=1) == 1 |
0 commit comments