Skip to content

Commit

Permalink
test_load_messages test fixed using patch
Browse files Browse the repository at this point in the history
  • Loading branch information
merrickliu888 committed Nov 3, 2023
1 parent bc13e8c commit ab7a72c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions python_ta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def _verify_pre_check(filepath: AnyStr) -> bool:

def _get_valid_files_to_check(module_name: Union[List[str], str]) -> Generator[AnyStr, None, None]:
"""A generator for all valid files to check."""
logging.info("RAN")
# Allow call to check with empty args
if module_name == "":
m = sys.modules["__main__"]
Expand Down
29 changes: 21 additions & 8 deletions tests/test_init_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Tests for top level __init__.py logging functionality in pyta"""
import tokenize
from unittest.mock import patch

import python_ta
from python_ta import _get_valid_files_to_check


# TODO how to get unexpected error?
Expand All @@ -16,32 +20,41 @@ def test_check_log(caplog) -> None:
assert expected_messages[i] in caplog.records[i].msg


def test_pre_check_log(caplog) -> None:
@patch("pylint.utils.pragma_parser.OPTION_PO.search", side_effect=IndentationError)
def test_pre_check_log(_, caplog) -> None:
"""Testing logging in _verify_pre_check function"""
# Checking pylint comment

# Checking indentation error (Use unexpected indent) # TODO does pyta handle these erros
# Checking indentation error (Use unexpected indent) # TODO does pyta handle these
# with patch("python_ta.OPTION_PO.search", side_effect=IndentationError):
python_ta.check_all(module_name="examples/syntax_errors/unexpected_indent")
assert "indentation error at line" in caplog.text

# Checking token error (Use syntax error examples)
python_ta.check_all(module_name="examples/syntax_errors/missing_colon")
assert "syntax error in your file" in caplog.text

# Checking unicode decode error
# with patch("python_ta.OPTION_PO.search", side_effect=tokenize.TokenError):
# python_ta.check_all(module_name="examples/syntax_errors/missing_colon")
# assert "syntax error in your file" in caplog.text
#
# # Checking unicode decode error
# with patch("python_ta.OPTION_PO.search", side_effect=UnicodeDecodeError):
# python_ta.check_all(module_name="examples/syntax_errors/missing_colon")
# assert "python_ta could not check your code due to an invalid character. Please check the following lines in your file and all characters that are marked with a �." in caplog.text


def test_files_to_check_log(caplog) -> None:
def test_get_valid_files_to_check(caplog) -> None:
"""Testing logging in _get_valid_files_to_check function"""
expected_logs = [
"No checks run. Input to check, `{'examples/nodes/assign'}`, has invalid type, must be a list of strings.",
"No check run on file `0`, with invalid type. Must be type: str.",
"Could not find the file called, `foo`",
]
# TODO this is some how passing and still returning a generator
# _get_valid_files_to_check({"examples/nodes/assign"})
# _get_valid_files_to_check([0])
# _get_valid_files_to_check("foo")
python_ta.check_all(module_name={"examples/nodes/assign"})
python_ta.check_all(module_name=[0])
python_ta.check_all(module_name="foo")

for i in range(3):
assert caplog.records[i].levelname == "ERROR"
assert expected_logs[i] in caplog.records[i].msg
Expand Down
2 changes: 1 addition & 1 deletion tests/test_init_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def test_sys_version_log(caplog, monkeypatch) -> None:
monkeypatch.setattr(sys, "version_info", (2, 6, 0))
import python_ta

python_ta.check_all()
# python_ta.check_all()
assert caplog.records[0].levelname == "WARNING"
assert "You need Python 3.7 or later to run PythonTA." in caplog.text

0 comments on commit ab7a72c

Please sign in to comment.