Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #42 #43

Merged
merged 2 commits into from
Jul 28, 2024
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
5 changes: 2 additions & 3 deletions src/dzira/cli/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ def wrapper(*args, **kwargs):
error_msg = " ".join(messages)
else:
error_msg = (
"failed not perform the request while trying to "
f"{func.__name__.replace('_', ' ')}"
" (no error supplied by JIRA) :("
f"{func.__name__.replace('_', ' ')} returned an error: "
f"{exc.response.reason!r} :("
)
else:
error_msg = exc
Expand Down
111 changes: 107 additions & 4 deletions tests/cli/test_output.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
from unittest.mock import patch, sentinel
import time
from unittest.mock import Mock, call, patch, sentinel

import pytest
from jira.exceptions import JIRAError

from src.dzira.betterdict import D
from src.dzira.cli.output import (
Expand Down Expand Up @@ -88,6 +90,107 @@ def test_is_initialized_properly(self):
assert spinner.colorizer == colors.c
assert spinner.use == True

@pytest.mark.xfail
def test_todo_run(self):
assert False, "Need to write tests"
@patch("src.dzira.cli.output.print")
@patch("src.dzira.cli.output.concurrent.futures.ThreadPoolExecutor")
def test_uses_spinner(self, mock_thread_pool_executor, mock_print):
mock_submit = Mock(
return_value=Mock(
running=Mock(side_effect=[True, False]),
result=Mock(
return_value=Mock(
stdout=sentinel.stdout
)
)
)
)
mock_thread_pool_executor.return_value.__enter__.return_value.submit = mock_submit

mock_colorizer = Mock(side_effect=[sentinel.for_running, sentinel.for_result])
spinner = Spinner(mock_colorizer)

@spinner.run(msg=sentinel.msg, done=sentinel.done)
def run_with_spinner(arg, kwarg=None):
time.sleep(0.11)
return [arg, kwarg]

run_with_spinner(sentinel.arg, kwarg=sentinel.kwarg)

assert run_with_spinner.is_decorated_with_spinner
assert mock_submit.call_args[0][0].__name__ == "run_with_spinner"
assert mock_submit.call_args[0][1] == sentinel.arg
assert mock_submit.call_args[1] == {"kwarg": sentinel.kwarg}
assert mock_print.call_args_list == [
call(sentinel.for_running, end="", flush=True, file=sys.stderr),
call(sentinel.for_result, flush=True, file=sys.stderr),
]
assert mock_colorizer.call_args_list == [
call("\r", "^magenta", "⠋", " ", sentinel.msg),
call("\r", "^green", sentinel.done, " ", sentinel.msg, "^reset", ":\t", sentinel.stdout)
]

@patch("src.dzira.cli.output.concurrent.futures.ThreadPoolExecutor")
def test_does_not_use_spinner(self, mock_thread_pool_executor):
spinner = Spinner(Mock())
spinner.use = False

@spinner.run("Testing")
def run_without_spinner():
return sentinel.result

result = run_without_spinner()

assert run_without_spinner.is_decorated_with_spinner
assert result == sentinel.result
assert not mock_thread_pool_executor.called

@patch("src.dzira.cli.output.print", Mock())
def test_gracefully_reports_errors(self):
colors = Colors()
spinner = Spinner(colors.c)

@spinner.run("Testing")
def jira_error_with_reason():
raise JIRAError(
"error",
status_code=500,
url="foo.bar.baz",
request=Mock(),
response=Mock(
json=Mock(return_value={}),
reason="blah!"
),
)

with pytest.raises(Exception) as exc:
jira_error_with_reason()

assert jira_error_with_reason.is_decorated_with_spinner
assert "jira error with reason returned an error: 'blah!'" in str(exc)

@spinner.run("Testing")
def jira_error_with_json():
raise JIRAError(
"error",
status_code=404,
url="foo.bar.baz",
request=Mock(),
response=Mock(
json=Mock(return_value={"errorMessages": ["page not found"]}),
),
)

with pytest.raises(Exception) as exc:
jira_error_with_json()

assert jira_error_with_json.is_decorated_with_spinner
assert "page not found" in str(exc)

@spinner.run("Testing")
def non_jira_error():
raise Exception("non jira exception")

with pytest.raises(Exception) as exc:
non_jira_error()

assert non_jira_error.is_decorated_with_spinner
assert "non jira exception" in str(exc)