diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 47cdcc5..82a23c5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,8 @@ Fixed ~~~~~ * The ``typing-extensions`` dependency is now also installed on Python 3.11, where it is required for ``typing_extensions.override``. +* Print generated and converted values when using ``python -m ulid``, matching the + ``ulid`` console command. * :meth:`.ULID.from_str` accepts lower case strings again, as the ULID spec defines the string representation as case insensitive. Lower case input has been rejected since 2.6.0. diff --git a/tests/test_cli.py b/tests/test_cli.py index 48984df..73af0ba 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,6 @@ +import subprocess +import sys + import pytest import ulid.__main__ as cli @@ -79,3 +82,49 @@ def test_build(option: str): ulid_out = ULID.from_str(output) if includes_timestamp: assert ulid_out.datetime == ulid.datetime + + +@pytest.mark.parametrize( + ("args", "stdin", "expected"), + [ + (["build", "--from-int", "0"], None, "00000000000000000000000000\n"), + (["build", "--from-int", "-"], "0\n", "00000000000000000000000000\n"), + (["show", "--int", "00000000000000000000000000"], None, "0\n"), + (["show", "--int", "-"], "00000000000000000000000000\n", "0\n"), + ], +) +def test_module_outputs_result(args: list[str], stdin: str | None, expected: str) -> None: + result = subprocess.run( + [sys.executable, "-m", "ulid", *args], + input=stdin, + capture_output=True, + text=True, + check=True, + timeout=10, + ) + assert result.stdout == expected + assert result.stderr == "" + + +@pytest.mark.parametrize("args", [[], ["--help"]]) +def test_module_help(args: list[str]) -> None: + result = subprocess.run( + [sys.executable, "-m", "ulid", *args], + capture_output=True, + text=True, + check=True, + timeout=10, + ) + assert result.stdout.startswith("usage: python -m ulid") + assert not result.stdout.endswith("None\n") + assert result.stderr == "" + + +def test_console_entrypoint( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + monkeypatch.setattr(sys, "argv", ["ulid", "build", "--from-int", "0"]) + cli.entrypoint() + captured = capsys.readouterr() + assert captured.out == "00000000000000000000000000\n" + assert captured.err == "" diff --git a/ulid/__main__.py b/ulid/__main__.py index 2c10bef..fc25d43 100644 --- a/ulid/__main__.py +++ b/ulid/__main__.py @@ -163,10 +163,10 @@ def show(args: argparse.Namespace) -> str: ).strip() -def entrypoint() -> None: # pragma: no cover - if (value := main(sys.argv[1:])) is not None: +def entrypoint(prog: str | None = None) -> None: # pragma: no cover + if (value := main(sys.argv[1:], prog)) is not None: print(value) # noqa: T201 if __name__ == "__main__": # pragma: no cover - main(sys.argv[1:], "python -m ulid") + entrypoint("python -m ulid")