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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
49 changes: 49 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import subprocess
import sys

import pytest

import ulid.__main__ as cli
Expand Down Expand Up @@ -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 == ""
6 changes: 3 additions & 3 deletions ulid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading