Esempio n. 1
0
def test_output_line_from_message(message: Callable) -> None:
    """Test that the OutputLine NamedTuple is instantiated correctly with from_msg."""
    expected_column = 2 if PY38_PLUS else 0

    output_line = OutputLine.from_msg(message())
    assert output_line.symbol == "missing-docstring"
    assert output_line.lineno == 1
    assert output_line.column == expected_column
    assert output_line.end_lineno == 1
    assert output_line.end_column == 3
    assert output_line.object == "obj"
    assert output_line.msg == "msg"
    assert output_line.confidence == "HIGH"

    output_line_with_end = OutputLine.from_msg(message(), True)
    assert output_line_with_end.symbol == "missing-docstring"
    assert output_line_with_end.lineno == 1
    assert output_line_with_end.column == expected_column
    assert output_line_with_end.end_lineno == 1
    assert output_line_with_end.end_column == 3
    assert output_line_with_end.object == "obj"
    assert output_line_with_end.msg == "msg"
    assert output_line_with_end.confidence == "HIGH"

    output_line_without_end = OutputLine.from_msg(message(), False)
    assert output_line_without_end.symbol == "missing-docstring"
    assert output_line_without_end.lineno == 1
    assert output_line_without_end.column == expected_column
    assert output_line_without_end.end_lineno is None
    assert output_line_without_end.end_column is None
    assert output_line_without_end.object == "obj"
    assert output_line_without_end.msg == "msg"
    assert output_line_without_end.confidence == "HIGH"
Esempio n. 2
0
def test_output_line_to_csv(confidence: Confidence, message: Callable) -> None:
    """Test that the OutputLine NamedTuple is instantiated correctly with from_msg
    and then converted to csv.
    """
    output_line = OutputLine.from_msg(message(confidence), True)
    csv = output_line.to_csv()
    expected_column = "2" if PY38_PLUS else "0"
    assert csv == (
        "missing-docstring",
        "1",
        expected_column,
        "1",
        "3",
        "obj",
        "msg",
        confidence.name,
    )

    output_line_without_end = OutputLine.from_msg(message(confidence), False)
    csv = output_line_without_end.to_csv()
    expected_column = "2" if PY38_PLUS else "0"
    assert csv == (
        "missing-docstring",
        "1",
        expected_column,
        "None",
        "None",
        "obj",
        "msg",
        confidence.name,
    )
Esempio n. 3
0
def test_output_line_from_csv_deprecated(
    confidence: Optional[str], expected_confidence: str
) -> None:
    """Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
    Test OutputLine's of length 5 or 6.
    """
    if confidence:
        proper_csv = [
            "missing-docstring",
            "1",
            "2",
            "obj",
            "msg",
            confidence,
        ]
    else:
        proper_csv = ["missing-docstring", "1", "2", "obj", "msg"]
    with pytest.warns(DeprecationWarning) as records:
        output_line = OutputLine.from_csv(proper_csv, True)
        assert len(records) == 1

    expected_column = 2 if PY38_PLUS else 0
    assert output_line == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        end_lineno=None,
        end_column=None,
        object="obj",
        msg="msg",
        confidence=expected_confidence,
    )
Esempio n. 4
0
def test_output_line_from_csv_error() -> None:
    with pytest.raises(
        MalformedOutputLineException,
        match="msg-symbolic-name:42:27:MyClass.my_function:The message",
    ):
        OutputLine.from_csv("'missing-docstring', 'line', 'column', 'obj', 'msg'")
    with pytest.raises(
        MalformedOutputLineException, match="symbol='missing-docstring' ?"
    ):
        csv = ("missing-docstring", "line", "column", "obj", "msg")
        OutputLine.from_csv(csv)
Esempio n. 5
0
 def _get_expected(self) -> Tuple["MessageCounter", List[OutputLine]]:
     with self._open_source_file() as f:
         expected_msgs = self.get_expected_messages(f)
     if not expected_msgs:
         expected_msgs = Counter()
     with self._open_expected_file() as f:
         expected_output_lines = [
             OutputLine.from_csv(row) for row in csv.reader(f, "test")
         ]
     return expected_msgs, expected_output_lines
Esempio n. 6
0
 def _get_expected(self):
     with self._open_source_file() as f:
         expected_msgs = self.get_expected_messages(f)
     if not expected_msgs:
         return Counter(), []
     with self._open_expected_file() as f:
         expected_output_lines = [
             OutputLine.from_csv(row) for row in csv.reader(f, "test")
         ]
     return expected_msgs, expected_output_lines
Esempio n. 7
0
def test_output_line() -> None:
    output_line = OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=2,
        object="",
        msg="Missing docstring's bad.",
        confidence=HIGH.name,
    )
    assert output_line.symbol == "missing-docstring"
Esempio n. 8
0
 def _get_expected(self) -> tuple[MessageCounter, list[OutputLine]]:
     with self._open_source_file() as f:
         expected_msgs = self.get_expected_messages(f)
     if not expected_msgs:
         expected_msgs = Counter()
     with self._open_expected_file() as f:
         expected_output_lines = [
             OutputLine.from_csv(row, self._check_end_position)
             for row in csv.reader(f, "test")
         ]
     return expected_msgs, expected_output_lines
