Fix bootstrap circular imports (#4108)

* Fix bootstrap circular imports

* fix test

* Lint
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2016-10-29 12:54:47 -07:00
1 parent 08a65a3b31
commit 9ea1101aba
5 files changed
+140 -62

No files matched your search

+10
View File
@@ -348,6 +348,16 @@ def patch_yaml_files(files_dict, endswith=True):
return patch.object(yaml, 'open', mock_open_f, create=True)
def mock_coro(return_value=None):
"""Helper method to return a coro that returns a value."""
@asyncio.coroutine
def coro():
"""Fake coroutine."""
return return_value
return coro
@contextmanager
def assert_setup_component(count, domain=None):
"""Collect valid configuration from setup_component.
+27 -6
View File
@@ -3,8 +3,10 @@ from unittest.mock import patch
from homeassistant import loader, bootstrap
from homeassistant.helpers import discovery
from homeassistant.util.async import run_coroutine_threadsafe
from tests.common import get_test_home_assistant, MockModule, MockPlatform
from tests.common import (
get_test_home_assistant, MockModule, MockPlatform, mock_coro)
class TestHelpersDiscovery:
@@ -12,7 +14,7 @@ class TestHelpersDiscovery:
def setup_method(self, method):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant(1)
self.hass = get_test_home_assistant()
def teardown_method(self, method):
"""Stop everything that was started."""
@@ -55,7 +57,8 @@ class TestHelpersDiscovery:
assert ['test service', 'another service'] == [info[0] for info
in calls_multi]
@patch('homeassistant.bootstrap.setup_component')
@patch('homeassistant.bootstrap.async_setup_component',
return_value=mock_coro(True)())
def test_platform(self, mock_setup_component):
"""Test discover platform method."""
calls = []
@@ -91,7 +94,17 @@ class TestHelpersDiscovery:
assert len(calls) == 1
def test_circular_import(self):
"""Test we don't break doing circular import."""
"""Test we don't break doing circular import.
This test will have test_component discover the switch.test_circular
component while setting up.
The supplied config will load test_component and will load
switch.test_circular.
That means that after startup, we will have test_component and switch
setup. The test_circular platform has been loaded twice.
"""
component_calls = []
platform_calls = []
@@ -122,9 +135,17 @@ class TestHelpersDiscovery:
'platform': 'test_circular',
}],
})
# We wait for the setup_lock to finish
run_coroutine_threadsafe(
self.hass.setup_lock.acquire(), self.hass.loop).result()
self.hass.block_till_done()
# test_component will only be setup once
assert len(component_calls) == 1
# The platform will be setup once via the config in `setup_component`
# and once via the discovery inside test_component.
assert len(platform_calls) == 2
assert 'test_component' in self.hass.config.components
assert 'switch' in self.hass.config.components
assert len(component_calls) == 1
assert len(platform_calls) == 1
+4 -2
View File
@@ -13,7 +13,8 @@ from homeassistant.helpers import discovery
import homeassistant.util.dt as dt_util
from tests.common import (
get_test_home_assistant, MockPlatform, MockModule, fire_time_changed)
get_test_home_assistant, MockPlatform, MockModule, fire_time_changed,
mock_coro)
_LOGGER = logging.getLogger(__name__)
DOMAIN = "test_domain"
@@ -226,7 +227,8 @@ class TestHelpersEntityComponent(unittest.TestCase):
@patch('homeassistant.helpers.entity_component.EntityComponent'
'._async_setup_platform')
@patch('homeassistant.bootstrap.setup_component', return_value=True)
@patch('homeassistant.bootstrap.async_setup_component',
return_value=mock_coro(True)())
def test_setup_does_discovery(self, mock_setup_component, mock_setup):
"""Test setup for discovery."""
component = EntityComponent(_LOGGER, DOMAIN, self.hass)