-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |