|  | 
|  | 1 | +from __future__ import annotations | 
|  | 2 | + | 
| 1 | 3 | import os | 
| 2 | 4 | import sys | 
|  | 5 | +from unittest.mock import MagicMock, call | 
| 3 | 6 | 
 | 
| 4 | 7 | import pytest | 
|  | 8 | +from yaspin.spinners import Spinners | 
| 5 | 9 | 
 | 
| 6 |  | -from shelloracle.shelloracle import get_query_from_pipe | 
|  | 10 | +from shelloracle.shelloracle import get_query_from_pipe, spinner | 
| 7 | 11 | 
 | 
| 8 | 12 | 
 | 
| 9 |  | -def test_get_query_from_pipe(monkeypatch): | 
| 10 |  | -    # Is a TTY | 
| 11 |  | -    monkeypatch.setattr(os, "isatty", lambda _: True) | 
| 12 |  | -    assert get_query_from_pipe() is None | 
|  | 13 | +@pytest.fixture | 
|  | 14 | +def mock_yaspin(monkeypatch): | 
|  | 15 | +    mock = MagicMock() | 
|  | 16 | +    monkeypatch.setattr("shelloracle.shelloracle.yaspin", mock) | 
|  | 17 | +    return mock | 
| 13 | 18 | 
 | 
| 14 |  | -    # Not a TTY and no lines in the pipe | 
| 15 |  | -    monkeypatch.setattr(os, "isatty", lambda _: False) | 
| 16 |  | -    monkeypatch.setattr(sys.stdin, "readlines", lambda: []) | 
| 17 |  | -    assert get_query_from_pipe() is None | 
| 18 | 19 | 
 | 
| 19 |  | -    # Not TTY and one line in the pipe | 
| 20 |  | -    monkeypatch.setattr(sys.stdin, "readlines", lambda: ["what is up"]) | 
| 21 |  | -    assert get_query_from_pipe() == "what is up" | 
|  | 20 | +@pytest.fixture | 
|  | 21 | +def mock_config(monkeypatch): | 
|  | 22 | +    config = MagicMock() | 
|  | 23 | +    monkeypatch.setattr("shelloracle.config._config", config) | 
|  | 24 | +    return config | 
|  | 25 | + | 
|  | 26 | + | 
|  | 27 | +@pytest.mark.parametrize("spinner_style,expected", [(None, call()), ("earth", call(Spinners.earth))]) | 
|  | 28 | +def test_spinner(spinner_style, expected, mock_config, mock_yaspin): | 
|  | 29 | +    mock_config.spinner_style = spinner_style | 
|  | 30 | +    spinner() | 
|  | 31 | +    assert mock_yaspin.call_args == expected | 
|  | 32 | + | 
| 22 | 33 | 
 | 
| 23 |  | -    # Not a TTY and multiple lines in the pipe | 
|  | 34 | +def test_spinner_fail(mock_yaspin, mock_config): | 
|  | 35 | +    mock_config.spinner_style = "not a spinner style" | 
|  | 36 | +    with pytest.raises(AttributeError): | 
|  | 37 | +        spinner() | 
|  | 38 | + | 
|  | 39 | + | 
|  | 40 | +@pytest.mark.parametrize("isatty,readlines,expected", [ | 
|  | 41 | +    (True, None, None), (False, [], None), (False, ["what is up"], "what is up") | 
|  | 42 | +]) | 
|  | 43 | +def test_get_query_from_pipe(isatty, readlines, expected, monkeypatch): | 
|  | 44 | +    monkeypatch.setattr(os, "isatty", lambda _: isatty) | 
|  | 45 | +    monkeypatch.setattr(sys.stdin, "readlines", lambda: readlines) | 
|  | 46 | +    assert get_query_from_pipe() == expected | 
|  | 47 | + | 
|  | 48 | + | 
|  | 49 | +def test_get_query_from_pipe_fail(monkeypatch): | 
|  | 50 | +    monkeypatch.setattr(os, "isatty", lambda _: False) | 
| 24 | 51 |     monkeypatch.setattr(sys.stdin, "readlines", lambda: ["what is up", "what is down"]) | 
| 25 | 52 |     with pytest.raises(ValueError): | 
| 26 | 53 |         get_query_from_pipe() | 
0 commit comments