Avoid keeping config dir in path (#107760)

This commit is contained in:
Joakim Plate 2024-01-16 13:38:47 +01:00 committed by GitHub
parent 7fe6fc987b
commit 523352c97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View File

@ -1177,8 +1177,12 @@ def _async_mount_config_dir(hass: HomeAssistant) -> None:
Async friendly but not a coroutine.
"""
if hass.config.config_dir not in sys.path:
sys.path.insert(0, hass.config.config_dir)
sys.path.insert(0, hass.config.config_dir)
with suppress(ImportError):
import custom_components # pylint: disable=import-outside-toplevel # noqa: F401
sys.path.remove(hass.config.config_dir)
sys.path_importer_cache.pop(hass.config.config_dir, None)
def _lookup_path(hass: HomeAssistant) -> list[str]:

View File

@ -873,3 +873,14 @@ async def test_async_suggest_report_issue(
)
== report_issue
)
async def test_config_folder_not_in_path(hass):
"""Test that config folder is not in path."""
# Verify that we are unable to import this file from top level
with pytest.raises(ImportError):
import check_config_not_in_path # noqa: F401
# Verify that we are able to load the file with absolute path
import tests.testing_config.check_config_not_in_path # noqa: F401

View File

@ -0,0 +1 @@
"""File that should not be possible to import via direct import."""