Skip to content

Commit

Permalink
feat: export without urls (#4763)
Browse files Browse the repository at this point in the history
Export requirements.txt without index URLs

Co-authored-by: DBM012 <[email protected]>
  • Loading branch information
DustinMoriarty and DBM012 authored Nov 15, 2021
1 parent d3bc0eb commit a694aad
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ Only the `requirements.txt` format is currently supported.
* `--dev`: Include development dependencies.
* `--extras (-E)`: Extra sets of dependencies to include.
* `--without-hashes`: Exclude hashes from the exported file.
* `--without-urls`: Exclude source repository urls from the exported file.
* `--with-credentials`: Include credentials for extra indices.

## env
Expand Down
6 changes: 6 additions & 0 deletions src/poetry/console/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class ExportCommand(Command):
),
option("output", "o", "The name of the output file.", flag=False),
option("without-hashes", None, "Exclude hashes from the exported file."),
option(
"without-urls",
None,
"Exclude source repository urls from the exported file.",
),
option("dev", None, "Include development dependencies."),
option(
"extras",
Expand Down Expand Up @@ -71,4 +76,5 @@ def handle(self) -> None:
dev=self.option("dev"),
extras=self.option("extras"),
with_credentials=self.option("with-credentials"),
with_urls=not self.option("without-urls"),
)
5 changes: 4 additions & 1 deletion src/poetry/utils/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def export(
dev: bool = False,
extras: Optional[Union[bool, Sequence[str]]] = None,
with_credentials: bool = False,
with_urls: bool = True,
) -> None:
if fmt not in self.ACCEPTED_FORMATS:
raise ValueError(f"Invalid export format: {fmt}")
Expand All @@ -45,6 +46,7 @@ def export(
dev=dev,
extras=extras,
with_credentials=with_credentials,
with_urls=with_urls,
)

def _export_requirements_txt(
Expand All @@ -55,6 +57,7 @@ def _export_requirements_txt(
dev: bool = False,
extras: Optional[Union[bool, Sequence[str]]] = None,
with_credentials: bool = False,
with_urls: bool = True,
) -> None:
indexes = set()
content = ""
Expand Down Expand Up @@ -122,7 +125,7 @@ def _export_requirements_txt(
content += "\n".join(sorted(dependency_lines))
content += "\n"

if indexes:
if indexes and with_urls:
# If we have extra indexes, we add them to the beginning of the output
indexes_header = ""
for index in sorted(indexes):
Expand Down
24 changes: 24 additions & 0 deletions tests/console/commands/test_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest.mock import ANY
from unittest.mock import Mock

import pytest

from poetry.console.commands.export import Exporter
from tests.helpers import get_package


Expand Down Expand Up @@ -110,3 +114,23 @@ def test_export_includes_extras_by_flag(tester, do_lock):
foo==1.0.0
"""
assert expected == tester.io.fetch_output()


def test_export_with_urls(monkeypatch, tester, poetry):
"""
We are just validating that the option gets passed. The option itself is tested in
the Exporter test.
"""
mock_export = Mock()
monkeypatch.setattr(Exporter, "export", mock_export)
tester.execute("--without-urls")
mock_export.assert_called_once_with(
ANY,
ANY,
ANY,
dev=False,
extras=[],
with_credentials=False,
with_hashes=True,
with_urls=False,
)
58 changes: 58 additions & 0 deletions tests/utils/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,64 @@ def test_exporter_exports_requirements_txt_with_legacy_packages(tmp_dir, poetry)
assert expected == content


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

exporter = Exporter(poetry)

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

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

expected = """\
bar==4.5.6 \\
--hash=sha256:67890
foo==1.2.3 \\
--hash=sha256:12345
"""

assert expected == content


def test_exporter_exports_requirements_txt_with_legacy_packages_trusted_host(
tmp_dir, poetry
):
Expand Down

0 comments on commit a694aad

Please sign in to comment.