Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.0 KB
Raw Blame
import subprocess
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts/ci/check_breaking_release.py"
sys.path.insert(0, str(SCRIPT.parent))
sys.path.insert(0, str(ROOT / ".github/scripts"))

import check_breaking_release  # noqa: E402
import release_package  # noqa: E402


@pytest.mark.parametrize(
    "version,normalized,major",
    [
        ("2.0.0-post1", "2.0.0.post1", False),
        ("2.0.0-1", "2.0.0.post1", False),
        ("2.0.0.post1", "2.0.0.post1", False),
        ("0.5.0.post1", "0.5.0.post1", False),
        ("0.5.0.post2", "0.5.0.post2", False),
        ("2.0.0_rev_2", "2.0.0.post2", False),
        ("2.0.0r", "2.0.0.post0", False),
        ("2.0.0rc1.post2.dev3", "2.0.0rc1.post2.dev3", False),
        ("2.0.0+local", "2.0.0+local", False),
        ("2.0.0rc1+build.1", "2.0.0rc1+build.1", False),
        ("0!2.0.0", "2.0.0", False),
        ("1!2.0.0", "1!2.0.0", False),
        ("2.0.0.0", "2.0.0.0", False),
        ("2.0.0.1", "2.0.0.1", False),
        ("1.0.0", "1.0.0", True),
        ("12.0.0", "12.0.0", True),
        ("0.1.0", "0.1.0", True),
        (" \tv2.0.0\n", "2.0.0", True),
        ("V02.00.00_RC_01", "2.0.0rc1", True),
        ("2.0.0-rc1", "2.0.0rc1", True),
        ("2.0.0rc1", "2.0.0rc1", True),
        ("2.0.0alpha", "2.0.0a0", True),
        ("2.0.0-a.2", "2.0.0a2", True),
        ("2.0.0.beta-2", "2.0.0b2", True),
        ("2.0.0b3", "2.0.0b3", True),
        ("2.0.0preview1", "2.0.0rc1", True),
        ("2.0.0pre", "2.0.0rc0", True),
        ("0.5.0c1", "0.5.0rc1", True),
        ("2.0.0-dev", "2.0.0.dev0", True),
        ("2.0.0.dev5", "2.0.0.dev5", True),
        ("2.0.0rc1.dev2", "2.0.0rc1.dev2", True),
        ("1.11.0rc1", "1.11.0rc1", False),
        ("2.0.1", "2.0.1", False),
        ("0.5.1rc1", "0.5.1rc1", False),
        ("0.0.0", "0.0.0", False),
    ],
)
def test_major_boundary_matches_publisher_version(
    version: str, normalized: str, major: bool
) -> None:
    assert release_package.normalize_version(version) == normalized
    assert check_breaking_release.is_major_release(version) is major


# CPython's int() string-conversion limit: a ≥4301-digit component makes
# the conversion itself raise ValueError instead of producing a number.
_HUGE_DIGITS = 4301
_HUGE_VERSION = "9" * _HUGE_DIGITS + ".0.0"


def test_huge_digit_component_is_refused_by_returning_false() -> None:
    """The docstring's refuse-by-False contract holds for huge digits.

    ``int()`` on a ≥4301-digit string raises ``ValueError`` (CPython's
    conversion limit), so the guard must refuse via its designed
    ``return False`` path — not escape as a traceback. The guard stays
    fail-closed either way, but the documented contract is return False.
    """
    assert check_breaking_release.is_major_release(_HUGE_VERSION) is False


@pytest.mark.parametrize(
    "version",
    [
        "2.0.0garbage",
        "2.0.0-",
        "2.0.0-rc1-extra",
        "2.0.0rc1.2",
        "2.0.0rc1rc2",
        "2.0.0--rc1",
        "2.0.0dev1rc1",
        "2.0.0\nrc1",
        "2.0.0..dev1",
        "\u0662.0.0",
        "2.\uff10.0",
        "2.0.0rc\u0661",
    ],
)
def test_malformed_publisher_versions_are_not_major(version: str) -> None:
    with pytest.raises(ValueError, match="Invalid package version"):
        release_package.normalize_version(version)
    assert check_breaking_release.is_major_release(version) is False


@pytest.mark.parametrize("pending", [True, False])
@pytest.mark.parametrize(
    "version,major",
    [
        ("2.0.0-post1", False),
        ("2.0.0-1", False),
        ("2.0.0-rc1-extra", False),
        ("2.0.0.1", False),
        ("2.0.0", True),
        ("0.5.0", True),
        ("2.0.0-rc1", True),
        ("2.0.0rc1", True),
    ],
)
def test_cli_enforces_full_version_when_breaking_changes_are_pending(
    tmp_path: Path, pending: bool, version: str, major: bool
) -> None:
    fragment = tmp_path / "7203.breaking.md"
    if pending:
        fragment.write_text("Breaking change\n", encoding="utf-8")

    result = subprocess.run(
        [
            sys.executable,
            "-I",
            "-S",
            str(SCRIPT),
            "--pyproject",
            str(ROOT / "pyproject.toml"),
            "--changelog-dir",
            str(tmp_path),
            "version",
            version,
        ],
        capture_output=True,
        text=True,
        check=False,
    )

    assert result.returncode == (1 if pending and not major else 0)
    assert result.stderr == ""
    if not pending:
        assert result.stdout == "No breaking changelog fragments are pending.\n"
    elif major:
        assert result.stdout == (
            f"Version {version} is a major release; breaking fragments allowed.\n"
        )
    else:
        assert result.stdout.startswith(
            f"::error::Version {version} is not a major release,"
        )
        assert fragment.name in result.stdout


def test_cli_pending_fragments_with_huge_version_fails_closed(
    tmp_path: Path,
) -> None:
    """Pending fragments + a huge-digit version exit 1 via ::error::.

    The version's first component exceeds CPython's ``int()`` string
    conversion limit, so a missing ``ValueError`` guard in
    ``is_major_release`` would surface as a traceback on stderr instead
    of the designed ``::error::`` annotation the release workflow
    reads.
    """
    fragment = tmp_path / "7332.breaking.md"
    fragment.write_text("Breaking change\n", encoding="utf-8")

    result = subprocess.run(
        [
            sys.executable,
            "-I",
            "-S",
            str(SCRIPT),
            "--pyproject",
            str(ROOT / "pyproject.toml"),
            "--changelog-dir",
            str(tmp_path),
            "version",
            _HUGE_VERSION,
        ],
        capture_output=True,
        text=True,
        check=False,
    )

    assert result.returncode == 1
    assert result.stderr == ""
    assert result.stdout.startswith(
        f"::error::Version {_HUGE_VERSION} is not a major release,"
    )
    assert fragment.name in result.stdout