diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-04-01 20:34:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-01 20:34:43 +0200 |
commit | a96c50cba6026ddbdf5915b91fd785f721ca4e15 (patch) | |
tree | defa5c2448699fd6347bfc0e58ca1bfbfc2b93af /tests/config/test_find_default_config_files.py | |
parent | e01fa86c00b2cd879c44570b383115a9407547f1 (diff) | |
download | pylint-git-a96c50cba6026ddbdf5915b91fd785f721ca4e15.tar.gz |
Refactor ``find_default_config_files`` (#6067)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests/config/test_find_default_config_files.py')
-rw-r--r-- | tests/config/test_find_default_config_files.py | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/config/test_find_default_config_files.py b/tests/config/test_find_default_config_files.py index 04392781b..ca1461bad 100644 --- a/tests/config/test_find_default_config_files.py +++ b/tests/config/test_find_default_config_files.py @@ -1,13 +1,145 @@ # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt + +import contextlib +import importlib +import os +import shutil +import sys +import tempfile from pathlib import Path +from typing import Iterator import pytest +from pylint import config, testutils from pylint.config.find_default_config_files import _cfg_has_config, _toml_has_config +@pytest.fixture +def pop_pylintrc() -> None: + """Remove the PYLINTRC environment variable""" + os.environ.pop("PYLINTRC", None) + + +if os.name == "java": + if os.name == "nt": + HOME = "USERPROFILE" + else: + HOME = "HOME" +elif sys.platform == "win32": + HOME = "USERPROFILE" +else: + HOME = "HOME" + + +@contextlib.contextmanager +def fake_home() -> Iterator[None]: + """Fake a home directory.""" + folder = tempfile.mkdtemp("fake-home") + old_home = os.environ.get(HOME) + try: + os.environ[HOME] = folder + yield + finally: + os.environ.pop("PYLINTRC", "") + if old_home is None: + del os.environ[HOME] + else: + os.environ[HOME] = old_home + shutil.rmtree(folder, ignore_errors=True) + + +@contextlib.contextmanager +def tempdir() -> Iterator[str]: + """Create a temp directory and change the current location to it. + + This is supposed to be used with a *with* statement. + """ + tmp = tempfile.mkdtemp() + + # Get real path of tempfile, otherwise test fail on mac os x + current_dir = os.getcwd() + os.chdir(tmp) + abs_tmp = os.path.abspath(".") + + try: + yield abs_tmp + finally: + os.chdir(current_dir) + shutil.rmtree(abs_tmp) + + +@pytest.mark.usefixtures("pop_pylintrc") +def test_pylintrc() -> None: + """Test that the environment variable is checked for existence.""" + with fake_home(): + current_dir = os.getcwd() + os.chdir(os.path.dirname(os.path.abspath(sys.executable))) + try: + assert not list(config.find_default_config_files()) + os.environ["PYLINTRC"] = os.path.join(tempfile.gettempdir(), ".pylintrc") + assert not list(config.find_default_config_files()) + os.environ["PYLINTRC"] = "." + assert not list(config.find_default_config_files()) + finally: + os.chdir(current_dir) + importlib.reload(config) + + +@pytest.mark.usefixtures("pop_pylintrc") +def test_pylintrc_parentdir() -> None: + """Test that the first pylintrc we find is the first parent directory.""" + with tempdir() as chroot: + chroot_path = Path(chroot) + testutils.create_files( + [ + "a/pylintrc", + "a/b/__init__.py", + "a/b/pylintrc", + "a/b/c/__init__.py", + "a/b/c/d/__init__.py", + "a/b/c/d/e/.pylintrc", + ] + ) + + with fake_home(): + assert not list(config.find_default_config_files()) + + results = { + "a": chroot_path / "a" / "pylintrc", + "a/b": chroot_path / "a" / "b" / "pylintrc", + "a/b/c": chroot_path / "a" / "b" / "pylintrc", + "a/b/c/d": chroot_path / "a" / "b" / "pylintrc", + "a/b/c/d/e": chroot_path / "a" / "b" / "c" / "d" / "e" / ".pylintrc", + } + for basedir, expected in results.items(): + os.chdir(chroot_path / basedir) + assert next(config.find_default_config_files()) == expected + + +@pytest.mark.usefixtures("pop_pylintrc") +def test_pylintrc_parentdir_no_package() -> None: + """Test that we don't find a pylintrc in sub-packages.""" + with tempdir() as chroot: + with fake_home(): + chroot_path = Path(chroot) + testutils.create_files( + ["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"] + ) + assert config.find_pylintrc() is None + results = { + "a": chroot_path / "a" / "pylintrc", + "a/b": chroot_path / "a" / "b" / "pylintrc", + "a/b/c": None, + "a/b/c/d": None, + } + for basedir, expected in results.items(): + os.chdir(chroot_path / basedir) + assert next(config.find_default_config_files(), None) == expected + + @pytest.mark.parametrize( "content,expected", [ |