Skip to content

Commit

Permalink
command: restore default behavior, consistent behavior for --with/--w…
Browse files Browse the repository at this point in the history
…ithout, tests
  • Loading branch information
radoering committed Apr 11, 2022
1 parent 14180c7 commit b50bcc6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/poetry_plugin_export/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ class ExportCommand(InstallerCommand):
option("with-credentials", None, "Include credentials for extra indices."),
]

@property
def activated_groups(self) -> set[str]:
groups = {}

for key in {"with", "without", "only"}:
groups[key] = {
group.strip()
for groups in self.option(key)
for group in groups.split(",")
}

for opt, new, group in [
("dev", "with", "dev"),
]:
if self.io.input.has_option(opt) and self.option(opt):
self.line_error(
f"<warning>The `<fg=yellow;options=bold>--{opt}</>` option is"
f" deprecated, use the `<fg=yellow;options=bold>--{new} {group}</>`"
" notation instead.</warning>"
)
groups[new].add(group)

if groups["only"] and (groups["with"] or groups["without"]):
self.line_error(
"<warning>The `<fg=yellow;options=bold>--with</>` and "
"`<fg=yellow;options=bold>--without</>` options are ignored when used"
" along with the `<fg=yellow;options=bold>--only</>` option."
"</warning>"
)

return groups["only"] or {"default"}.union(groups["with"]).difference(
groups["without"]
)

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

Expand Down
39 changes: 39 additions & 0 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 @@ -129,6 +141,33 @@ def test_export_uses_requirements_txt_format_by_default(
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):
tester.execute("--format requirements.txt --extras feature_bar")
expected = f"""\
Expand Down

0 comments on commit b50bcc6

Please sign in to comment.