diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2756151..aab4aa7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ----- diff --git a/src/dependency_groups/__main__.py b/src/dependency_groups/__main__.py index d20326f..eb3f922 100644 --- a/src/dependency_groups/__main__.py +++ b/src/dependency_groups/__main__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import sys from ._argparse_compat import ArgumentParser @@ -5,7 +7,7 @@ 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+", @@ -38,7 +40,7 @@ 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) @@ -46,13 +48,12 @@ def main() -> None: 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) diff --git a/tests/test_main_cli.py b/tests/test_main_cli.py new file mode 100644 index 0000000..7bf586a --- /dev/null +++ b/tests/test_main_cli.py @@ -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 + + +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"