File: test_functional.py

package info (click to toggle)
pylint 4.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: python: 76,515; makefile: 70
file content (80 lines) | stat: -rw-r--r-- 2,439 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt

"""Functional full-module tests for PyLint."""

from __future__ import annotations

import sys
from collections.abc import Iterator
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from _pytest.config import Config

from pylint import testutils
from pylint.constants import PY312_PLUS
from pylint.lint.pylinter import MANAGER
from pylint.testutils import UPDATE_FILE, UPDATE_OPTION
from pylint.testutils.functional import (
    FunctionalTestFile,
    LintModuleOutputUpdate,
    get_functional_test_files_from_directory,
)

if TYPE_CHECKING:
    from pylint.lint import PyLinter

FUNCTIONAL_DIR = Path(__file__).parent.resolve() / "functional"


TEST_WITH_EXPECTED_DEPRECATION = [
    "anomalous_backslash_escape",
    "anomalous_unicode_escape",
    "excess_escapes",
    "future_unicode_literals",
]


@pytest.fixture
def revert_stateful_config_changes(linter: PyLinter) -> Iterator[PyLinter]:
    yield linter
    # Revert any stateful configuration changes.
    MANAGER.brain["module_denylist"] = set()
    MANAGER.brain["prefer_stubs"] = False


@pytest.mark.usefixtures("revert_stateful_config_changes")
@pytest.mark.parametrize(
    "test_file",
    get_functional_test_files_from_directory(FUNCTIONAL_DIR),
    ids=lambda x: x.base,
)
def test_functional(test_file: FunctionalTestFile, pytestconfig: Config) -> None:
    __tracebackhide__ = True  # pylint: disable=unused-variable
    lint_test: LintModuleOutputUpdate | testutils.LintModuleTest
    if UPDATE_FILE.exists():
        lint_test = LintModuleOutputUpdate(test_file, pytestconfig)
    else:
        lint_test = testutils.LintModuleTest(test_file, pytestconfig)
    lint_test.setUp()

    if test_file.base in TEST_WITH_EXPECTED_DEPRECATION:
        exception_type = SyntaxWarning if PY312_PLUS else DeprecationWarning
        with pytest.warns(exception_type, match="invalid escape sequence"):
            lint_test.runTest()
    else:
        lint_test.runTest()


if __name__ == "__main__":
    if UPDATE_OPTION in sys.argv:
        UPDATE_FILE.touch()
        sys.argv.remove(UPDATE_OPTION)
    try:
        pytest.main(sys.argv)
    finally:
        if UPDATE_FILE.exists():
            UPDATE_FILE.unlink()