From 0c97c4857df716abb4f7b390f487b0bec27a217f Mon Sep 17 00:00:00 2001 From: David Hotham Date: Fri, 2 Sep 2022 12:11:10 +0100 Subject: [PATCH] pay attention to package sources in export --- src/poetry_plugin_export/walker.py | 1 + tests/test_exporter.py | 77 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/poetry_plugin_export/walker.py b/src/poetry_plugin_export/walker.py index 6e613fd..53c3fe6 100644 --- a/src/poetry_plugin_export/walker.py +++ b/src/poetry_plugin_export/walker.py @@ -229,6 +229,7 @@ def get_locked_package( for package in candidates if package.python_constraint.allows_all(dependency.python_constraint) and dependency.constraint.allows(package.version) + and (dependency.source_type is None or dependency.is_same_source_as(package)) ] # If we have an overlapping candidate, we must use it. diff --git a/tests/test_exporter.py b/tests/test_exporter.py index a056fae..6b83d82 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -18,6 +18,8 @@ from poetry_plugin_export.exporter import Exporter from tests.markers import MARKER_CPYTHON +from tests.markers import MARKER_DARWIN +from tests.markers import MARKER_LINUX from tests.markers import MARKER_PY from tests.markers import MARKER_PY27 from tests.markers import MARKER_PY36 @@ -2321,3 +2323,78 @@ def test_exporter_omits_unwanted_extras( exporter.export("requirements.txt", Path(tmp_dir), io) assert io.fetch_output() == "\n".join(expected) + "\n" + + +def test_exporter_respects_package_sources(tmp_dir: str, poetry: Poetry) -> None: + poetry.locker.mock_lock_data( # type: ignore[attr-defined] + { + "package": [ + { + "name": "foo", + "python-versions": ">=3.6", + "version": "1.0.0", + "category": "main", + "optional": False, + "dependencies": {}, + "source": { + "type": "url", + "url": "https://example.com/foo-darwin.whl", + }, + }, + { + "name": "foo", + "python-versions": ">=3.6", + "version": "1.0.0", + "category": "main", + "optional": False, + "dependencies": {}, + "source": { + "type": "url", + "url": "https://example.com/foo-linux.whl", + }, + }, + ], + "metadata": { + "lock-version": "1.1", + "python-versions": "^3.6", + "content-hash": ( + "832b13a88e5020c27cbcd95faa577bf0dbf054a65c023b45dc9442b640d414e6" + ), + "hashes": { + "foo": [], + }, + }, + } + ) + root = poetry.package.with_dependency_groups([], only=True) + root.python_versions = "^3.6" + root.add_dependency( + Factory.create_dependency( + name="foo", + constraint={ + "url": "https://example.com/foo-linux.whl", + "platform": "linux", + }, + ) + ) + root.add_dependency( + Factory.create_dependency( + name="foo", + constraint={ + "url": "https://example.com/foo-darwin.whl", + "platform": "darwin", + }, + ) + ) + poetry._package = root + + io = BufferedIO() + exporter = Exporter(poetry) + exporter.export("requirements.txt", Path(tmp_dir), io) + + expected = f"""\ +foo @ https://example.com/foo-darwin.whl ; {MARKER_PY36} and {MARKER_DARWIN} +foo @ https://example.com/foo-linux.whl ; {MARKER_PY36} and {MARKER_LINUX} +""" + + assert io.fetch_output() == expected