|
| 1 | +from __future__ import annotations |
| 2 | +import contextlib |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from collections.abc import Iterator |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from mypy.defaults import CONFIG_NAMES, SHARED_CONFIG_NAMES |
| 10 | +from mypy.config_parser import _find_config_file |
| 11 | + |
| 12 | + |
| 13 | +@contextlib.contextmanager |
| 14 | +def chdir(target: Path) -> Iterator[None]: |
| 15 | + # Replace with contextlib.chdir in Python 3.11 |
| 16 | + dir = os.getcwd() |
| 17 | + os.chdir(target) |
| 18 | + try: |
| 19 | + yield |
| 20 | + finally: |
| 21 | + os.chdir(dir) |
| 22 | + |
| 23 | + |
| 24 | +def write_config(path: Path, content: str | None = None) -> None: |
| 25 | + if path.suffix == ".toml": |
| 26 | + if content is None: |
| 27 | + content = "[tool.mypy]\nstrict = true" |
| 28 | + path.write_text(content) |
| 29 | + else: |
| 30 | + if content is None: |
| 31 | + content = "[mypy]\nstrict = True" |
| 32 | + path.write_text(content) |
| 33 | + |
| 34 | + |
| 35 | +class FindConfigFileSuite(unittest.TestCase): |
| 36 | + |
| 37 | + def test_no_config(self) -> None: |
| 38 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 39 | + tmpdir = Path(_tmpdir) |
| 40 | + (tmpdir / ".git").touch() |
| 41 | + with contextlib.chdir(tmpdir): |
| 42 | + result = _find_config_file() |
| 43 | + assert result is None |
| 44 | + |
| 45 | + def test_parent_config_with_and_without_git(self) -> None: |
| 46 | + for name in CONFIG_NAMES + SHARED_CONFIG_NAMES: |
| 47 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 48 | + tmpdir = Path(_tmpdir) |
| 49 | + |
| 50 | + config = tmpdir / name |
| 51 | + write_config(config) |
| 52 | + |
| 53 | + child = tmpdir / "child" |
| 54 | + child.mkdir() |
| 55 | + |
| 56 | + with chdir(child): |
| 57 | + result = _find_config_file() |
| 58 | + assert result is not None |
| 59 | + assert Path(result[2]).resolve() == config.resolve() |
| 60 | + |
| 61 | + (child / ".git").touch() |
| 62 | + |
| 63 | + result = _find_config_file() |
| 64 | + assert result is None |
| 65 | + |
| 66 | + def test_precedence_basic(self) -> None: |
| 67 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 68 | + tmpdir = Path(_tmpdir) |
| 69 | + |
| 70 | + pyproject = tmpdir / "pyproject.toml" |
| 71 | + setup_cfg = tmpdir / "setup.cfg" |
| 72 | + mypy_ini = tmpdir / "mypy.ini" |
| 73 | + dot_mypy = tmpdir / ".mypy.ini" |
| 74 | + write_config(pyproject) |
| 75 | + write_config(setup_cfg) |
| 76 | + write_config(mypy_ini) |
| 77 | + write_config(dot_mypy) |
| 78 | + |
| 79 | + with contextlib.chdir(tmpdir): |
| 80 | + result = _find_config_file() |
| 81 | + assert result is not None |
| 82 | + assert result[2] == "mypy.ini" |
| 83 | + |
| 84 | + mypy_ini.unlink() |
| 85 | + result = _find_config_file() |
| 86 | + assert result is not None |
| 87 | + assert result[2] == ".mypy.ini" |
| 88 | + |
| 89 | + dot_mypy.unlink() |
| 90 | + result = _find_config_file() |
| 91 | + assert result is not None |
| 92 | + assert result[2] == "pyproject.toml" |
| 93 | + |
| 94 | + pyproject.unlink() |
| 95 | + result = _find_config_file() |
| 96 | + assert result is not None |
| 97 | + assert result[2] == "setup.cfg" |
| 98 | + |
| 99 | + def test_precedence_missing_section(self) -> None: |
| 100 | + with tempfile.TemporaryDirectory() as _tmpdir: |
| 101 | + tmpdir = Path(_tmpdir) |
| 102 | + |
| 103 | + child = tmpdir / "child" |
| 104 | + child.mkdir() |
| 105 | + |
| 106 | + parent_mypy = tmpdir / "mypy.ini" |
| 107 | + child_pyproject = child / "pyproject.toml" |
| 108 | + write_config(parent_mypy) |
| 109 | + write_config(child_pyproject, content="") |
| 110 | + |
| 111 | + with contextlib.chdir(child): |
| 112 | + result = _find_config_file() |
| 113 | + assert result is not None |
| 114 | + assert Path(result[2]).resolve() == parent_mypy.resolve() |
0 commit comments