-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch exception in tokenization subsystem per line, log CRITICAL (#223)
Catch exception in tokenization subsystem per line, log CRITICAL Reviewed-by: https://github.com/apps/ansible-zuul
- Loading branch information
Showing
2 changed files
with
80 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""tests for colorize | ||
""" | ||
import json | ||
import os | ||
|
||
from unittest.mock import patch | ||
|
||
from ansible_navigator.ui_framework.colorize import Colorize | ||
|
||
from ansible_navigator.yaml import human_dump | ||
|
||
SHARE_DIR = os.path.abspath( | ||
os.path.join(os.path.basename(__file__), "..", "share", "ansible_navigator") | ||
) | ||
|
||
|
||
def test_basic_success_json(): | ||
"""Ensure the json string is returned as 1 lines, 5 parts and can be reassembled | ||
to the json string""" | ||
sample = json.dumps({"test": "data"}) | ||
result = Colorize(SHARE_DIR).render(doc=sample, scope="source.json") | ||
assert len(result) == 1 | ||
assert len(result[0]) == 5 | ||
assert "".join(line_part["chars"] for line_part in result[0]) == sample | ||
|
||
|
||
def test_basic_success_yaml(): | ||
"""Ensure the yaml string is returned as 2 lines, with 1 and 3 parts | ||
respectively, ensure the parts of the second line can be reaseembled to | ||
the second line of the yaml string | ||
""" | ||
sample = human_dump({"test": "data"}) | ||
result = Colorize(SHARE_DIR).render(doc=sample, scope="source.yaml") | ||
assert len(result) == 2 | ||
assert len(result[0]) == 1 | ||
assert result[0][0]["chars"] == sample.splitlines()[0] | ||
assert len(result[1]) == 3 | ||
assert "".join(line_part["chars"] for line_part in result[1]) == sample.splitlines()[1] | ||
|
||
|
||
@patch("ansible_navigator.ui_framework.colorize.tokenize") | ||
def test_graceful_failure(mocked_func, caplog): | ||
"""Ensure a tokenization error returns the original one line json string | ||
w/o color and the log reflects the critical error | ||
""" | ||
mocked_func.side_effect = ValueError() | ||
sample = json.dumps({"test": "data"}) | ||
result = Colorize(SHARE_DIR).render(doc=sample, scope="source.json") | ||
assert len(result) == 1 | ||
assert len(result[0]) == 1 | ||
assert result[0][0]["chars"] == sample | ||
assert result[0][0]["color"] is None | ||
assert result[0][0]["column"] == 0 | ||
assert "rendered without color" in caplog.text |