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:
parent
3eab4a234b
commit
4b813f2460
2 changed files with 67 additions and 9 deletions
|
@ -333,12 +333,22 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
|||
|
||||
def visit_import(self, node: nodes.Import) -> None:
|
||||
"""Called when a Import node is visited."""
|
||||
if self.current_package is None:
|
||||
return
|
||||
for module, _alias in node.names:
|
||||
if module.startswith(f"{self.current_package}."):
|
||||
self.add_message("hass-relative-import", node=node)
|
||||
continue
|
||||
if module.startswith("homeassistant.components.") and module.endswith(
|
||||
"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)
|
||||
|
||||
def _visit_importfrom_relative(
|
||||
|
@ -389,6 +399,13 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
|||
node.modname.endswith(".const")
|
||||
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)
|
||||
return
|
||||
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):
|
||||
|
|
|
@ -148,22 +148,41 @@ def test_bad_import(
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"import_node",
|
||||
"import_node,module_name",
|
||||
[
|
||||
(
|
||||
"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(
|
||||
linter: UnittestLinter,
|
||||
imports_checker: BaseChecker,
|
||||
import_node: str,
|
||||
module_name: str,
|
||||
) -> None:
|
||||
"""Ensure bad root imports are rejected."""
|
||||
|
||||
node = astroid.extract_node(
|
||||
f"{import_node} #@",
|
||||
"homeassistant.components.pylint_test.climate",
|
||||
module_name,
|
||||
)
|
||||
imports_checker.visit_module(node.parent)
|
||||
|
||||
|
@ -175,23 +194,45 @@ def test_good_root_import(
|
|||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"import_node",
|
||||
"import_node,module_name",
|
||||
[
|
||||
(
|
||||
"import homeassistant.components.climate.const as climate",
|
||||
"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(
|
||||
linter: UnittestLinter,
|
||||
imports_checker: BaseChecker,
|
||||
import_node: str,
|
||||
module_name: str,
|
||||
) -> None:
|
||||
"""Ensure bad root imports are rejected."""
|
||||
|
||||
node = astroid.extract_node(
|
||||
f"{import_node} #@",
|
||||
"homeassistant.components.pylint_test.climate",
|
||||
module_name,
|
||||
)
|
||||
imports_checker.visit_module(node.parent)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue