From dfd8e4bb8eb3dcb8c2e513dbd5fcae2d5cb5d8da Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Wed, 8 Jun 2022 00:03:30 +0200 Subject: [PATCH] Follow most recent packaging specification for wheel name normalization (#394) * test(masonry): add tests for `escape_*` methods * feat(masonry): follow newer PyPA specification for wheel name As per https://packaging.python.org/en/latest/specifications/binary-distribution-format/#binary-distribution-format, the specification replaces the original PEP 427 specification. The specification for the distribution name is https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode. * refactor(masonry): indicate that `escape_name` is for generated wheels only * doc(masonry): improve `escape_name` documentation Co-authored-by: Bjorn Neergaard --- src/poetry/core/masonry/utils/helpers.py | 7 +++-- tests/masonry/utils/test_helpers.py | 37 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/masonry/utils/test_helpers.py diff --git a/src/poetry/core/masonry/utils/helpers.py b/src/poetry/core/masonry/utils/helpers.py index 1dddfa51a..b79089f3e 100644 --- a/src/poetry/core/masonry/utils/helpers.py +++ b/src/poetry/core/masonry/utils/helpers.py @@ -29,5 +29,8 @@ def escape_version(version: str) -> str: def escape_name(name: str) -> str: - """Escaped wheel name as specified in :pep:`427#escaping-and-unicode`.""" - return re.sub(r"[^\w\d.]+", "_", name, flags=re.UNICODE) + """ + Escaped wheel name as specified in https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode. + This function should only be used for the generation of artifact names, and not to normalize or filter existing artifact names. + """ + return re.sub(r"[-_.]+", "_", name, flags=re.UNICODE).lower() diff --git a/tests/masonry/utils/test_helpers.py b/tests/masonry/utils/test_helpers.py new file mode 100644 index 000000000..8be13c40a --- /dev/null +++ b/tests/masonry/utils/test_helpers.py @@ -0,0 +1,37 @@ +from __future__ import annotations + +import pytest + +from poetry.core.masonry.utils.helpers import escape_name +from poetry.core.masonry.utils.helpers import escape_version + + +@pytest.mark.parametrize( + "version,expected", + [ + ("1.2.3", "1.2.3"), + ("1.2.3_1", "1.2.3_1"), + ("1.2.3-1", "1.2.3_1"), + ("1.2.3-1", "1.2.3_1"), + ("2022.2", "2022.2"), + ("12.20.12-----451---14-1-4-41", "12.20.12_451_14_1_4_41"), + ("1.0b2.dev1", "1.0b2.dev1"), + ("1.0+abc.7", "1.0+abc.7"), + ], +) +def test_escape_version(version: str, expected: str) -> None: + assert escape_version(version) == expected + + +@pytest.mark.parametrize( + "name,expected", + [ + ("foo", "foo"), + ("foo-bar", "foo_bar"), + ("FOO-bAr", "foo_bar"), + ("foo.bar", "foo_bar"), + ("foo123-ba---.r", "foo123_ba_r"), + ], +) +def test_escape_name(name: str, expected: str) -> None: + assert escape_name(name) == expected