diff --git a/pylint/testutils/functional/test_file.py b/pylint/testutils/functional/test_file.py index 342ced44bf..fae018753a 100644 --- a/pylint/testutils/functional/test_file.py +++ b/pylint/testutils/functional/test_file.py @@ -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") @@ -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: diff --git a/tests/test_functional.py b/tests/test_functional.py index 918bb1c47d..4af2bca62e 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -66,6 +66,9 @@ def test_functional( else: actual_output = lint_test.check_messages() - golden_master.check( - lint_test.serialize_output(actual_output), test_file.expected_output - ) + serialized_output = lint_test.serialize_output(actual_output) + if test_file.expected_output_is_fallback: + expected = Path(test_file.expected_output).read_text(encoding="utf-8") + assert serialized_output.rstrip() == expected.rstrip() + else: + golden_master.check(serialized_output, test_file.expected_output) diff --git a/tests/testutils/test_functional_testutils.py b/tests/testutils/test_functional_testutils.py index 2b1574e354..9d7d7416e4 100644 --- a/tests/testutils/test_functional_testutils.py +++ b/tests/testutils/test_functional_testutils.py @@ -127,6 +127,55 @@ def test_expected_output_file_matching(files: list[str], output_file_name: str) assert test_file.expected_output == f".{os.path.sep}{output_file_name}" +@pytest.mark.parametrize( + ["files", "output_file_name", "is_fallback"], + [ + ([], "file.txt", False), + (["file.txt"], "file.txt", False), + (["file.315.txt"], "file.txt", False), # don't match 3.15 + (["file.42.txt"], "file.txt", False), # don't match 4.2 + (["file.32.txt", "file.txt"], "file.32.txt", True), + (["file.313.txt", "file.txt"], "file.313.txt", True), + (["file.314.txt", "file.txt"], "file.314.txt", False), + ( + [ + "file.310.txt", + "file.313.txt", + "file.312.txt", + "file.314.txt", + "file.txt", + ], + "file.314.txt", + False, + ), + ( + ["file.310.txt", "file.313.txt", "file.312.txt", "file.txt"], + "file.313.txt", + True, + ), + # don't match other test file names accidentally + ([".file.313.txt"], "file.txt", False), + (["file_other.313.txt"], "file.txt", False), + (["other_file.313.txt"], "file.txt", False), + ], +) +def test_expected_output_fallback_detection( + files: list[str], output_file_name: str, is_fallback: bool +) -> None: + """Test output file fallback detection. Pin current Python version to 3.13.""" + with tempdir(): + for file in files: + with open(file, "w", encoding="utf-8"): + ... + test_file = FunctionalTestFile(".", "file.py") + with patch( + "pylint.testutils.functional.test_file._CURRENT_VERSION", + new=(3, 14), + ): + assert test_file.expected_output == f".{os.path.sep}{output_file_name}" + assert test_file.expected_output_is_fallback is is_fallback + + def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None: """Test that all messages not targeted in the functional test are disabled when running with --minimal-messages-config.