From d91f88f7630b900c3cfb131abb0c1db6e3a27e31 Mon Sep 17 00:00:00 2001 From: Arthur Pastel Date: Thu, 19 Sep 2024 19:28:56 +0200 Subject: [PATCH] feat: also store the semver version --- .github/workflows/ci.yml | 2 +- pyproject.toml | 3 ++- scripts/generate_semver.py | 41 +++++++++++++++++++++++++++++++++ scripts/release.sh | 1 + src/pytest_codspeed/__init__.py | 2 ++ tests/test_generate_semver.py | 19 +++++++++++++++ 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 scripts/generate_semver.py create mode 100644 tests/test_generate_semver.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8247e14..1ee2804 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: name: Install valgrind run: sudo apt-get install valgrind -y - name: Install dependencies with pytest${{ matrix.pytest-version }} - run: pip install .[dev,compat] "pytest${{ matrix.pytest-version }}" + run: pip install .[dev,compat,build] "pytest${{ matrix.pytest-version }}" - if: matrix.config != 'pytest-benchmark' name: Uninstall pytest-benchmark run: pip uninstall -y pytest-benchmark diff --git a/pyproject.toml b/pyproject.toml index 2095bf5..85e0144 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,7 @@ dependencies = [ ] [project.optional-dependencies] +build = ["semver>=3.0.2"] lint = ["mypy ~= 1.11.2", "ruff ~= 0.6.5"] compat = [ "pytest-benchmark ~= 4.0.0", @@ -94,7 +95,7 @@ float_to_top = true [tool.pytest.ini_options] addopts = "--ignore=tests/benchmarks --ignore=tests/examples --ignore=tests/benchmarks/TheAlgorithms" filterwarnings = ["ignore::DeprecationWarning:pytest_benchmark.utils.*:"] -pythonpath = ["tests/benchmarks/TheAlgorithms"] +pythonpath = ["tests/benchmarks/TheAlgorithms", "./scripts"] [tool.coverage.run] branch = true diff --git a/scripts/generate_semver.py b/scripts/generate_semver.py new file mode 100644 index 0000000..98372b2 --- /dev/null +++ b/scripts/generate_semver.py @@ -0,0 +1,41 @@ +""" +This script converts a PyPI version string to a semantic version string and updates the +__semver_version__ variable in the __init__.py file of the pytest_codspeed package. +""" + +import re +from pathlib import Path + +from packaging.version import Version as PyPIVersion +from semver import Version as SemVerVersion + + +def pypi_version_to_semver(pypi_version_str: str) -> str: + py_version = PyPIVersion(pypi_version_str) + if py_version.epoch != 0: + raise ValueError("Can't convert an epoch to semver") + if py_version.post is not None: + raise ValueError("Can't convert a post part to semver") + + pre = None if not py_version.pre else "".join([str(i) for i in py_version.pre]) + semver = SemVerVersion(*py_version.release, prerelease=pre, build=py_version.dev) + return str(semver) + + +def main(): + from pytest_codspeed import __version__ as pypi_version + + semver_version = pypi_version_to_semver(pypi_version) + init_file_path = Path("./src/pytest_codspeed/__init__.py") + content = init_file_path.read_text() + + content = re.sub( + r'__semver_version__\s*=\s*".*"', + f'__semver_version__ = "{semver_version}"', + content, + ) + init_file_path.write_text(content) + + +if __name__ == "__main__": + main() diff --git a/scripts/release.sh b/scripts/release.sh index dd769bd..6db80b7 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -8,6 +8,7 @@ if [ $# -ne 1 ]; then fi hatch version $1 +python scripts/generate_semver.py NEW_VERSION=$(hatch version) git add src/pytest_codspeed/__init__.py # Fail if there are any unstaged changes left diff --git a/src/pytest_codspeed/__init__.py b/src/pytest_codspeed/__init__.py index 2bc9a83..155891d 100644 --- a/src/pytest_codspeed/__init__.py +++ b/src/pytest_codspeed/__init__.py @@ -1,4 +1,6 @@ __version__ = "3.0.0b0" +# We also have the semver version since __version__ is not semver compliant +__semver_version__ = "3.0.0-b0" from .plugin import BenchmarkFixture diff --git a/tests/test_generate_semver.py b/tests/test_generate_semver.py new file mode 100644 index 0000000..b2a8c24 --- /dev/null +++ b/tests/test_generate_semver.py @@ -0,0 +1,19 @@ +import pytest +from generate_semver import pypi_version_to_semver + + +def test_pypi_version_to_semver_valid(): + # Test cases for valid PyPI version strings + assert pypi_version_to_semver("1.0.0") == "1.0.0" + assert pypi_version_to_semver("1.0.0a1") == "1.0.0-a1" + assert pypi_version_to_semver("1.0.0b1") == "1.0.0-b1" + assert pypi_version_to_semver("1.0.0rc1") == "1.0.0-rc1" + + +def test_pypi_version_to_semver_invalid(): + # Test cases for invalid PyPI version strings that should raise ValueError + with pytest.raises(ValueError, match="Can't convert an epoch to semver"): + pypi_version_to_semver("1!1.0.0") + + with pytest.raises(ValueError, match="Can't convert a post part to semver"): + pypi_version_to_semver("1.0.0.post")