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

Fix 1.0.0 issues #50

Merged
merged 14 commits into from
Apr 11, 2022
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
5 changes: 3 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class ExportCommand(InstallerCommand):
option("with-credentials", None, "Include credentials for extra indices."),
]

@property
def non_optional_groups(self) -> set[str]:
return {"default"}

def handle(self) -> None:
fmt = self.option("format")

Expand Down
33 changes: 10 additions & 23 deletions src/poetry_plugin_export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, poetry: Poetry) -> None:
self._with_credentials = False
self._with_urls = True
self._extras: list[str] = []
self._groups: Iterable[str] | None = None
self._groups: Iterable[str] = ["default"]

@classmethod
def is_format_supported(cls, fmt: str) -> bool:
Expand Down Expand Up @@ -78,14 +78,11 @@ def _export_requirements_txt(self, cwd: Path, output: IO | str) -> None:
content = ""
dependency_lines = set()

if self._groups is not None:
root = self._poetry.package.with_dependency_groups(
list(self._groups), only=True
)
else:
root = self._poetry.package.without_optional_dependency_groups()
root = self._poetry.package.with_dependency_groups(
list(self._groups), only=True
)

locked_repository = self._poetry.locker.locked_repository(True)
locked_repository = self._poetry.locker.locked_repository()

pool = Pool(ignore_repository_names=True)
pool.add_repository(locked_repository)
Expand All @@ -98,17 +95,9 @@ def _export_requirements_txt(self, cwd: Path, output: IO | str) -> None:
ops = solver.solve().calculate_operations()
packages = sorted((op.package for op in ops), key=lambda pkg: pkg.name)

# Get project dependencies.
if self._groups is not None:
abn marked this conversation as resolved.
Show resolved Hide resolved
root_package = self._poetry.package.with_dependency_groups(
list(self._groups), only=True
)
else:
root_package = self._poetry.package.without_optional_dependency_groups()

for dependency_package in self._poetry.locker.get_project_dependency_packages(
project_requires=root_package.all_requires,
project_python_marker=root_package.python_marker,
project_requires=root.all_requires,
project_python_marker=root.python_marker,
extras=self._extras,
):
line = ""
Expand Down Expand Up @@ -161,11 +150,9 @@ def _export_requirements_txt(self, cwd: Path, output: IO | str) -> None:

hashes.append(f"{algorithm}:{h}")

if hashes:
line += " \\\n"
for i, h in enumerate(hashes):
suffix = " \\\n" if i < len(hashes) - 1 else ""
line += f" --hash={h}{suffix}"
for h in hashes:
line += f" \\\n --hash={h}"
abn marked this conversation as resolved.
Show resolved Hide resolved

dependency_lines.add(line)

content += "\n".join(sorted(dependency_lines))
Expand Down
47 changes: 43 additions & 4 deletions tests/command/test_command_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
foo = "^1.0"
bar = { version = "^1.1", optional = true }

[tool.poetry.group.dev.dependencies]
baz = "^2.0"

[tool.poetry.group.opt]
optional = true

[tool.poetry.group.opt.dependencies]
opt = "^2.2"


[tool.poetry.extras]
feature_bar = ["bar"]
"""
Expand All @@ -59,6 +69,8 @@
def setup(repo: Repository) -> None:
repo.add_package(Package("foo", "1.0.0"))
repo.add_package(Package("bar", "1.1.0"))
repo.add_package(Package("baz", "2.0.0"))
repo.add_package(Package("opt", "2.2.0"))


@pytest.fixture
Expand Down Expand Up @@ -88,7 +100,7 @@ def _export_requirements(tester: CommandTester, poetry: Poetry) -> None:
foo==1.0.0 ; {MARKER_PY}
"""

assert expected == content
assert content == expected


def test_export_exports_requirements_txt_file_locks_if_no_lock_file(
Expand Down Expand Up @@ -116,7 +128,7 @@ def test_export_prints_to_stdout_by_default(tester: CommandTester, do_lock: None
expected = f"""\
foo==1.0.0 ; {MARKER_PY}
"""
assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_export_uses_requirements_txt_format_by_default(
Expand All @@ -126,7 +138,34 @@ def test_export_uses_requirements_txt_format_by_default(
expected = f"""\
foo==1.0.0 ; {MARKER_PY}
"""
assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


@pytest.mark.parametrize(
"options, expected",
[
("", f"foo==1.0.0 ; {MARKER_PY}\n"),
("--with dev", f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n"),
("--with opt", f"foo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ; {MARKER_PY}\n"),
(
"--with dev,opt",
f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\nopt==2.2.0 ;"
f" {MARKER_PY}\n",
),
("--without default", "\n"),
("--without dev", f"foo==1.0.0 ; {MARKER_PY}\n"),
("--without opt", f"foo==1.0.0 ; {MARKER_PY}\n"),
("--without default,dev,opt", "\n"),
("--only default", f"foo==1.0.0 ; {MARKER_PY}\n"),
("--only dev", f"baz==2.0.0 ; {MARKER_PY}\n"),
("--only default,dev", f"baz==2.0.0 ; {MARKER_PY}\nfoo==1.0.0 ; {MARKER_PY}\n"),
],
)
def test_export_groups(
tester: CommandTester, do_lock: None, options: str, expected: str
):
tester.execute(options)
assert tester.io.fetch_output() == expected


def test_export_includes_extras_by_flag(tester: CommandTester, do_lock: None):
Expand All @@ -135,7 +174,7 @@ def test_export_includes_extras_by_flag(tester: CommandTester, do_lock: None):
bar==1.1.0 ; {MARKER_PY}
foo==1.0.0 ; {MARKER_PY}
"""
assert expected == tester.io.fetch_output()
assert tester.io.fetch_output() == expected


def test_export_with_urls(
Expand Down
Loading