|
4 | 4 |
|
5 | 5 | """Tests for the functional test framework."""
|
6 | 6 |
|
| 7 | +import contextlib |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import tempfile |
| 11 | +from collections.abc import Iterator |
7 | 12 | from pathlib import Path
|
8 |
| -from unittest.mock import MagicMock |
| 13 | +from unittest.mock import MagicMock, patch |
9 | 14 |
|
10 | 15 | import pytest
|
11 | 16 | from _pytest.outcomes import Skipped
|
|
20 | 25 | DATA_DIRECTORY = HERE / "data"
|
21 | 26 |
|
22 | 27 |
|
| 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 | + |
23 | 48 | @pytest.fixture(name="pytest_config")
|
24 | 49 | def pytest_config_fixture() -> MagicMock:
|
25 | 50 | def _mock_getoption(option: str) -> bool:
|
@@ -69,6 +94,37 @@ def test_get_functional_test_files_from_crowded_directory() -> None:
|
69 | 94 | assert "max_overflow" not in str(exc_info.value)
|
70 | 95 |
|
71 | 96 |
|
| 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 | + |
72 | 128 | def test_minimal_messages_config_enabled(pytest_config: MagicMock) -> None:
|
73 | 129 | """Test that all messages not targeted in the functional test are disabled
|
74 | 130 | when running with --minimal-messages-config.
|
|
0 commit comments