Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use a true semver version #44

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion src/pytest_codspeed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
__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

__all__ = ["BenchmarkFixture", "__version__"]
__all__ = ["BenchmarkFixture", "__version__", "__semver_version__"]
4 changes: 2 additions & 2 deletions src/pytest_codspeed/instruments/valgrind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from typing import TYPE_CHECKING

from pytest_codspeed import __version__
from pytest_codspeed import __semver_version__
from pytest_codspeed.instruments import Instrument
from pytest_codspeed.instruments.valgrind._wrapper import get_lib

Expand All @@ -30,7 +30,7 @@ def __init__(self, config: CodSpeedConfig) -> None:
if self.should_measure:
self.lib = get_lib()
self.lib.dump_stats_at(
f"Metadata: pytest-codspeed {__version__}".encode("ascii")
f"Metadata: pytest-codspeed {__semver_version__}".encode("ascii")
)
if SUPPORTS_PERF_TRAMPOLINE:
sys.activate_stack_trampoline("perf") # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_codspeed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sysconfig
from pathlib import Path

from pytest_codspeed import __version__
from pytest_codspeed import __semver_version__

if sys.version_info < (3, 10):
import importlib_metadata as importlib_metadata
Expand Down Expand Up @@ -51,7 +51,7 @@ def get_environment_metadata() -> dict[str, dict]:
return {
"creator": {
"name": "pytest-codspeed",
"version": __version__,
"version": __semver_version__,
"pid": os.getpid(),
},
"python": {
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")
Loading