mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Unflake folder watcher test (#13569)
* Unflake folder watcher test * Fix tests * Lint
This commit is contained in:
parent
0c0e0c36af
commit
ff9f500c51
@ -202,8 +202,5 @@ wakeonlan==1.0.0
|
|||||||
# homeassistant.components.cloud
|
# homeassistant.components.cloud
|
||||||
warrant==0.6.1
|
warrant==0.6.1
|
||||||
|
|
||||||
# homeassistant.components.folder_watcher
|
|
||||||
watchdog==0.8.3
|
|
||||||
|
|
||||||
# homeassistant.components.sensor.yahoo_finance
|
# homeassistant.components.sensor.yahoo_finance
|
||||||
yahoo-finance==1.4.0
|
yahoo-finance==1.4.0
|
||||||
|
@ -91,7 +91,6 @@ TEST_REQUIREMENTS = (
|
|||||||
'yahoo-finance',
|
'yahoo-finance',
|
||||||
'pythonwhois',
|
'pythonwhois',
|
||||||
'wakeonlan',
|
'wakeonlan',
|
||||||
'watchdog',
|
|
||||||
'vultr'
|
'vultr'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,64 +1,56 @@
|
|||||||
"""The tests for the folder_watcher component."""
|
"""The tests for the folder_watcher component."""
|
||||||
import unittest
|
from unittest.mock import Mock, patch
|
||||||
from unittest.mock import MagicMock
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from homeassistant.components import folder_watcher
|
from homeassistant.components import folder_watcher
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import MockDependency
|
||||||
|
|
||||||
CWD = os.path.join(os.path.dirname(__file__))
|
|
||||||
FILE = 'file.txt'
|
|
||||||
|
|
||||||
|
|
||||||
class TestFolderWatcher(unittest.TestCase):
|
async def test_invalid_path_setup(hass):
|
||||||
"""Test the file_watcher component."""
|
"""Test that a invalid path is not setup."""
|
||||||
|
assert not await async_setup_component(
|
||||||
|
hass, folder_watcher.DOMAIN, {
|
||||||
|
folder_watcher.DOMAIN: {
|
||||||
|
folder_watcher.CONF_FOLDER: 'invalid_path'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
def setup_method(self, method):
|
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.hass.config.whitelist_external_dirs = set((CWD))
|
|
||||||
|
|
||||||
def teardown_method(self, method):
|
async def test_valid_path_setup(hass):
|
||||||
"""Stop everything that was started."""
|
"""Test that a valid path is setup."""
|
||||||
self.hass.stop()
|
cwd = os.path.join(os.path.dirname(__file__))
|
||||||
|
hass.config.whitelist_external_dirs = set((cwd))
|
||||||
|
with patch.object(folder_watcher, 'Watcher'):
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass, folder_watcher.DOMAIN, {
|
||||||
|
folder_watcher.DOMAIN: {folder_watcher.CONF_FOLDER: cwd}
|
||||||
|
})
|
||||||
|
|
||||||
def test_invalid_path_setup(self):
|
|
||||||
"""Test that a invalid path is not setup."""
|
|
||||||
config = {
|
|
||||||
folder_watcher.DOMAIN: [{
|
|
||||||
folder_watcher.CONF_FOLDER: 'invalid_path'
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
self.assertFalse(
|
|
||||||
setup_component(self.hass, folder_watcher.DOMAIN, config))
|
|
||||||
|
|
||||||
def test_valid_path_setup(self):
|
@MockDependency('watchdog', 'events')
|
||||||
"""Test that a valid path is setup."""
|
def test_event(mock_watchdog):
|
||||||
config = {
|
"""Check that HASS events are fired correctly on watchdog event."""
|
||||||
folder_watcher.DOMAIN: [{folder_watcher.CONF_FOLDER: CWD}]
|
class MockPatternMatchingEventHandler:
|
||||||
}
|
"""Mock base class for the pattern matcher event handler."""
|
||||||
|
|
||||||
self.assertTrue(setup_component(
|
def __init__(self, patterns):
|
||||||
self.hass, folder_watcher.DOMAIN, config))
|
pass
|
||||||
|
|
||||||
def test_event(self):
|
mock_watchdog.events.PatternMatchingEventHandler = \
|
||||||
"""Check that HASS events are fired correctly on watchdog event."""
|
MockPatternMatchingEventHandler
|
||||||
from watchdog.events import FileModifiedEvent
|
hass = Mock()
|
||||||
|
handler = folder_watcher.create_event_handler(['*'], hass)
|
||||||
# Cant use setup_component as need to retrieve Watcher object.
|
handler.on_created(Mock(
|
||||||
w = folder_watcher.Watcher(CWD,
|
is_directory=False,
|
||||||
folder_watcher.DEFAULT_PATTERN,
|
src_path='/hello/world.txt',
|
||||||
self.hass)
|
event_type='created'
|
||||||
w.startup(None)
|
))
|
||||||
|
assert hass.bus.fire.called
|
||||||
self.hass.bus.fire = MagicMock()
|
assert hass.bus.fire.mock_calls[0][1][0] == folder_watcher.DOMAIN
|
||||||
|
assert hass.bus.fire.mock_calls[0][1][1] == {
|
||||||
# Trigger a fake filesystem event through the Watcher Observer emitter.
|
'event_type': 'created',
|
||||||
(emitter,) = w._observer.emitters
|
'path': '/hello/world.txt',
|
||||||
emitter.queue_event(FileModifiedEvent(FILE))
|
'file': 'world.txt',
|
||||||
|
'folder': '/hello',
|
||||||
# Wait for the event to propagate.
|
}
|
||||||
self.hass.block_till_done()
|
|
||||||
|
|
||||||
assert self.hass.bus.fire.called
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user