Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pytest requirement from ~=7.4 to ~=8.2 #9576

Merged
merged 9 commits into from
Jun 4, 2024
22 changes: 9 additions & 13 deletions pylint/testutils/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import unittest
from pathlib import Path
from typing import Any, Dict
from unittest.mock import Mock

from pylint.lint import Run

Expand Down Expand Up @@ -135,18 +134,15 @@ def get_expected_output(

def run_using_a_configuration_file(
configuration_path: Path | str, file_to_lint: str = __file__
) -> tuple[Mock, Mock, Run]:
) -> Run:
"""Simulate a run with a configuration without really launching the checks."""
configuration_path = str(configuration_path)
args = ["--rcfile", configuration_path, file_to_lint]
# We do not capture the `SystemExit` as then the `runner` variable
# would not be accessible outside the `with` block.
with unittest.mock.patch("sys.exit") as mocked_exit:
# Do not actually run checks, that could be slow. We don't mock
# `PyLinter.check`: it calls `PyLinter.initialize` which is
# needed to properly set up messages inclusion/exclusion
# in `_msg_states`, used by `is_message_enabled`.
check = "pylint.lint.pylinter.check_parallel"
with unittest.mock.patch(check) as mocked_check_parallel:
runner = Run(args)
return mocked_exit, mocked_check_parallel, runner
# Do not actually run checks, that could be slow. We don't mock
# `PyLinter.check`: it calls `PyLinter.initialize` which is
# needed to properly set up messages inclusion/exclusion
# in `_msg_states`, used by `is_message_enabled`.
check = "pylint.lint.pylinter.check_parallel"
with unittest.mock.patch(check):
runner = Run(args, exit=False)
return runner
2 changes: 1 addition & 1 deletion requirements_test_min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
astroid==3.2.2 # Pinned to a specific version for tests
typing-extensions~=4.12
py~=1.11.0
pytest~=7.4
pytest~=8.2
pytest-benchmark~=4.0
pytest-timeout~=2.3
towncrier~=23.11
Expand Down
10 changes: 4 additions & 6 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ def test_can_read_toml_env_variable(tmp_path: Path, file_to_lint_path: str) -> N
)
env_var = "tmp_path_env"
os.environ[env_var] = str(config_file)
mock_exit, _, runner = run_using_a_configuration_file(
f"${env_var}", file_to_lint_path
)
mock_exit.assert_called_once_with(0)
runner = run_using_a_configuration_file(f"${env_var}", file_to_lint_path)
assert runner.linter.msg_status == 0
check_configuration_file_reader(runner)


Expand Down Expand Up @@ -226,7 +224,7 @@ def test_disable_before_enable_all_takes_effect() -> None:
runner = Run(["--disable=fixme", "--enable=all", str(FIXME_MODULE)], exit=False)
assert not runner.linter.stats.by_msg

_, _, toml_runner = run_using_a_configuration_file(
toml_runner = run_using_a_configuration_file(
HERE
/ "functional"
/ "toml"
Expand All @@ -239,7 +237,7 @@ def test_enable_before_disable_all_takes_effect() -> None:
runner = Run(["--enable=fixme", "--disable=all", str(FIXME_MODULE)], exit=False)
assert runner.linter.stats.by_msg

_, _, toml_runner = run_using_a_configuration_file(
toml_runner = run_using_a_configuration_file(
HERE
/ "functional"
/ "toml"
Expand Down
12 changes: 4 additions & 8 deletions tests/config/test_functional_config_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ def default_configuration(
) -> PylintConfiguration:
empty_pylintrc = tmp_path / "pylintrc"
empty_pylintrc.write_text("")
mock_exit, _, runner = run_using_a_configuration_file(
str(empty_pylintrc), file_to_lint_path
)
mock_exit.assert_called_once_with(0)
runner = run_using_a_configuration_file(str(empty_pylintrc), file_to_lint_path)
assert runner.linter.msg_status == 0
return runner.linter.config.__dict__


Expand Down Expand Up @@ -88,10 +86,8 @@ def test_functional_config_loading(
warnings.filterwarnings(
"ignore", message="The use of 'MASTER'.*", category=UserWarning
)
mock_exit, _, runner = run_using_a_configuration_file(
configuration_path, file_to_lint_path
)
mock_exit.assert_called_once_with(expected_code)
runner = run_using_a_configuration_file(configuration_path, file_to_lint_path)
assert runner.linter.msg_status == expected_code
out, err = capsys.readouterr()
# 'rstrip()' applied, so we can have a final newline in the expected test file
assert expected_output.rstrip() == out.rstrip(), msg
Expand Down
12 changes: 10 additions & 2 deletions tests/lint/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from shutil import copy, rmtree
from unittest import mock

import astroid
import platformdirs
import pytest
from astroid import nodes
Expand Down Expand Up @@ -1053,7 +1054,9 @@ def test_finds_pyi_file() -> None:
exit=False,
)
assert run.linter.current_file is not None
assert run.linter.current_file.endswith("foo.pyi")
assert run.linter.current_file.endswith(
"a_module_that_we_definitely_dont_use_in_the_functional_tests.pyi"
)


def test_recursive_finds_pyi_file() -> None:
Expand All @@ -1068,7 +1071,9 @@ def test_recursive_finds_pyi_file() -> None:
exit=False,
)
assert run.linter.current_file is not None
assert run.linter.current_file.endswith("foo.pyi")
assert run.linter.current_file.endswith(
"a_module_that_we_definitely_dont_use_in_the_functional_tests.pyi"
)


def test_no_false_positive_from_pyi_stub() -> None:
Expand Down Expand Up @@ -1126,6 +1131,9 @@ def test_recursive_ignore(ignore_parameter: str, ignore_parameter_value: str) ->
):
module = os.path.abspath(join(REGRTEST_DATA_DIR, *regrtest_data_module))
assert module in linted_file_paths
# We lint the modules in `regrtest` in other tests as well. Prevent test pollution by
# explicitly clearing the astroid caches.
astroid.MANAGER.clear_cache()


def test_source_roots_globbing() -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This module is named in a particular way to prevent test pollution. It was previously named 'foo' and
# all mentions of 'foo' were wrongly resolved to this stub file.
foo = 1

def three_item_iterable(): ...
4 changes: 0 additions & 4 deletions tests/regrtest_data/pyi/foo.pyi

This file was deleted.

2 changes: 1 addition & 1 deletion tests/regrtest_data/uses_module_with_stub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""If the stub is preferred over the .py, this might emit not-an-iterable"""
from pyi.foo import three_item_iterable
from pyi.a_module_that_we_definitely_dont_use_in_the_functional_tests import three_item_iterable

for val in three_item_iterable():
print(val)