mirror of
https://github.com/home-assistant/core.git
synced 2025-12-27 06:28:31 +00:00
Add check_config helper (#24557)
* check_config * no ignore * tests * try tests again
This commit is contained in:
committed by
Paulus Schoutsen
parent
236debb455
commit
2e26f0bd2b
@@ -16,7 +16,6 @@ from unittest.mock import MagicMock, Mock, patch
|
||||
|
||||
import homeassistant.util.dt as date_util
|
||||
import homeassistant.util.yaml.loader as yaml_loader
|
||||
import homeassistant.util.yaml.dumper as yaml_dumper
|
||||
|
||||
from homeassistant import auth, config_entries, core as ha, loader
|
||||
from homeassistant.auth import (
|
||||
@@ -682,7 +681,6 @@ def patch_yaml_files(files_dict, endswith=True):
|
||||
raise FileNotFoundError("File not found: {}".format(fname))
|
||||
|
||||
return patch.object(yaml_loader, 'open', mock_open_f, create=True)
|
||||
return patch.object(yaml_dumper, 'open', mock_open_f, create=True)
|
||||
|
||||
|
||||
def mock_coro(return_value=None, exception=None):
|
||||
|
||||
149
tests/helpers/test_check_config.py
Normal file
149
tests/helpers/test_check_config.py
Normal file
@@ -0,0 +1,149 @@
|
||||
"""Test check_config helper."""
|
||||
import logging
|
||||
import os # noqa: F401 pylint: disable=unused-import
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.helpers.check_config import (
|
||||
async_check_ha_config_file, CheckConfigError)
|
||||
from homeassistant.config import YAML_CONFIG_FILE
|
||||
from tests.common import patch_yaml_files
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
BASE_CONFIG = (
|
||||
'homeassistant:\n'
|
||||
' name: Home\n'
|
||||
' latitude: -26.107361\n'
|
||||
' longitude: 28.054500\n'
|
||||
' elevation: 1600\n'
|
||||
' unit_system: metric\n'
|
||||
' time_zone: GMT\n'
|
||||
'\n\n'
|
||||
)
|
||||
|
||||
BAD_CORE_CONFIG = (
|
||||
'homeassistant:\n'
|
||||
' unit_system: bad\n'
|
||||
'\n\n'
|
||||
)
|
||||
|
||||
|
||||
def log_ha_config(conf):
|
||||
"""Log the returned config."""
|
||||
cnt = 0
|
||||
_LOGGER.debug("CONFIG - %s lines - %s errors", len(conf), len(conf.errors))
|
||||
for key, val in conf.items():
|
||||
_LOGGER.debug("#%s - %s: %s", cnt, key, val)
|
||||
cnt += 1
|
||||
for cnt, err in enumerate(conf.errors):
|
||||
_LOGGER.debug("error[%s] = %s", cnt, err)
|
||||
|
||||
|
||||
async def test_bad_core_config(hass, loop):
|
||||
"""Test a bad core config setup."""
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BAD_CORE_CONFIG,
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
assert isinstance(res.errors[0].message, str)
|
||||
assert res.errors[0].domain == 'homeassistant'
|
||||
assert res.errors[0].config == {'unit_system': 'bad'}
|
||||
|
||||
# Only 1 error expected
|
||||
res.errors.pop(0)
|
||||
assert not res.errors
|
||||
|
||||
|
||||
async def test_config_platform_valid(hass, loop):
|
||||
"""Test a valid platform setup."""
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + 'light:\n platform: demo',
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
assert res.keys() == {'homeassistant', 'light'}
|
||||
assert res['light'] == [{'platform': 'demo'}]
|
||||
assert not res.errors
|
||||
|
||||
|
||||
async def test_component_platform_not_found(hass, loop):
|
||||
"""Test errors if component or platform not found."""
|
||||
# Make sure they don't exist
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + 'beer:',
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
assert res.keys() == {'homeassistant'}
|
||||
assert res.errors[0] == CheckConfigError(
|
||||
'Integration not found: beer', None, None)
|
||||
|
||||
# Only 1 error expected
|
||||
res.errors.pop(0)
|
||||
assert not res.errors
|
||||
|
||||
|
||||
async def test_component_platform_not_found_2(hass, loop):
|
||||
"""Test errors if component or platform not found."""
|
||||
# Make sure they don't exist
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + 'light:\n platform: beer',
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
assert res.keys() == {'homeassistant', 'light'}
|
||||
assert res['light'] == []
|
||||
|
||||
assert res.errors[0] == CheckConfigError(
|
||||
'Integration beer not found when trying to verify its '
|
||||
'light platform.', None, None)
|
||||
|
||||
# Only 1 error expected
|
||||
res.errors.pop(0)
|
||||
assert not res.errors
|
||||
|
||||
|
||||
async def test_package_invalid(hass, loop):
|
||||
"""Test a valid platform setup."""
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + (
|
||||
' packages:\n'
|
||||
' p1:\n'
|
||||
' group: ["a"]'),
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
assert res.errors[0].domain == 'homeassistant.packages.p1.group'
|
||||
assert res.errors[0].config == {'group': ['a']}
|
||||
# Only 1 error expected
|
||||
res.errors.pop(0)
|
||||
assert not res.errors
|
||||
|
||||
assert res.keys() == {'homeassistant'}
|
||||
|
||||
|
||||
async def test_bootstrap_error(hass, loop):
|
||||
"""Test a valid platform setup."""
|
||||
files = {
|
||||
YAML_CONFIG_FILE: BASE_CONFIG + 'automation: !include no.yaml',
|
||||
}
|
||||
with patch('os.path.isfile', return_value=True), patch_yaml_files(files):
|
||||
res = await async_check_ha_config_file(hass)
|
||||
log_ha_config(res)
|
||||
|
||||
res.errors[0].domain is None
|
||||
|
||||
# Only 1 error expected
|
||||
res.errors.pop(0)
|
||||
assert not res.errors
|
||||
@@ -5,7 +5,7 @@ from unittest.mock import patch
|
||||
|
||||
import homeassistant.scripts.check_config as check_config
|
||||
from homeassistant.config import YAML_CONFIG_FILE
|
||||
from tests.common import patch_yaml_files, get_test_config_dir
|
||||
from tests.common import get_test_config_dir, patch_yaml_files
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -34,7 +34,6 @@ def normalize_yaml_files(check_dict):
|
||||
for key in sorted(check_dict['yaml_files'].keys())]
|
||||
|
||||
|
||||
# pylint: disable=no-self-use,invalid-name
|
||||
@patch('os.path.isfile', return_value=True)
|
||||
def test_bad_core_config(isfile_patch, loop):
|
||||
"""Test a bad core config setup."""
|
||||
|
||||
@@ -30,7 +30,7 @@ from homeassistant.components.config.automation import (
|
||||
CONFIG_PATH as AUTOMATIONS_CONFIG_PATH)
|
||||
from homeassistant.components.config.script import (
|
||||
CONFIG_PATH as SCRIPTS_CONFIG_PATH)
|
||||
import homeassistant.scripts.check_config as check_config
|
||||
import homeassistant.helpers.check_config as check_config
|
||||
|
||||
from tests.common import (
|
||||
get_test_config_dir, patch_yaml_files)
|
||||
@@ -555,7 +555,7 @@ async def test_loading_configuration_from_packages(hass):
|
||||
|
||||
|
||||
@asynctest.mock.patch(
|
||||
'homeassistant.scripts.check_config.check_ha_config_file')
|
||||
'homeassistant.helpers.check_config.async_check_ha_config_file')
|
||||
async def test_check_ha_config_file_correct(mock_check, hass):
|
||||
"""Check that restart propagates to stop."""
|
||||
mock_check.return_value = check_config.HomeAssistantConfig()
|
||||
@@ -563,7 +563,7 @@ async def test_check_ha_config_file_correct(mock_check, hass):
|
||||
|
||||
|
||||
@asynctest.mock.patch(
|
||||
'homeassistant.scripts.check_config.check_ha_config_file')
|
||||
'homeassistant.helpers.check_config.async_check_ha_config_file')
|
||||
async def test_check_ha_config_file_wrong(mock_check, hass):
|
||||
"""Check that restart with a bad config doesn't propagate to stop."""
|
||||
mock_check.return_value = check_config.HomeAssistantConfig()
|
||||
|
||||
Reference in New Issue
Block a user