Esempio n. 9
0
 def _get_actual(self):
     messages = self._linter.reporter.messages
     messages.sort(key=lambda m: (m.line, m.symbol, m.msg))
     received_msgs = Counter()
     received_output_lines = []
     for msg in messages:
         assert (msg.symbol !=
                 "fatal"), f"Pylint analysis failed because of '{msg.msg}'"
         received_msgs[msg.line, msg.symbol] += 1
         received_output_lines.append(OutputLine.from_msg(msg))
     return received_msgs, received_output_lines
Esempio n. 10
0
 def _get_actual(self) -> Tuple[MessageCounter, List[OutputLine]]:
     messages: List[Message] = self._linter.reporter.messages
     messages.sort(key=lambda m: (m.line, m.symbol, m.msg))
     received_msgs: MessageCounter = Counter()
     received_output_lines = []
     for msg in messages:
         assert (msg.symbol !=
                 "fatal"), f"Pylint analysis failed because of '{msg.msg}'"
         received_msgs[msg.line, msg.symbol] += 1
         received_output_lines.append(
             OutputLine.from_msg(msg, self._check_end_position))
     return received_msgs, received_output_lines
Esempio n. 11
0
def test_output_line_to_csv(confidence: Confidence, message: Callable) -> None:
    output_line = OutputLine.from_msg(message(confidence))
    csv = output_line.to_csv()
    expected_column = "2" if PY38_PLUS else "0"
    assert csv == (
        "missing-docstring",
        "1",
        expected_column,
        "obj",
        "msg",
        confidence.name,
    )
Esempio n. 12
0
    def _get_expected(self):
        with self._open_source_file() as fobj:
            expected_msgs = self.get_expected_messages(fobj)

        if expected_msgs:
            with self._open_expected_file() as fobj:
                expected_output_lines = [
                    OutputLine.from_csv(row)
                    for row in csv.reader(fobj, "test")
                ]
        else:
            expected_output_lines = []
        return expected_msgs, expected_output_lines
Esempio n. 13
0
def test_output_line_from_csv(
    confidence: Optional[str], expected_confidence: str
) -> None:
    if confidence:
        proper_csv = [
            "missing-docstring",
            "1",
            "2",
            "obj",
            "msg",
            confidence,
        ]
    else:
        proper_csv = ["missing-docstring", "1", "2", "obj", "msg"]
    output_line = OutputLine.from_csv(proper_csv)
    expected_column = 2 if PY38_PLUS else 0
    assert output_line == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        object="obj",
        msg="msg",
        confidence=expected_confidence,
    )
Esempio n. 14
0
def test_output_line_from_csv() -> None:
    """Test that the OutputLine NamedTuple is instantiated correctly with from_csv.
    Test OutputLine of length 8.
    """
    proper_csv = [
        "missing-docstring",
        "1",
        "2",
        "1",
        "None",
        "obj",
        "msg",
        "HIGH",
    ]
    expected_column = 2 if PY38_PLUS else 0

    output_line = OutputLine.from_csv(proper_csv)
    assert output_line == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        end_lineno=1,
        end_column=None,
        object="obj",
        msg="msg",
        confidence="HIGH",
    )
    output_line_with_end = OutputLine.from_csv(proper_csv, True)
    assert output_line_with_end == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        end_lineno=1,
        end_column=None,
        object="obj",
        msg="msg",
        confidence="HIGH",
    )
    output_line_without_end = OutputLine.from_csv(proper_csv, False)
    assert output_line_without_end == OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=expected_column,
        end_lineno=None,
        end_column=None,
        object="obj",
        msg="msg",
        confidence="HIGH",
    )
Esempio n. 15
0
def test_output_line() -> None:
    """Test that the OutputLine NamedTuple is instantiated correctly."""
    output_line = OutputLine(
        symbol="missing-docstring",
        lineno=1,
        column=2,
        end_lineno=1,
        end_column=4,
        object="",
        msg="Missing docstring's bad.",
        confidence=HIGH.name,
    )
    assert output_line.symbol == "missing-docstring"
    assert output_line.lineno == 1
    assert output_line.column == 2
    assert output_line.end_lineno == 1
    assert output_line.end_column == 4
    assert output_line.object == ""
    assert output_line.msg == "Missing docstring's bad."
    assert output_line.confidence == "HIGH"
Esempio n. 16
0
def test_output_line_from_csv_error() -> None:
    """Test that errors are correctly raised for incorrect OutputLine's."""
    # Test a csv-string which does not have a number for line and column
    with pytest.warns(
            UserWarning,
            match="msg-symbolic-name:42:27:MyClass.my_function:The message",
    ):
        OutputLine.from_csv(
            "'missing-docstring', 'line', 'column', 'obj', 'msg'", True)
    # Test a tuple which does not have a number for line and column
    with pytest.warns(UserWarning,
                      match="we got 'missing-docstring:line:column:obj:msg'"):
        csv = ("missing-docstring", "line", "column", "obj", "msg")
        OutputLine.from_csv(csv, True)
    # Test a csv-string that is too long
    with pytest.warns(
            UserWarning,
            match="msg-symbolic-name:42:27:MyClass.my_function:The message",
    ):
        OutputLine.from_csv(
            "'missing-docstring', 1, 2, 'obj', 'msg', 'func', 'message', 'conf', 'too_long'",
            True,
        )
Esempio n. 17
0
def test_output_line_from_message(message: Callable) -> None:
    output_line = OutputLine.from_msg(message())
    assert output_line.symbol == "missing-docstring"
    assert output_line.msg == "msg"