diff --git a/rexi/cli.py b/rexi/cli.py index cf85d15..fcbe354 100644 --- a/rexi/cli.py +++ b/rexi/cli.py @@ -1,4 +1,5 @@ import os +import select import sys from typing import Annotated, Optional @@ -40,6 +41,23 @@ def rexi_cli( if input_file: input_text = input_file.read() else: + """ + Yep this part is abit ugly. + couldn't find a better way to implement it so it will work with `typer`, `pytest` and `textual` + """ # noqa: E501 + if not select.select( + [ + sys.stdin, + ], + [], + [], + 0.0, + )[0]: + raise typer.BadParameter( + "stdin is empty, " + "please provide text thru the stdin " + "or use the `-i` flag" + ) input_text = sys.stdin.read() try: os.close(sys.stdin.fileno()) diff --git a/tests/test_cli.py b/tests/test_cli.py index 9c40717..c4fcc0c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -18,11 +18,16 @@ def test_on_args(monkeypatch: MonkeyPatch) -> None: class_mock = Mock() instance_mock = Mock() open_mock = Mock() + select = Mock() + with monkeypatch.context(): + select.return_value = [[True]] class_mock.return_value = instance_mock + monkeypatch.setattr("select.select", select) monkeypatch.setattr("rexi.cli.RexiApp", class_mock) monkeypatch.setattr("builtins.open", open_mock) runner.invoke(app, input=a) + open_mock.assert_called_once_with("/dev/tty", "rb") class_mock.assert_called_once_with( text.decode(), initial_mode=None, initial_pattern=None @@ -67,3 +72,16 @@ def test_initial_mode(monkeypatch: MonkeyPatch, tmp_path: Path) -> None: monkeypatch.setattr("rexi.cli.RexiApp", class_mock) runner.invoke(app, args=["-i", str(tmp_path / "text_file"), "--mode", "match"]) class_mock.assert_called_once_with(text, initial_mode="match", initial_pattern=None) + + +def test_no_stdin_error(monkeypatch: MonkeyPatch) -> None: + """ + Couldn't find a better way to test the CLI without patching everything :( + """ + runner = CliRunner() + select = Mock() + with monkeypatch.context(): + select.return_value = [[]] + monkeypatch.setattr("select.select", select) + result = runner.invoke(app) + assert "Invalid value" in result.output