-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to configure status spinner (#59)
* Add ability to configure status spinner
- Loading branch information
Showing
3 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
from yaspin.spinners import Spinners | ||
|
||
from shelloracle.shelloracle import get_query_from_pipe | ||
from shelloracle.shelloracle import get_query_from_pipe, spinner | ||
|
||
|
||
def test_get_query_from_pipe(monkeypatch): | ||
# Is a TTY | ||
monkeypatch.setattr(os, "isatty", lambda _: True) | ||
assert get_query_from_pipe() is None | ||
@pytest.fixture | ||
def mock_yaspin(monkeypatch): | ||
mock = MagicMock() | ||
monkeypatch.setattr("shelloracle.shelloracle.yaspin", mock) | ||
return mock | ||
|
||
# Not a TTY and no lines in the pipe | ||
monkeypatch.setattr(os, "isatty", lambda _: False) | ||
monkeypatch.setattr(sys.stdin, "readlines", lambda: []) | ||
assert get_query_from_pipe() is None | ||
|
||
# Not TTY and one line in the pipe | ||
monkeypatch.setattr(sys.stdin, "readlines", lambda: ["what is up"]) | ||
assert get_query_from_pipe() == "what is up" | ||
@pytest.fixture | ||
def mock_config(monkeypatch): | ||
config = MagicMock() | ||
monkeypatch.setattr("shelloracle.config._config", config) | ||
return config | ||
|
||
|
||
@pytest.mark.parametrize("spinner_style,expected", [(None, call()), ("earth", call(Spinners.earth))]) | ||
def test_spinner(spinner_style, expected, mock_config, mock_yaspin): | ||
mock_config.spinner_style = spinner_style | ||
spinner() | ||
assert mock_yaspin.call_args == expected | ||
|
||
|
||
# Not a TTY and multiple lines in the pipe | ||
def test_spinner_fail(mock_yaspin, mock_config): | ||
mock_config.spinner_style = "not a spinner style" | ||
with pytest.raises(AttributeError): | ||
spinner() | ||
|
||
|
||
@pytest.mark.parametrize("isatty,readlines,expected", [ | ||
(True, None, None), (False, [], None), (False, ["what is up"], "what is up") | ||
]) | ||
def test_get_query_from_pipe(isatty, readlines, expected, monkeypatch): | ||
monkeypatch.setattr(os, "isatty", lambda _: isatty) | ||
monkeypatch.setattr(sys.stdin, "readlines", lambda: readlines) | ||
assert get_query_from_pipe() == expected | ||
|
||
|
||
def test_get_query_from_pipe_fail(monkeypatch): | ||
monkeypatch.setattr(os, "isatty", lambda _: False) | ||
monkeypatch.setattr(sys.stdin, "readlines", lambda: ["what is up", "what is down"]) | ||
with pytest.raises(ValueError): | ||
get_query_from_pipe() |