Beispiel #1
0
def test_package(finalize_linter: PyLinter, file_names: list[str],
                 check: Callable) -> None:
    finalize_linter.check(file_names)
    finalize_linter.reporter = cast(  # Due to fixture
        testutils.GenericTestReporter, finalize_linter.reporter)
    got = finalize_linter.reporter.finalize().strip()
    assert check(got)
Beispiel #2
0
def test_check_package___init__(finalize_linter: PyLinter) -> None:
    filename = ["package.__init__"]
    finalize_linter.check(filename)
    checked = list(finalize_linter.stats.by_module.keys())
    assert sorted(checked) == sorted(filename)

    os.chdir(join(REGR_DATA, "package"))
    finalize_linter.check(["__init__"])
    checked = list(finalize_linter.stats.by_module.keys())
    assert checked == ["__init__"]
Beispiel #3
0
def test_types_redefined(linter: PyLinter) -> None:
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), "data",
                         "bad_builtin.py")
    with fix_import_path([elif_test]):
        linter.check([elif_test])
    msgs = sorted(linter.reporter.messages, key=lambda item: item.line)
    assert len(msgs) == 2
    for msg, expected in zip(msgs, EXPECTED):
        assert msg.symbol == "bad-builtin"
        assert msg.msg == expected
Beispiel #4
0
def test_check_package___init__(finalize_linter: PyLinter) -> None:
    filename = ["package.__init__"]
    finalize_linter.check(filename)
    checked = list(finalize_linter.stats["by_module"].keys())  # type: ignore # Refactor of PyLinter.stats necessary
    assert checked == filename

    os.chdir(join(REGR_DATA, "package"))
    finalize_linter.check(["__init__"])
    checked = list(finalize_linter.stats["by_module"].keys())  # type: ignore
    assert checked == ["__init__"]
Beispiel #5
0
def test_elseif_message(linter: PyLinter) -> None:
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), "data", "elif.py")
    linter.check([elif_test])
    msgs = linter.reporter.messages
    assert len(msgs) == 2
    for msg in msgs:
        assert msg.symbol == "else-if-used"
        assert msg.msg == 'Consider using "elif" instead of "else if"'
    assert msgs[0].line == 9
    assert msgs[1].line == 21
Beispiel #6
0
def test_docstring_message(linter: PyLinter) -> None:
    docstring_test = join(dirname(abspath(__file__)), "data", "docstring.py")
    linter.check([docstring_test])
    msgs = linter.reporter.messages
    assert len(msgs) == 7
    for msg, expected_symbol, expected_msg in zip(
        msgs, EXPECTED_SYMBOLS, EXPECTED_MSGS
    ):
        assert msg.symbol == expected_symbol
        assert msg.msg == expected_msg
Beispiel #7
0
def test_emptystring_message(linter: PyLinter) -> None:
    elif_test = osp.join(osp.dirname(osp.abspath(__file__)), "data",
                         "empty_string_comparison.py")
    linter.check([elif_test])
    msgs = linter.reporter.messages
    expected_lineno = [6, 9, 12, 15]
    assert len(msgs) == len(expected_lineno)
    for msg, lineno in zip(msgs, expected_lineno):
        assert msg.symbol == "compare-to-empty-string"
        assert msg.msg == "Avoid comparisons to empty string"
        assert msg.line == lineno
Beispiel #8
0
def test_comment_base_case(linter: PyLinter) -> None:
    comment_test = str(Path(__file__).parent.joinpath("data", "empty_comment.py"))
    linter.check([comment_test])
    msgs = linter.reporter.messages
    assert len(msgs) == 4
    for msg in msgs:
        assert msg.symbol == "empty-comment"
        assert msg.msg == "Line with empty comment"
    assert msgs[0].line == 2
    assert msgs[1].line == 3
    assert msgs[2].line == 5
    assert msgs[3].line == 7
Beispiel #9
0
def test_crash_in_file(linter: PyLinter, capsys: CaptureFixture,
                       tmpdir: LocalPath) -> None:
    args = linter.load_command_line_configuration([__file__])
    linter.crash_file_path = str(tmpdir / "pylint-crash-%Y")
    linter.check(args)
    out, err = capsys.readouterr()
    assert not out
    assert not err
    files = tmpdir.listdir()
    assert len(files) == 1
    assert "pylint-crash-20" in str(files[0])
    with open(files[0], encoding="utf8") as f:
        content = f.read()
    assert "Failed to import module spam." in content
