diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3f73e73..55c6af4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,6 +8,9 @@ Changelog - Drop support for Python 3.8 +- Normalize OCI and Hugging Face versions consistently when the package type + contains uppercase characters or is supplied as bytes. + - Fix type hint for ``BasePurlType.allowed_qualifiers`` https://github.com/package-url/packageurl-python/pull/213 diff --git a/src/packageurl/__init__.py b/src/packageurl/__init__.py index 3bfae87..c1ee909 100644 --- a/src/packageurl/__init__.py +++ b/src/packageurl/__init__.py @@ -385,7 +385,7 @@ def normalize( type_norm = normalize_type(type, encode) namespace_norm = normalize_namespace(namespace, type_norm, encode) name_norm = normalize_name(name, qualifiers, type_norm, encode) - version_norm = normalize_version(version, type, encode) + version_norm = normalize_version(version, type_norm, encode) qualifiers_norm = normalize_qualifiers(qualifiers, encode) subpath_norm = normalize_subpath(subpath, encode) return type_norm, namespace_norm, name_norm, version_norm, qualifiers_norm, subpath_norm diff --git a/tests/test_packageurl.py b/tests/test_packageurl.py index 2c36549..47ab1f5 100644 --- a/tests/test_packageurl.py +++ b/tests/test_packageurl.py @@ -29,11 +29,36 @@ import re import unittest +import pytest + from packageurl import PackageURL from packageurl import normalize from packageurl import normalize_qualifiers +@pytest.mark.parametrize("ptype", ["OCI", "Oci", b"oci", "HuggingFace", b"huggingface"]) +def test_version_normalization_uses_normalized_type(ptype): + purl = PackageURL(type=ptype, name="example", version="ABCDEF") + + assert purl.version == "abcdef" + assert purl == PackageURL.from_string(purl.to_string()) + + +@pytest.mark.parametrize("encode", [True, False, None]) +@pytest.mark.parametrize("ptype", ["OCI", b"oci", "HuggingFace", b"huggingface"]) +def test_normalize_version_with_normalized_type(ptype, encode): + normalized = normalize(ptype, None, "example", "ABCDEF", None, None, encode=encode) + + assert normalized[3] == "abcdef" + + +def test_case_sensitive_version_is_preserved_with_normalized_type(): + purl = PackageURL(type="Maven", name="example", version="1.0-RC1") + + assert purl.version == "1.0-RC1" + assert purl == PackageURL.from_string(purl.to_string()) + + def create_test_function( description, purl,