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

tests: add testcase that shows that pyreverse will not extract the inheritance link for a flat folder as described in #7686 #9693

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pylint/testutils/pyreverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ def get_functional_test_files(
return test_files


def get_functional_test_modules(
Hannoma marked this conversation as resolved.
Show resolved Hide resolved
root_directory: Path,
) -> list[FunctionalPyreverseTestfile]:
"""Get all functional test files from the given directory."""
test_files = []
for path in root_directory.rglob("__init__.py"):
Hannoma marked this conversation as resolved.
Show resolved Hide resolved
module_directory = path.parent
module_name = module_directory.name
config_file = module_directory / f"{module_name}.rc"
if config_file.exists():
test_files.append(
FunctionalPyreverseTestfile(
source=module_directory, options=_read_config(config_file)
)
)
else:
test_files.append(
FunctionalPyreverseTestfile(
source=module_directory,
options={
"source_roots": [],
"output_formats": ["mmd"],
"command_line_args": [],
},
)
)
return test_files


def _read_config(config_file: Path) -> TestFileOptions:
config = configparser.ConfigParser()
config.read(str(config_file))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
classDiagram
class AbstractParent {
}
class Child {
}
class ConcreteChild {
}
class GenericParent {
}
Child --|> AbstractParent
ConcreteChild --|> GenericParent
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from abc import ABC
from dataclasses import dataclass
from typing import Generic, TypeVar


@dataclass
class AbstractParent(ABC):
"""parent class"""


@dataclass
class Child(AbstractParent):
"""child class"""


GenericType = TypeVar("GenericType")


class GenericParent(Generic[GenericType]):
"""parent class"""


class ConcreteChild(GenericParent[int]):
"""child class"""
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from dataclasses import dataclass

from parent import AbstractParent


@dataclass
class Child(AbstractParent):
"""child class"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
classDiagram
class Child {
}
class AbstractParent {
}
Child --|> AbstractParent
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
command_line_args=-ASmy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABC
from dataclasses import dataclass


@dataclass
class AbstractParent(ABC):
"""parent class"""
39 changes: 39 additions & 0 deletions tests/pyreverse/test_pyreverse_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
from pylint.testutils.pyreverse import (
FunctionalPyreverseTestfile,
get_functional_test_files,
get_functional_test_modules,
)

FUNCTIONAL_DIR = Path(__file__).parent / "functional"
CLASS_DIAGRAMS_DIR = FUNCTIONAL_DIR / "class_diagrams"
CLASS_DIAGRAM_TESTS = get_functional_test_files(CLASS_DIAGRAMS_DIR)
CLASS_DIAGRAM_TEST_IDS = [testfile.source.stem for testfile in CLASS_DIAGRAM_TESTS]
MODULE_DIAGRAMS_DIR = FUNCTIONAL_DIR / "modules"
MODULE_DIAGRAM_TESTS = get_functional_test_modules(MODULE_DIAGRAMS_DIR)
MODULE_DIAGRAM_TEST_IDS = [
testmodule.source.name for testmodule in MODULE_DIAGRAM_TESTS
]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -49,3 +55,36 @@ def test_class_diagrams(testfile: FunctionalPyreverseTestfile, tmp_path: Path) -
assert testfile.source.with_suffix(f".{output_format}").read_text(
encoding="utf8"
) == (tmp_path / f"classes.{output_format}").read_text(encoding="utf8")


@pytest.mark.parametrize(
"testmodule",
MODULE_DIAGRAM_TESTS,
ids=MODULE_DIAGRAM_TEST_IDS,
)
def test_modules(testmodule: FunctionalPyreverseTestfile, tmp_path: Path) -> None:
input_path = testmodule.source
if testmodule.options["source_roots"]:
source_roots = ",".join(
[
os.path.realpath(
os.path.expanduser(os.path.join(input_path, source_root))
)
for source_root in testmodule.options["source_roots"]
]
)
else:
source_roots = ""
for output_format in testmodule.options["output_formats"]:
output_file = input_path / f"{input_path.name}.{output_format}"
with pytest.raises(SystemExit) as sys_exit:
args = ["-o", f"{output_format}", "-d", str(tmp_path)]
if source_roots:
args += ["--source-roots", source_roots]
args.extend(testmodule.options["command_line_args"])
args += [str(input_path)]
Run(args)
assert sys_exit.value.code == 0
assert output_file.read_text(encoding="utf8") == (
tmp_path / f"classes.{output_format}"
).read_text(encoding="utf8")
Loading