diff options
Diffstat (limited to 'tests/lint')
-rw-r--r-- | tests/lint/__init__.py | 4 | ||||
-rw-r--r-- | tests/lint/test_caching.py | 4 | ||||
-rw-r--r-- | tests/lint/test_pylinter.py | 16 | ||||
-rw-r--r-- | tests/lint/test_run_pylint.py | 4 | ||||
-rw-r--r-- | tests/lint/test_utils.py | 16 | ||||
-rw-r--r-- | tests/lint/unittest_expand_modules.py | 4 | ||||
-rw-r--r-- | tests/lint/unittest_lint.py | 149 |
7 files changed, 33 insertions, 164 deletions
diff --git a/tests/lint/__init__.py b/tests/lint/__init__.py index e8a8ff79f..cfd18ddd3 100644 --- a/tests/lint/__init__.py +++ b/tests/lint/__init__.py @@ -1,3 +1,3 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt diff --git a/tests/lint/test_caching.py b/tests/lint/test_caching.py index 55c2b54dd..e891f6810 100644 --- a/tests/lint/test_caching.py +++ b/tests/lint/test_caching.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt # Pytest fixtures work like this by design # pylint: disable=redefined-outer-name diff --git a/tests/lint/test_pylinter.py b/tests/lint/test_pylinter.py index 1d0f43819..1e4f40a00 100644 --- a/tests/lint/test_pylinter.py +++ b/tests/lint/test_pylinter.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt import os from pathlib import Path @@ -8,8 +8,6 @@ from typing import Any, NoReturn from unittest import mock from unittest.mock import patch -import pytest -from _pytest.recwarn import WarningsRecorder from pytest import CaptureFixture from pylint.lint.pylinter import PyLinter @@ -24,10 +22,8 @@ def raise_exception(*args: Any, **kwargs: Any) -> NoReturn: def test_crash_in_file( linter: PyLinter, capsys: CaptureFixture[str], tmp_path: Path ) -> None: - with pytest.warns(DeprecationWarning): - args = linter.load_command_line_configuration([__file__]) linter.crash_file_path = str(tmp_path / "pylint-crash-%Y") - linter.check(args) + linter.check([__file__]) out, err = capsys.readouterr() assert not out assert not err @@ -37,12 +33,6 @@ def test_crash_in_file( assert any(m.symbol == "fatal" for m in linter.reporter.messages) -def test_check_deprecation(linter: PyLinter, recwarn: WarningsRecorder) -> None: - linter.check("myfile.py") - msg = recwarn.pop() - assert "check function will only accept sequence" in str(msg) - - def test_crash_during_linting( linter: PyLinter, capsys: CaptureFixture[str], tmp_path: Path ) -> None: diff --git a/tests/lint/test_run_pylint.py b/tests/lint/test_run_pylint.py index 73dc26331..5057f1343 100644 --- a/tests/lint/test_run_pylint.py +++ b/tests/lint/test_run_pylint.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from pathlib import Path diff --git a/tests/lint/test_utils.py b/tests/lint/test_utils.py index 872919f72..fa449374a 100644 --- a/tests/lint/test_utils.py +++ b/tests/lint/test_utils.py @@ -1,12 +1,13 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt import unittest.mock from pathlib import Path, PosixPath import pytest +from pylint.constants import full_version from pylint.lint.utils import get_fatal_error_message, prepare_crash_report from pylint.testutils._run import _Run as Run @@ -17,19 +18,22 @@ def test_prepare_crash_report(tmp_path: PosixPath) -> None: python_content = "from shadok import MagicFaucet" with open(python_file, "w", encoding="utf8") as f: f.write(python_content) + template_path = None try: - raise Exception(exception_content) # pylint: disable=broad-exception-raised - except Exception as ex: # pylint: disable=broad-except + raise ValueError(exception_content) + except ValueError as ex: template_path = prepare_crash_report( ex, str(python_file), str(tmp_path / "pylint-crash-%Y.txt") ) - assert str(tmp_path) in str(template_path) # pylint: disable=used-before-assignment + assert str(tmp_path) in str(template_path) with open(template_path, encoding="utf8") as f: template_content = f.read() assert python_content in template_content assert exception_content in template_content assert "in test_prepare_crash_report" in template_content - assert "raise Exception(exception_content)" in template_content + assert "raise ValueError(exception_content)" in template_content + assert "<details open>" in template_content + assert full_version in template_content def test_get_fatal_error_message() -> None: diff --git a/tests/lint/unittest_expand_modules.py b/tests/lint/unittest_expand_modules.py index a8c46498c..7120a1748 100644 --- a/tests/lint/unittest_expand_modules.py +++ b/tests/lint/unittest_expand_modules.py @@ -1,6 +1,6 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations diff --git a/tests/lint/unittest_lint.py b/tests/lint/unittest_lint.py index d3faa5453..918533795 100644 --- a/tests/lint/unittest_lint.py +++ b/tests/lint/unittest_lint.py @@ -1,42 +1,38 @@ # 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 +# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt # pylint: disable=redefined-outer-name from __future__ import annotations import argparse -import datetime import os import re import sys import tempfile from collections.abc import Iterator from contextlib import contextmanager -from importlib import reload from io import StringIO from os import chdir, getcwd from os.path import abspath, dirname, join, sep from pathlib import Path from shutil import copy, rmtree +from unittest import mock import platformdirs import pytest from astroid import nodes from pytest import CaptureFixture -from pylint import checkers, config, exceptions, interfaces, lint, testutils +from pylint import checkers, constants, exceptions, interfaces, lint, testutils from pylint.checkers.utils import only_required_for_messages from pylint.constants import ( MSG_STATE_CONFIDENCE, MSG_STATE_SCOPE_CONFIG, MSG_STATE_SCOPE_MODULE, - OLD_DEFAULT_PYLINT_HOME, PYLINT_HOME, - USER_HOME, _get_pylint_home, - _warn_about_old_home, ) from pylint.exceptions import InvalidMessageError from pylint.lint import PyLinter, expand_modules @@ -116,15 +112,6 @@ def fake_path() -> Iterator[list[str]]: sys.path[:] = orig -def test_deprecated() -> None: - """Test that fix_import_path() and get_python_path() are deprecated""" - with tempdir(): - create_files(["__init__.py"]) - with pytest.deprecated_call(): - with lint.fix_import_path([""]): - expand_modules.get_python_path("__init__.py") - - def test_no_args(fake_path: list[str]) -> None: with lint.augmented_sys_path([]): assert sys.path == fake_path @@ -137,7 +124,7 @@ def test_no_args(fake_path: list[str]) -> None: def test_one_arg(fake_path: list[str], case: list[str]) -> None: with tempdir() as chroot: create_files(["a/b/__init__.py"]) - expected = [join(chroot, "a")] + fake_path + expected = [join(chroot, "a"), *fake_path] extra_sys_paths = [ expand_modules.discover_package_path(arg, []) for arg in case @@ -161,7 +148,7 @@ def test_one_arg(fake_path: list[str], case: list[str]) -> None: def test_two_similar_args(fake_path: list[str], case: list[str]) -> None: with tempdir() as chroot: create_files(["a/b/__init__.py", "a/c/__init__.py"]) - expected = [join(chroot, "a")] + fake_path + expected = [join(chroot, "a"), *fake_path] extra_sys_paths = [ expand_modules.discover_package_path(arg, []) for arg in case @@ -940,126 +927,14 @@ def pop_pylintrc() -> None: @pytest.mark.usefixtures("pop_pylintrc") def test_pylint_home() -> None: - uhome = os.path.expanduser("~") - if uhome == "~": - expected = OLD_DEFAULT_PYLINT_HOME - else: - expected = platformdirs.user_cache_dir("pylint") - assert config.PYLINT_HOME == expected + expected = platformdirs.user_cache_dir("pylint") + assert constants.PYLINT_HOME == expected assert PYLINT_HOME == expected +@mock.patch.dict(os.environ, {"PYLINTHOME": "whatever.d"}) def test_pylint_home_from_environ() -> None: - try: - pylintd = join(tempfile.gettempdir(), OLD_DEFAULT_PYLINT_HOME) - os.environ["PYLINTHOME"] = pylintd - try: - assert _get_pylint_home() == pylintd - finally: - try: - rmtree(pylintd) - except FileNotFoundError: - pass - finally: - del os.environ["PYLINTHOME"] - - -def test_warn_about_old_home(capsys: CaptureFixture[str]) -> None: - """Test that we correctly warn about old_home.""" - # Create old home - old_home = Path(USER_HOME) / OLD_DEFAULT_PYLINT_HOME - old_home.mkdir(parents=True, exist_ok=True) - - # Create spam prevention file - ten_years_ago = datetime.datetime.now() - datetime.timedelta(weeks=520) - new_prevention_file = Path(PYLINT_HOME) / ten_years_ago.strftime( - "pylint_warned_about_old_cache_already_%Y-%m-%d.temp" - ) - with open(new_prevention_file, "w", encoding="utf8") as f: - f.write("") - - # Remove current prevention file - cur_prevention_file = Path(PYLINT_HOME) / datetime.datetime.now().strftime( - "pylint_warned_about_old_cache_already_%Y-%m-%d.temp" - ) - if cur_prevention_file.exists(): - os.remove(cur_prevention_file) - - _warn_about_old_home(Path(PYLINT_HOME)) - - assert not new_prevention_file.exists() - assert cur_prevention_file.exists() - - out = capsys.readouterr() - assert "PYLINTHOME is now" in out.err - - -@pytest.mark.usefixtures("pop_pylintrc") -def test_pylintrc() -> None: - with fake_home(): - current_dir = getcwd() - chdir(os.path.dirname(os.path.abspath(sys.executable))) - # pylint: disable = too-many-try-statements - try: - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() is None - os.environ["PYLINTRC"] = join(tempfile.gettempdir(), ".pylintrc") - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() is None - os.environ["PYLINTRC"] = "." - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() is None - finally: - chdir(current_dir) - reload(config) - - -@pytest.mark.usefixtures("pop_pylintrc") -def test_pylintrc_parentdir() -> None: - with tempdir() as chroot: - 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(): - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() is None - results = { - "a": join(chroot, "a", "pylintrc"), - "a/b": join(chroot, "a", "b", "pylintrc"), - "a/b/c": join(chroot, "a", "b", "pylintrc"), - "a/b/c/d": join(chroot, "a", "b", "pylintrc"), - "a/b/c/d/e": join(chroot, "a", "b", "c", "d", "e", ".pylintrc"), - } - for basedir, expected in results.items(): - os.chdir(join(chroot, basedir)) - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() == expected - - -@pytest.mark.usefixtures("pop_pylintrc") -def test_pylintrc_parentdir_no_package() -> None: - with tempdir() as chroot: - with fake_home(): - create_files(["a/pylintrc", "a/b/pylintrc", "a/b/c/d/__init__.py"]) - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() is None - results = { - "a": join(chroot, "a", "pylintrc"), - "a/b": join(chroot, "a", "b", "pylintrc"), - "a/b/c": None, - "a/b/c/d": None, - } - for basedir, expected in results.items(): - os.chdir(join(chroot, basedir)) - with pytest.warns(DeprecationWarning): - assert config.find_pylintrc() == expected + assert _get_pylint_home() == "whatever.d" class _CustomPyLinter(PyLinter): @@ -1267,7 +1142,7 @@ def test_globbing() -> None: def test_relative_imports(initialized_linter: PyLinter) -> None: - """Regression test for https://github.com/PyCQA/pylint/issues/3651""" + """Regression test for https://github.com/pylint-dev/pylint/issues/3651""" linter = initialized_linter with tempdir() as tmpdir: create_files(["x/y/__init__.py", "x/y/one.py", "x/y/two.py"], tmpdir) @@ -1322,7 +1197,7 @@ print(submodule1) def test_lint_namespace_package_under_dir(initialized_linter: PyLinter) -> None: - """Regression test for https://github.com/PyCQA/pylint/issues/1667""" + """Regression test for https://github.com/pylint-dev/pylint/issues/1667""" linter = initialized_linter with tempdir(): create_files(["outer/namespace/__init__.py", "outer/namespace/module.py"]) |