Skip to content

Commit

Permalink
Drop dependency on py module (#7829)
Browse files Browse the repository at this point in the history
* pytest 7.2 no longer depends on py so it's better to drop it.

`tmp_path` fixture is newer and uses `pathlib.Path` from stdlib
instead of `LocalPath` from `py._path`.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Co-authored-by: Andreas Finkler <3929834+DudeNr33@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 23, 2022
1 parent 694d302 commit 7ebaad8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
7 changes: 3 additions & 4 deletions tests/config/test_per_directory_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt


from py._path.local import LocalPath
from pathlib import Path

from pylint.lint import Run


def test_fall_back_on_base_config(tmpdir: LocalPath) -> None:
def test_fall_back_on_base_config(tmp_path: Path) -> None:
"""Test that we correctly fall back on the base config."""
# A file under the current dir should fall back to the highest level
# For pylint this is ./pylintrc
test_file = tmpdir / "test.py"
test_file = tmp_path / "test.py"
runner = Run([__name__], exit=False)
assert id(runner.linter.config) == id(runner.linter._base_config)

Expand Down
15 changes: 8 additions & 7 deletions tests/lint/test_pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt

import os
from pathlib import Path
from typing import Any, NoReturn
from unittest import mock
from unittest.mock import patch

import pytest
from _pytest.recwarn import WarningsRecorder
from py._path.local import LocalPath
from pytest import CaptureFixture

from pylint.lint.pylinter import PyLinter
Expand All @@ -21,16 +22,16 @@ def raise_exception(*args: Any, **kwargs: Any) -> NoReturn:

@patch.object(FileState, "iter_spurious_suppression_messages", raise_exception)
def test_crash_in_file(
linter: PyLinter, capsys: CaptureFixture[str], tmpdir: LocalPath
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(tmpdir / "pylint-crash-%Y")
linter.crash_file_path = str(tmp_path / "pylint-crash-%Y")
linter.check(args)
out, err = capsys.readouterr()
assert not out
assert not err
files = tmpdir.listdir() # type: ignore[no-untyped-call]
files = os.listdir(tmp_path)
assert len(files) == 1
assert "pylint-crash-20" in str(files[0])
assert any(m.symbol == "fatal" for m in linter.reporter.messages)
Expand All @@ -43,17 +44,17 @@ def test_check_deprecation(linter: PyLinter, recwarn: WarningsRecorder) -> None:


def test_crash_during_linting(
linter: PyLinter, capsys: CaptureFixture[str], tmpdir: LocalPath
linter: PyLinter, capsys: CaptureFixture[str], tmp_path: Path
) -> None:
with mock.patch(
"pylint.lint.PyLinter.check_astroid_module", side_effect=RuntimeError
):
linter.crash_file_path = str(tmpdir / "pylint-crash-%Y")
linter.crash_file_path = str(tmp_path / "pylint-crash-%Y")
linter.check([__file__])
out, err = capsys.readouterr()
assert not out
assert not err
files = tmpdir.listdir() # type: ignore[no-untyped-call]
files = os.listdir(tmp_path)
assert len(files) == 1
assert "pylint-crash-20" in str(files[0])
assert any(m.symbol == "astroid-error" for m in linter.reporter.messages)
9 changes: 3 additions & 6 deletions tests/pyreverse/test_pyreverse_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pathlib import Path

import pytest
from py._path.local import LocalPath

from pylint.pyreverse.main import Run
from pylint.testutils.pyreverse import (
Expand All @@ -23,17 +22,15 @@
CLASS_DIAGRAM_TESTS,
ids=CLASS_DIAGRAM_TEST_IDS,
)
def test_class_diagrams(
testfile: FunctionalPyreverseTestfile, tmpdir: LocalPath
) -> None:
def test_class_diagrams(testfile: FunctionalPyreverseTestfile, tmp_path: Path) -> None:
input_file = testfile.source
for output_format in testfile.options["output_formats"]:
with pytest.raises(SystemExit) as sys_exit:
args = ["-o", f"{output_format}", "-d", str(tmpdir)]
args = ["-o", f"{output_format}", "-d", str(tmp_path)]
args.extend(testfile.options["command_line_args"])
args += [str(input_file)]
Run(args)
assert sys_exit.value.code == 0
assert testfile.source.with_suffix(f".{output_format}").read_text(
encoding="utf8"
) == Path(tmpdir / f"classes.{output_format}").read_text(encoding="utf8")
) == (tmp_path / f"classes.{output_format}").read_text(encoding="utf8")
26 changes: 13 additions & 13 deletions tests/test_pylint_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from unittest.mock import MagicMock, mock_open, patch

import pytest
from py._path.local import LocalPath

from pylint import run_epylint, run_pylint, run_pyreverse, run_symilar
from pylint.lint import Run
from pylint.testutils import GenericTestReporter as Reporter
from pylint.testutils.utils import _test_cwd

if sys.version_info >= (3, 8):
from typing import Protocol
Expand All @@ -34,20 +34,20 @@ def __call__(self, argv: Sequence[str] | None = None) -> NoReturn | None:


@pytest.mark.parametrize("runner", [run_pylint, run_pyreverse, run_symilar])
def test_runner(runner: _RunCallable, tmpdir: LocalPath) -> None:
def test_runner(runner: _RunCallable, tmp_path: pathlib.Path) -> None:
filepath = os.path.abspath(__file__)
testargs = ["", filepath]
with tmpdir.as_cwd():
with _test_cwd(tmp_path):
with patch.object(sys, "argv", testargs):
with pytest.raises(SystemExit) as err:
runner()
assert err.value.code == 0


def test_epylint(tmpdir: LocalPath) -> None:
def test_epylint(tmp_path: pathlib.Path) -> None:
"""TODO: 3.0 delete with epylint."""
filepath = os.path.abspath(__file__)
with tmpdir.as_cwd():
with _test_cwd(tmp_path):
with patch.object(sys, "argv", ["", filepath]):
with pytest.raises(SystemExit) as err:
with pytest.warns(DeprecationWarning):
Expand All @@ -56,29 +56,29 @@ def test_epylint(tmpdir: LocalPath) -> None:


@pytest.mark.parametrize("runner", [run_pylint, run_pyreverse, run_symilar])
def test_runner_with_arguments(runner: _RunCallable, tmpdir: LocalPath) -> None:
def test_runner_with_arguments(runner: _RunCallable, tmp_path: pathlib.Path) -> None:
"""Check the runners with arguments as parameter instead of sys.argv."""
filepath = os.path.abspath(__file__)
testargs = [filepath]
with tmpdir.as_cwd():
with _test_cwd(tmp_path):
with pytest.raises(SystemExit) as err:
runner(testargs)
assert err.value.code == 0


def test_epylint_with_arguments(tmpdir: LocalPath) -> None:
def test_epylint_with_arguments(tmp_path: pathlib.Path) -> None:
"""TODO: 3.0 delete with epylint."""
filepath = os.path.abspath(__file__)
testargs = [filepath]
with tmpdir.as_cwd():
with _test_cwd(tmp_path):
with pytest.raises(SystemExit) as err:
with pytest.warns(DeprecationWarning):
run_epylint(testargs)
assert err.value.code == 0


def test_pylint_argument_deduplication(
tmpdir: LocalPath, tests_directory: pathlib.Path
tmp_path: pathlib.Path, tests_directory: pathlib.Path
) -> None:
"""Check that the Pylint runner does not over-report on duplicate
arguments.
Expand All @@ -90,7 +90,7 @@ def test_pylint_argument_deduplication(
testargs = shlex.split("--report n --score n --max-branches 13")
testargs.extend([filepath] * 4)
exit_stack = contextlib.ExitStack()
exit_stack.enter_context(tmpdir.as_cwd())
exit_stack.enter_context(_test_cwd(tmp_path))
exit_stack.enter_context(patch.object(sys, "argv", testargs))
err = exit_stack.enter_context(pytest.raises(SystemExit))
with exit_stack:
Expand All @@ -99,7 +99,7 @@ def test_pylint_argument_deduplication(


def test_pylint_run_jobs_equal_zero_dont_crash_with_cpu_fraction(
tmpdir: LocalPath,
tmp_path: pathlib.Path,
) -> None:
"""Check that the pylint runner does not crash if `pylint.lint.run._query_cpu`
determines only a fraction of a CPU core to be available.
Expand All @@ -122,7 +122,7 @@ def _mock_path(*args: str, **kwargs: Any) -> pathlib.Path:

filepath = os.path.abspath(__file__)
testargs = [filepath, "--jobs=0"]
with tmpdir.as_cwd():
with _test_cwd(tmp_path):
with pytest.raises(SystemExit) as err:
with patch("builtins.open", _mock_open):
with patch("pylint.lint.run.Path", _mock_path):
Expand Down
Loading

0 comments on commit 7ebaad8

Please sign in to comment.