Skip to content

Commit 8b71ccc

Browse files
committed
Add tests and fix edge cases
1 parent d0454ae commit 8b71ccc

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

pylint/testutils/functional/test_file.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
from collections.abc import Callable
1010
from os.path import basename, exists, join, split
1111
from pathlib import Path
12-
from typing import TypedDict
12+
from typing import Final, TypedDict
13+
14+
_CURRENT_VERSION: Final = sys.version_info[:2]
1315

1416

1517
def parse_python_version(ver_str: str) -> tuple[int, ...]:
@@ -105,14 +107,15 @@ def expected_output(self) -> str:
105107
p.stem
106108
for p in Path(self._directory).glob(f"{split(self.base)[-1]}.[0-9]*.txt")
107109
]
108-
# pylint: disable-next=bad-builtin
109-
current_version = int("".join(map(str, sys.version_info[:2])))
110110
output_options = [
111-
int(version) for s in files if (version := s.rpartition(".")[2]).isalnum()
111+
(int(version[0]), int(version[1:]))
112+
for s in files
113+
if (version := s.rpartition(".")[2]).isalnum()
112114
]
113115
for opt in sorted(output_options, reverse=True):
114-
if current_version >= opt:
115-
return join(self._directory, f"{self.base}.{opt}.txt")
116+
if _CURRENT_VERSION >= opt:
117+
str_opt = "".join([str(s) for s in opt])
118+
return join(self._directory, f"{self.base}.{str_opt}.txt")
116119
return join(self._directory, self.base + ".txt")
117120

118121
@property

tests/testutils/test_functional_testutils.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
"""Tests for the functional test framework."""
66

7+
import contextlib
8+
import os
9+
import shutil
10+
import tempfile
11+
from collections.abc import Iterator
712
from pathlib import Path
8-
from unittest.mock import MagicMock
13+
from unittest.mock import MagicMock, patch
914

1015
import pytest
1116
from _pytest.outcomes import Skipped
@@ -20,6 +25,26 @@
2025
DATA_DIRECTORY = HERE / "data"
2126

2227

28+
@contextlib.contextmanager
29+
def tempdir() -> Iterator[str]:
30+
"""Create a temp directory and change the current location to it.
31+
32+
This is supposed to be used with a *with* statement.
33+
"""
34+
tmp = tempfile.mkdtemp()
35+
36+
# Get real path of tempfile, otherwise test fail on mac os x
37+
current_dir = os.getcwd()
38+
os.chdir(tmp)
39+
abs_tmp = os.path.abspath(".")
40+
41+
try:
42+
yield abs_tmp
43+
finally:
44+
os.chdir(current_dir)
45+
shutil.rmtree(abs_tmp)
46+
47+
2348
@pytest.fixture(name="pytest_config")
2449
def pytest_config_fixture() -> MagicMock:
2550
def _mock_getoption(option: str) -> bool:
@@ -69,6 +94,37 @@ def test_get_functional_test_files_from_crowded_directory() -> None:
6994
assert "max_overflow" not in str(exc_info.value)
7095

7196

97+
@pytest.mark.parametrize(
98+
["files", "output_file_name"],
99+
[
100+
([], "file.txt"),
101+
(["file.txt"], "file.txt"),
102+
(["file.314.txt"], "file.txt"), # don't match 3.14
103+
(["file.42.txt"], "file.txt"), # don't match 4.2
104+
(["file.32.txt", "file.txt"], "file.32.txt"),
105+
(["file.312.txt", "file.txt"], "file.312.txt"),
106+
(["file.313.txt", "file.txt"], "file.313.txt"),
107+
(["file.310.txt", "file.313.txt", "file.312.txt", "file.txt"], "file.313.txt"),
108+
# don't match other test file names accidentally
109+
([".file.313.txt"], "file.txt"),
110+
(["file_other.313.txt"], "file.txt"),
111+
(["other_file.313.txt"], "file.txt"),
112+
],
113+
)
114+
def test_expected_output_file_matching(files: list[str], output_file_name: str) -> None:
115+
"""Test output file matching. Pin current Python version to 3.13."""
116+
with tempdir():
117+
for file in files:
118+
with open(file, "w", encoding="utf-8"):
119+
...
120+
test_file = FunctionalTestFile(".", "file.py")
121+
with patch(
122+
"pylint.testutils.functional.test_file._CURRENT_VERSION",
123+
new=(3, 13),
124+
):
125+
assert test_file.expected_output == f"./{output_file_name}"
126+
127+
72128
def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None:
73129
"""Test that all messages not targeted in the functional test are disabled
74130
when running with --minimal-messages-config.

0 commit comments

Comments
 (0)