Adjust pylint plugin for tests directory (#78727)

* Add module_name to parametrize

* Add tests for tests directory

* Apply patch from mib1185

* Adjust plugin to allow imports from component being tested
This commit is contained in:
epenet 2022-09-19 14:27:21 +02:00 committed by GitHub
parent 3eab4a234b
commit 4b813f2460
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 9 deletions

View file

@ -333,12 +333,22 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
def visit_import(self, node: nodes.Import) -> None: def visit_import(self, node: nodes.Import) -> None:
"""Called when a Import node is visited.""" """Called when a Import node is visited."""
if self.current_package is None:
return
for module, _alias in node.names: for module, _alias in node.names:
if module.startswith(f"{self.current_package}."): if module.startswith(f"{self.current_package}."):
self.add_message("hass-relative-import", node=node) self.add_message("hass-relative-import", node=node)
continue
if module.startswith("homeassistant.components.") and module.endswith( if module.startswith("homeassistant.components.") and module.endswith(
"const" "const"
): ):
if (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == module.split(".")[2]
):
# Ignore check if the component being tested matches
# the component being imported from
continue
self.add_message("hass-component-root-import", node=node) self.add_message("hass-component-root-import", node=node)
def _visit_importfrom_relative( def _visit_importfrom_relative(
@ -389,6 +399,13 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
node.modname.endswith(".const") node.modname.endswith(".const")
or "const" in {names[0] for names in node.names} or "const" in {names[0] for names in node.names}
): ):
if (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == node.modname.split(".")[2]
):
# Ignore check if the component being tested matches
# the component being imported from
return
self.add_message("hass-component-root-import", node=node) self.add_message("hass-component-root-import", node=node)
return return
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname): if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):

View file

@ -148,22 +148,41 @@ def test_bad_import(
@pytest.mark.parametrize( @pytest.mark.parametrize(
"import_node", "import_node,module_name",
[ [
"from homeassistant.components import climate", (
"from homeassistant.components.climate import ClimateEntityFeature", "from homeassistant.components import climate",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate import ClimateEntityFeature",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.pylint_test import const",
"tests.components.pylint_test.climate",
),
(
"from homeassistant.components.pylint_test.const import CONSTANT",
"tests.components.pylint_test.climate",
),
(
"import homeassistant.components.pylint_test.const as climate",
"tests.components.pylint_test.climate",
),
], ],
) )
def test_good_root_import( def test_good_root_import(
linter: UnittestLinter, linter: UnittestLinter,
imports_checker: BaseChecker, imports_checker: BaseChecker,
import_node: str, import_node: str,
module_name: str,
) -> None: ) -> None:
"""Ensure bad root imports are rejected.""" """Ensure bad root imports are rejected."""
node = astroid.extract_node( node = astroid.extract_node(
f"{import_node} #@", f"{import_node} #@",
"homeassistant.components.pylint_test.climate", module_name,
) )
imports_checker.visit_module(node.parent) imports_checker.visit_module(node.parent)
@ -175,23 +194,45 @@ def test_good_root_import(
@pytest.mark.parametrize( @pytest.mark.parametrize(
"import_node", "import_node,module_name",
[ [
"import homeassistant.components.climate.const as climate", (
"from homeassistant.components.climate import const", "import homeassistant.components.climate.const as climate",
"from homeassistant.components.climate.const import ClimateEntityFeature", "homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate import const",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate.const import ClimateEntityFeature",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate import const",
"tests.components.pylint_test.climate",
),
(
"from homeassistant.components.climate.const import CONSTANT",
"tests.components.pylint_test.climate",
),
(
"import homeassistant.components.climate.const as climate",
"tests.components.pylint_test.climate",
),
], ],
) )
def test_bad_root_import( def test_bad_root_import(
linter: UnittestLinter, linter: UnittestLinter,
imports_checker: BaseChecker, imports_checker: BaseChecker,
import_node: str, import_node: str,
module_name: str,
) -> None: ) -> None:
"""Ensure bad root imports are rejected.""" """Ensure bad root imports are rejected."""
node = astroid.extract_node( node = astroid.extract_node(
f"{import_node} #@", f"{import_node} #@",
"homeassistant.components.pylint_test.climate", module_name,
) )
imports_checker.visit_module(node.parent) imports_checker.visit_module(node.parent)