From af555e9826aa6c97899107323bc3d3c487aa7d41 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 26 Jan 2024 10:45:53 +0800 Subject: [PATCH] fix: concatenate the markers with extra marker Signed-off-by: Frost Ming --- pyproject_metadata/__init__.py | 14 +++++++++++--- tests/test_rfc822.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pyproject_metadata/__init__.py b/pyproject_metadata/__init__.py index 70e33a7..ed2366b 100644 --- a/pyproject_metadata/__init__.py +++ b/pyproject_metadata/__init__.py @@ -337,9 +337,17 @@ def _build_extra_req( ) -> packaging.requirements.Requirement: # append or add our extra marker requirement = copy.copy(requirement) - requirement.marker = packaging.markers.Marker( - f'{requirement.marker} and extra == "{extra}"' if requirement.marker else f'extra == "{extra}"', - ) + if requirement.marker: + if "or" in requirement.marker._markers: + requirement.marker = packaging.markers.Marker( + f'({requirement.marker}) and extra == "{extra}"' + ) + else: + requirement.marker = packaging.markers.Marker( + f'{requirement.marker} and extra == "{extra}"' + ) + else: + requirement.marker = packaging.markers.Marker(f'extra == "{extra}"') return requirement @staticmethod diff --git a/tests/test_rfc822.py b/tests/test_rfc822.py index 9fde183..ac286ac 100644 --- a/tests/test_rfc822.py +++ b/tests/test_rfc822.py @@ -144,3 +144,26 @@ def test_body(): finibus nulla. Donec sit amet ante in neque pulvinar faucibus sed nec justo. Fusce hendrerit massa libero, sit amet pulvinar magna tempor quis. ''') + + +def test_convert_optional_dependencies(): + metadata = pyproject_metadata.StandardMetadata.from_pyproject( + { + "project": { + "name": "example", + "version": "0.1.0", + "optional-dependencies": { + "test": [ + 'foo; os_name == "nt" or sys_platform == "win32"', + 'bar; os_name == "posix" and sys_platform == "linux"', + ], + }, + }, + } + ) + message = metadata.as_rfc822() + requires = message.headers["Requires-Dist"] + assert requires == [ + 'foo; (os_name == "nt" or sys_platform == "win32") and extra == "test"', + 'bar; os_name == "posix" and sys_platform == "linux" and extra == "test"', + ]