Skip to content

Commit

Permalink
feat: also store the semver version
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Sep 19, 2024
1 parent 630460a commit d91f88f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions scripts/generate_semver.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/pytest_codspeed/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 19 additions & 0 deletions tests/test_generate_semver.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit d91f88f

Please # to comment.