diff options
author | Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> | 2021-10-17 10:33:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-17 10:33:57 +0200 |
commit | 89cd646ea8c9cdee041c12fa543ef0cad2128b12 (patch) | |
tree | 087bb12f4dc7e768ec048bbd471768175505428c /tests/pyreverse | |
parent | dbced8d8a694a3bd3c2e17a8e1ed42188afc02d8 (diff) | |
download | pylint-git-89cd646ea8c9cdee041c12fa543ef0cad2128b12.tar.gz |
pyreverse - Add project modules to sys.path (#4856)
* pyreverse - Add project modules to sys.path
- Use the fix_import_path context manager in the existing pyreverse
tests
- Style refactor:
- Use a constant for the path
- Move the constants closer to the top of the module
- Add a test for the fix_import_path context manager
to ensure the project root directory is in sys.path
- Prevent re-defining from outer scope
- Add a type-hint & docstrings
Closes #2479
Diffstat (limited to 'tests/pyreverse')
-rw-r--r-- | tests/pyreverse/conftest.py | 5 | ||||
-rw-r--r-- | tests/pyreverse/test_main.py | 31 | ||||
-rw-r--r-- | tests/pyreverse/test_writer.py | 29 |
3 files changed, 48 insertions, 17 deletions
diff --git a/tests/pyreverse/conftest.py b/tests/pyreverse/conftest.py index cb9e0780a..9536fbcb0 100644 --- a/tests/pyreverse/conftest.py +++ b/tests/pyreverse/conftest.py @@ -3,6 +3,7 @@ from typing import Callable, Optional import pytest from astroid.nodes.scoped_nodes import Module +from pylint.lint import fix_import_path from pylint.pyreverse.inspector import Project, project_from_files from pylint.testutils.pyreverse import PyreverseConfig @@ -50,6 +51,8 @@ def get_project() -> Callable: def _astroid_wrapper(func: Callable, modname: str) -> Module: return func(modname) - return project_from_files([module], _astroid_wrapper, project_name=name) + with fix_import_path([module]): + project = project_from_files([module], _astroid_wrapper, project_name=name) + return project return _get_project diff --git a/tests/pyreverse/test_main.py b/tests/pyreverse/test_main.py new file mode 100644 index 000000000..792bcbb8b --- /dev/null +++ b/tests/pyreverse/test_main.py @@ -0,0 +1,31 @@ +"""Unittest for the main module""" +import os +import sys +from typing import Iterator + +import pytest + +from pylint.lint import fix_import_path + +TEST_DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data")) +PROJECT_ROOT_DIR = os.path.abspath(os.path.join(TEST_DATA_DIR, "..")) + + +@pytest.fixture(params=[PROJECT_ROOT_DIR, TEST_DATA_DIR]) +def setup_path(request) -> Iterator: + current_sys_path = list(sys.path) + sys.path[:] = [] + current_dir = os.getcwd() + os.chdir(request.param) + yield + os.chdir(current_dir) + sys.path[:] = current_sys_path + + +@pytest.mark.usefixtures("setup_path") +def test_project_root_in_sys_path(): + """Test the context manager adds the project root directory to sys.path. + This should happen when pyreverse is run from any directory + """ + with fix_import_path([TEST_DATA_DIR]): + assert sys.path == [PROJECT_ROOT_DIR] diff --git a/tests/pyreverse/test_writer.py b/tests/pyreverse/test_writer.py index 9826dfa39..dc6394262 100644 --- a/tests/pyreverse/test_writer.py +++ b/tests/pyreverse/test_writer.py @@ -51,6 +51,14 @@ _DEFAULTS = { "output_directory": "", } +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") + +DOT_FILES = ["packages_No_Name.dot", "classes_No_Name.dot"] +COLORIZED_DOT_FILES = ["packages_colorized.dot", "classes_colorized.dot"] +VCG_FILES = ["packages_No_Name.vcg", "classes_No_Name.vcg"] +PUML_FILES = ["packages_No_Name.puml", "classes_No_Name.puml"] +COLORIZED_PUML_FILES = ["packages_colorized.puml", "classes_colorized.puml"] + class Config: """config object for tests""" @@ -74,17 +82,10 @@ def _file_lines(path: str) -> List[str]: return [line for line in lines if line] -DOT_FILES = ["packages_No_Name.dot", "classes_No_Name.dot"] -COLORIZED_DOT_FILES = ["packages_colorized.dot", "classes_colorized.dot"] -VCG_FILES = ["packages_No_Name.vcg", "classes_No_Name.vcg"] -PUML_FILES = ["packages_No_Name.puml", "classes_No_Name.puml"] -COLORIZED_PUML_FILES = ["packages_colorized.puml", "classes_colorized.puml"] - - @pytest.fixture() def setup_dot(default_config: PyreverseConfig, get_project: Callable) -> Iterator: writer = DiagramWriter(default_config) - project = get_project(os.path.join(os.path.dirname(__file__), "..", "data")) + project = get_project(TEST_DATA_DIR) yield from _setup(project, default_config, writer) @@ -93,23 +94,21 @@ def setup_colorized_dot( colorized_dot_config: PyreverseConfig, get_project: Callable ) -> Iterator: writer = DiagramWriter(colorized_dot_config) - project = get_project( - os.path.join(os.path.dirname(__file__), "..", "data"), name="colorized" - ) + project = get_project(TEST_DATA_DIR, name="colorized") yield from _setup(project, colorized_dot_config, writer) @pytest.fixture() def setup_vcg(vcg_config: PyreverseConfig, get_project: Callable) -> Iterator: writer = DiagramWriter(vcg_config) - project = get_project(os.path.join(os.path.dirname(__file__), "..", "data")) + project = get_project(TEST_DATA_DIR) yield from _setup(project, vcg_config, writer) @pytest.fixture() def setup_puml(puml_config: PyreverseConfig, get_project: Callable) -> Iterator: writer = DiagramWriter(puml_config) - project = get_project(os.path.join(os.path.dirname(__file__), "..", "data")) + project = get_project(TEST_DATA_DIR) yield from _setup(project, puml_config, writer) @@ -118,9 +117,7 @@ def setup_colorized_puml( colorized_puml_config: PyreverseConfig, get_project: Callable ) -> Iterator: writer = DiagramWriter(colorized_puml_config) - project = get_project( - os.path.join(os.path.dirname(__file__), "..", "data"), name="colorized" - ) + project = get_project(TEST_DATA_DIR, name="colorized") yield from _setup(project, colorized_puml_config, writer) |