Skip to content

Commit

Permalink
Add unit tests for LintModuleOutputUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Nov 28, 2021
1 parent f349e0c commit 9b8ea92
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/testutils/test_lint_module_output_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE

# pylint: disable=redefined-outer-name
from pathlib import Path
from typing import Callable, Tuple

import pytest

from pylint.testutils import FunctionalTestFile
from pylint.testutils.functional import LintModuleOutputUpdate


@pytest.fixture()
def lint_module_fixture(
tmp_path: Path,
) -> Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]:
def inner(base: str) -> Tuple[Path, Path, LintModuleOutputUpdate]:
filename = tmp_path / f"{base}.py"
expected_output_file = tmp_path / f"{base}.txt"
lmou = LintModuleOutputUpdate(
test_file=FunctionalTestFile(str(tmp_path), str(filename))
)
return filename, expected_output_file, lmou

return inner


def test_lint_module_output_update_fail_before(
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
) -> None:
"""There is a fail before the output need to be updated."""
filename, expected_output_file, lmou = lint_module_fixture("foo")
filename.write_text("", encoding="utf8")
assert not expected_output_file.exists()
with pytest.raises(AssertionError, match="1: disallowed-name"):
lmou.runTest()
assert not expected_output_file.exists()


def test_lint_module_output_update_effective(
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
) -> None:
"""The file is updated following a successful tests with wrong output."""
filename, expected_output_file, lmou = lint_module_fixture("foo")
filename.write_text("# [disallowed-name]\n", encoding="utf8")
lmou.runTest()
assert (expected_output_file).exists()
assert (
expected_output_file.read_text(encoding="utf8")
== 'disallowed-name:1:0:None:None::"Disallowed name ""foo""":UNDEFINED\n'
)


def test_lint_module_output_update_remove_useless_txt(
lint_module_fixture: Callable[[str], Tuple[Path, Path, LintModuleOutputUpdate]]
) -> None:
"""The file is updated following a successful tests with wrong output."""
filename, expected_output_file, lmou = lint_module_fixture("fine_name")
expected_output_file.write_text("", encoding="utf8")
filename.write_text("", encoding="utf8")
lmou.runTest()
assert not (expected_output_file).exists()

0 comments on commit 9b8ea92

Please sign in to comment.