Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/export without urls #4763

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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