Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pylint/testutils/functional/lint_module_output_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def _check_output_text(
actual_output: list[OutputLine],
) -> None:
"""Overwrite or remove the expected output file based on actual output."""
if self._test_file.expected_output_is_fallback:
return
# Remove the expected file if no output is actually emitted and a file exists
if not actual_output:
if os.path.exists(self._test_file.expected_output):
Expand Down
16 changes: 14 additions & 2 deletions pylint/testutils/functional/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ def module(self) -> str:

@property
def expected_output(self) -> str:
output, _ = self._resolve_expected_output()
return output

@property
def expected_output_is_fallback(self) -> bool:
_, is_fallback = self._resolve_expected_output()
return is_fallback

def _resolve_expected_output(self) -> tuple[str, bool]:
files = [
p.stem
for p in Path(self._directory).glob(f"{split(self.base)[-1]}.[0-9]*.txt")
Expand All @@ -115,8 +124,11 @@ def expected_output(self) -> str:
for opt in sorted(output_options, reverse=True):
if _CURRENT_VERSION >= opt:
str_opt = "".join([str(s) for s in opt])
return join(self._directory, f"{self.base}.{str_opt}.txt")
return join(self._directory, self.base + ".txt")
return (
join(self._directory, f"{self.base}.{str_opt}.txt"),
_CURRENT_VERSION != opt,
)
return join(self._directory, self.base + ".txt"), False

@property
def source(self) -> str:
Expand Down
43 changes: 43 additions & 0 deletions tests/testutils/test_lint_module_output_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil
from collections.abc import Callable
from pathlib import Path
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -71,6 +72,48 @@ def test_lint_module_output_update_remove_useless_txt(
assert not expected_output_file.exists()


def test_lint_module_output_update_does_not_remove_fallback_versioned_output(
lint_module_fixture: Callable[[str], tuple[Path, Path, LintModuleOutputUpdate]],
) -> None:
"""Fallback versioned expected output should not be deleted on update."""
filename, expected_output_file, lmou = lint_module_fixture("fine_name")
expected_output_file.write_text("", encoding="utf8")
filename.write_text("", encoding="utf8")
fallback_output_file = filename.with_suffix(".313.txt")
fallback_output_file.write_text("old output\n", encoding="utf8")

with patch(
"pylint.testutils.functional.test_file._CURRENT_VERSION",
new=(3, 14),
):
assert Path(lmou._test_file.expected_output) == fallback_output_file
lmou.runTest()
assert expected_output_file.exists()
assert fallback_output_file.exists()


def test_lint_module_output_update_does_not_overwrite_fallback_versioned_output(
lint_module_fixture: Callable[[str], tuple[Path, Path, LintModuleOutputUpdate]],
) -> None:
"""Fallback versioned expected output should not be overwritten on update."""
filename, expected_output_file, lmou = lint_module_fixture("foo")
expected_output_file.write_text("", encoding="utf8")
filename.write_text("# [disallowed-name]\n", encoding="utf8")
fallback_output_file = filename.with_suffix(".313.txt")
fallback_output_file.write_text("old output\n", encoding="utf8")

with patch(
"pylint.testutils.functional.test_file._CURRENT_VERSION",
new=(3, 14),
):
assert Path(lmou._test_file.expected_output) == fallback_output_file
lmou.runTest()

assert expected_output_file.exists()
assert fallback_output_file.exists()
assert fallback_output_file.read_text(encoding="utf8") == "old output\n"


@pytest.mark.parametrize(
"directory_path", DIRECTORIES, ids=[str(p) for p in DIRECTORIES]
)
Expand Down