Skip to content

Commit

Permalink
Make index urls a set to avoid exporting duplicates to requirements.t…
Browse files Browse the repository at this point in the history
…xt (#1610)

* Make index urls a set to avoid exporting duplicates to requirements.txt

Fixes #1434

* Add test for deduplication of sources in exporter

* Holy test names, blackman.

* Sort indexes for consistent output with multiple extra indexes
  • Loading branch information
StephenBrown2 authored and sdispater committed Nov 21, 2019
1 parent 3cf8b31 commit b29999c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
9 changes: 4 additions & 5 deletions poetry/utils/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _export_requirements_txt(
extras=None,
with_credentials=False,
): # type: (Path, Union[IO, str], bool, bool, bool) -> None
indexes = []
indexes = set()
content = ""
packages = self._poetry.locker.locked_repository(dev).packages

Expand Down Expand Up @@ -108,7 +108,7 @@ def _export_requirements_txt(
package.source_type not in {"git", "directory", "file", "url"}
and package.source_url
):
indexes.append(package.source_url)
indexes.add(package.source_url)

if package.files and with_hashes:
hashes = []
Expand All @@ -134,10 +134,9 @@ def _export_requirements_txt(
content += line

if indexes:
# If we have extra indexes, we add them to the begin
# of the output
# If we have extra indexes, we add them to the beginning of the output
indexes_header = ""
for index in indexes:
for index in sorted(indexes):
repository = [
r
for r in self._poetry.pool.repositories
Expand Down
80 changes: 80 additions & 0 deletions tests/utils/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,86 @@ def test_exporter_exports_requirements_txt_with_legacy_packages(tmp_dir, poetry)
assert expected == content


def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_sources(
tmp_dir, poetry
):
poetry.pool.add_repository(
LegacyRepository(
"custom",
"https://example.com/simple",
auth=Auth("https://example.com/simple", "foo", "bar"),
)
)
poetry.pool.add_repository(LegacyRepository("custom", "https://foobaz.com/simple",))
poetry.locker.mock_lock_data(
{
"package": [
{
"name": "foo",
"version": "1.2.3",
"category": "main",
"optional": False,
"python-versions": "*",
"source": {
"type": "legacy",
"url": "https://example.com/simple",
"reference": "",
},
},
{
"name": "bar",
"version": "4.5.6",
"category": "dev",
"optional": False,
"python-versions": "*",
"source": {
"type": "legacy",
"url": "https://example.com/simple",
"reference": "",
},
},
{
"name": "baz",
"version": "7.8.9",
"category": "dev",
"optional": False,
"python-versions": "*",
"source": {
"type": "legacy",
"url": "https://foobaz.com/simple",
"reference": "",
},
},
],
"metadata": {
"python-versions": "*",
"content-hash": "123456789",
"hashes": {"foo": ["12345"], "bar": ["67890"], "baz": ["24680"]},
},
}
)
exporter = Exporter(poetry)

exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt", dev=True)

with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f:
content = f.read()

expected = """\
--extra-index-url https://example.com/simple
--extra-index-url https://foobaz.com/simple
bar==4.5.6 \\
--hash=sha256:67890
baz==7.8.9 \\
--hash=sha256:24680
foo==1.2.3 \\
--hash=sha256:12345
"""

assert expected == content


def test_exporter_exports_requirements_txt_with_legacy_packages_and_credentials(
tmp_dir, poetry, config
):
Expand Down

0 comments on commit b29999c

Please sign in to comment.