Beispiel #10
0
def test_overlapping_exceptions(linter: PyLinter) -> None:
    test = join(dirname(__file__), "data", "overlapping_exceptions.py")
    linter.check([test])
    msgs = linter.reporter.messages

    expected = [
        (13,
         "Overlapping exceptions (SomeException and SomeException are the same)"
         ),
        (
            18,
            "Overlapping exceptions (SomeException is an ancestor class of SubclassException)",
        ),
        (23,
         "Overlapping exceptions (SomeException and AliasException are the same)"
         ),
        (
            28,
            "Overlapping exceptions (AliasException is an ancestor class of SubclassException)",
        ),
        (34,
         "Overlapping exceptions (SomeException and AliasException are the same)"
         ),
        (
            34,
            "Overlapping exceptions (SomeException is an ancestor class of SubclassException)",
        ),
        (
            34,
            "Overlapping exceptions (AliasException is an ancestor class of SubclassException)",
        ),
        (
            39,
            "Overlapping exceptions (ArithmeticError is an ancestor class of FloatingPointError)",
        ),
        (
            44,
            "Overlapping exceptions (ValueError is an ancestor class of UnicodeDecodeError)",
        ),
    ]

    assert len(msgs) == len(expected)
    for msg, exp in zip(msgs, expected):
        assert msg.msg_id == "W0714"
        assert msg.symbol == "overlapping-except"
        assert msg.category == "warning"
        assert (msg.line, msg.msg) == exp
Beispiel #11
0
def test_overlapping_exceptions_py33(linter: PyLinter) -> None:
    """From Python 3.3 both IOError and socket.error are aliases for OSError."""
    test = join(dirname(__file__), "data", "overlapping_exceptions_py33.py")
    linter.check([test])
    msgs = linter.reporter.messages

    expected = [
        (7, "Overlapping exceptions (IOError and OSError are the same)"),
        (12, "Overlapping exceptions (socket.error and OSError are the same)"),
        (
            17,
            "Overlapping exceptions (socket.error is an ancestor class of ConnectionError)",
        ),
    ]

    assert len(msgs) == len(expected)
    for msg, exp in zip(msgs, expected):
        assert msg.msg_id == "W0714"
        assert msg.symbol == "overlapping-except"
        assert msg.category == "warning"
        assert (msg.line, msg.msg) == exp
Beispiel #12
0
def test_descriptor_crash(fname: str, finalize_linter: PyLinter) -> None:
    finalize_linter.check([join(REGR_DATA, fname)])
    finalize_linter.reporter.finalize().strip()
Beispiel #13
0
def test_check_deprecation(linter: PyLinter, recwarn):
    linter.check("myfile.py")
    msg = recwarn.pop()
    assert "check function will only accept sequence" in str(msg)
Beispiel #14
0
def test_descriptor_crash(fname: str, finalize_linter: PyLinter) -> None:
    finalize_linter.check([join(REGR_DATA, fname)])
    finalize_linter.reporter = cast(  # Due to fixture
        testutils.GenericTestReporter, finalize_linter.reporter)
    finalize_linter.reporter.finalize().strip()
Beispiel #15
0
def test_crash(finalize_linter: PyLinter, file_names: list[str]) -> None:
    finalize_linter.check(file_names)
Beispiel #16
0
def test_max_mccabe_rate(linter: PyLinter, fname_mccabe_example: str,
                         complexity: int, expected: List[str]) -> None:
    linter.global_set_option("max-complexity", complexity)
    linter.check([fname_mccabe_example])
    real_msgs = [message.msg for message in linter.reporter.messages]
    assert sorted(expected) == sorted(real_msgs)
Beispiel #17
0
def test_hang(finalize_linter: PyLinter, file_names: List[str]) -> None:
    finalize_linter.check(file_names)
Beispiel #18
0
def test_hang(finalize_linter: PyLinter, file_names: List[str],
              timeout_s: float) -> None:
    with timeout(timeout_s):
        finalize_linter.check(file_names)