1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Replacing tempfile with mock_open in tests (#3753)

- test_bootstrap.py
- test_config.py
- components/test_init.py
- components/test_panel_custom.py
- components/test_api.py
- components/notify/test_file.py
- components/notify/test_demo.py
- components/camera/test_local_file.py
- helpers/test_config_validation.py
- util/test_package.py
- util/test_yaml.py

No changes needed in:
- components/cover/test_command_line.py
- components/switch/test_command_line.py
- components/rollershutter/test_command_line.py
- components/test_shell_command.py
- components/notify/test_command_line.py

Misc changes in:
- components/mqtt/test_server.py

Also, removed some unused mock parameters in tests/components/mqtt/test_server.py.
This commit is contained in:
Rob Capellini
2016-10-17 23:16:36 -04:00
committed by Paulus Schoutsen
parent 7d67017de7
commit 272539105f
12 changed files with 403 additions and 335 deletions

View File

@@ -1,9 +1,8 @@
"""The tests for the panel_custom component."""
import os
import shutil
from tempfile import NamedTemporaryFile
import unittest
from unittest.mock import patch
from unittest.mock import Mock, patch
from homeassistant import bootstrap
from homeassistant.components import panel_custom
@@ -47,31 +46,40 @@ class TestPanelCustom(unittest.TestCase):
@patch('homeassistant.components.panel_custom.register_panel')
def test_webcomponent_custom_path(self, mock_register, _mock_setup):
"""Test if a web component is found in config panels dir."""
with NamedTemporaryFile() as fp:
config = {
'panel_custom': {
'name': 'todomvc',
'webcomponent_path': fp.name,
'sidebar_title': 'Sidebar Title',
'sidebar_icon': 'mdi:iconicon',
'url_path': 'nice_url',
'config': 5,
}
}
filename = 'mock.file'
with patch('os.path.isfile', return_value=False):
assert not bootstrap.setup_component(self.hass, 'panel_custom',
config)
assert not mock_register.called
assert bootstrap.setup_component(self.hass, 'panel_custom', config)
assert mock_register.called
args = mock_register.mock_calls[0][1]
kwargs = mock_register.mock_calls[0][2]
assert args == (self.hass, 'todomvc', fp.name)
assert kwargs == {
'config': 5,
'url_path': 'nice_url',
config = {
'panel_custom': {
'name': 'todomvc',
'webcomponent_path': filename,
'sidebar_title': 'Sidebar Title',
'sidebar_icon': 'mdi:iconicon',
'sidebar_title': 'Sidebar Title'
'url_path': 'nice_url',
'config': 5,
}
}
with patch('os.path.isfile', Mock(return_value=False)):
assert not bootstrap.setup_component(
self.hass, 'panel_custom', config
)
assert not mock_register.called
with patch('os.path.isfile', Mock(return_value=True)):
with patch('os.access', Mock(return_value=True)):
assert bootstrap.setup_component(
self.hass, 'panel_custom', config
)
assert mock_register.called
args = mock_register.mock_calls[0][1]
assert args == (self.hass, 'todomvc', filename)
kwargs = mock_register.mock_calls[0][2]
assert kwargs == {
'config': 5,
'url_path': 'nice_url',
'sidebar_icon': 'mdi:iconicon',
'sidebar_title': 'Sidebar Title'
}