Perform check_config service in current process (#13017)

* Perform check_config service in current process

* feedback
This commit is contained in:
Johann Kellerman 2018-03-19 23:20:04 +02:00 committed by Paulus Schoutsen
parent a04c6d5830
commit 4270bc7abb
2 changed files with 18 additions and 38 deletions

View file

@ -1,5 +1,4 @@
"""Module to help with parsing and generating configuration files."""
import asyncio
from collections import OrderedDict
# pylint: disable=no-name-in-module
from distutils.version import LooseVersion # pylint: disable=import-error
@ -7,7 +6,6 @@ import logging
import os
import re
import shutil
import sys
# pylint: disable=unused-import
from typing import Any, List, Tuple # NOQA
@ -665,22 +663,14 @@ async def async_check_ha_config_file(hass):
This method is a coroutine.
"""
proc = await asyncio.create_subprocess_exec(
sys.executable, '-m', 'homeassistant', '--script',
'check_config', '--config', hass.config.config_dir,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT, loop=hass.loop)
from homeassistant.scripts.check_config import check_ha_config_file
# Wait for the subprocess exit
log, _ = await proc.communicate()
exit_code = await proc.wait()
res = await hass.async_add_job(
check_ha_config_file, hass.config.config_dir)
# Convert to ASCII
log = RE_ASCII.sub('', log.decode())
if exit_code != 0 or RE_YAML_ERROR.search(log):
return log
return None
if not res.errors:
return None
return '\n'.join([err.message for err in res.errors])
@callback

View file

@ -27,9 +27,9 @@ from homeassistant.components.config.script import (
CONFIG_PATH as SCRIPTS_CONFIG_PATH)
from homeassistant.components.config.customize import (
CONFIG_PATH as CUSTOMIZE_CONFIG_PATH)
import homeassistant.scripts.check_config as check_config
from tests.common import (
get_test_config_dir, get_test_home_assistant, mock_coro)
from tests.common import get_test_config_dir, get_test_home_assistant
CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
@ -514,35 +514,25 @@ class TestConfig(unittest.TestCase):
assert len(self.hass.config.whitelist_external_dirs) == 1
assert "/test/config/www" in self.hass.config.whitelist_external_dirs
@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_correct(self, mock_create):
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_correct(self, mock_check):
"""Check that restart propagates to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value': mock_coro((b'output', None)),
'wait.return_value': mock_coro(0)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_coro(process_mock)
mock_check.return_value = check_config.HomeAssistantConfig()
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass), self.hass.loop
config_util.async_check_ha_config_file(self.hass),
self.hass.loop
).result() is None
@mock.patch('asyncio.create_subprocess_exec')
def test_check_ha_config_file_wrong(self, mock_create):
@mock.patch('homeassistant.scripts.check_config.check_ha_config_file')
def test_check_ha_config_file_wrong(self, mock_check):
"""Check that restart with a bad config doesn't propagate to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value':
mock_coro(('\033[34mhello'.encode('utf-8'), None)),
'wait.return_value': mock_coro(1)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_coro(process_mock)
mock_check.return_value = check_config.HomeAssistantConfig()
mock_check.return_value.add_error("bad")
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass),
self.hass.loop
).result() == 'hello'
).result() == 'bad'
# pylint: disable=redefined-outer-name