Skip to content
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 CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Unreleased
- Fix a bug in which ``pip-install-dependency-groups`` failed when the
resolved groups were empty. It now prints ``Nothing to install`` and exits
normally.
- `dependency-groups --list` now respects the `-o`/`--output` option.

1.3.1
-----
Expand Down
15 changes: 8 additions & 7 deletions src/dependency_groups/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import sys

from ._argparse_compat import ArgumentParser
from ._implementation import resolve
from ._toml_compat import tomllib


def main() -> None:
def main(*, argv: list[str] | None = None) -> None:
if tomllib is None:
print(
"Usage error: dependency-groups CLI requires tomli or Python 3.11+",
Expand Down Expand Up @@ -38,21 +40,20 @@ def main() -> None:
action="store_true",
help="List the available dependency groups",
)
args = parser.parse_args()
args = parser.parse_args(argv if argv is not None else sys.argv[1:])

with open(args.pyproject_file, "rb") as fp:
pyproject = tomllib.load(fp)

dependency_groups_raw = pyproject.get("dependency-groups", {})

if args.list:
print(*dependency_groups_raw.keys())
return
if not args.GROUP_NAME:
content = " ".join(dependency_groups_raw.keys())
elif not args.GROUP_NAME:
print("A GROUP_NAME is required", file=sys.stderr)
raise SystemExit(3)

content = "\n".join(resolve(dependency_groups_raw, *args.GROUP_NAME))
else:
content = "\n".join(resolve(dependency_groups_raw, *args.GROUP_NAME))

if args.output is None or args.output == "-":
print(content)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_main_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import dataclasses

import pytest

PYPROJECT = """\
[dependency-groups]
test = ["pytest"]
docs = ["sphinx"]
"""


@dataclasses.dataclass
class CLIResult:
code: int
stdout: str
stderr: str


@pytest.fixture
def run(capsys):
from dependency_groups.__main__ import main as cli_main

def _run(*argv):
try:
cli_main(argv=[str(arg) for arg in argv])
rc = 0
except SystemExit as e:
rc = e.code

stdio = capsys.readouterr()
return CLIResult(rc, stdio.out, stdio.err)

return _run

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll work up a follow-up changeset to combine these fixtures with the other test module.

I have some other CLI testing patterns I've found useful, like baking in a default assertion on the exit code.



def test_list_to_stdout(run, tmp_path):
tomlfile = tmp_path / "pyproject.toml"
tomlfile.write_text(PYPROJECT)

res = run("-f", tomlfile, "--list")
assert res.code == 0
assert res.stdout == "test docs\n"


def test_list_respects_output_file(run, tmp_path):
tomlfile = tmp_path / "pyproject.toml"
tomlfile.write_text(PYPROJECT)
outfile = tmp_path / "out.txt"

res = run("-f", tomlfile, "--list", "-o", outfile)
assert res.code == 0
assert res.stdout == ""
assert outfile.read_text() == "test docs\n"