Skip to content

Commit

Permalink
Fixing no stdin error (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
royreznik authored Feb 9, 2024
1 parent ece37a7 commit e301ba3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rexi/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import select
import sys
from typing import Annotated, Optional

Expand Down Expand Up @@ -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())
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e301ba3

Please sign in to comment.