diff --git a/tests/components/test_shell_command.py b/tests/components/test_shell_command.py index 7cd7217449b..d9248d8f861 100644 --- a/tests/components/test_shell_command.py +++ b/tests/components/test_shell_command.py @@ -7,6 +7,8 @@ Tests demo component. import os import tempfile import unittest +from unittest.mock import patch +from subprocess import SubprocessError from homeassistant import core from homeassistant.components import shell_command @@ -26,13 +28,44 @@ class TestShellCommand(unittest.TestCase): """ Test if able to call a configured service. """ with tempfile.TemporaryDirectory() as tempdirname: path = os.path.join(tempdirname, 'called.txt') - shell_command.setup(self.hass, { + self.assertTrue(shell_command.setup(self.hass, { 'shell_command': { 'test_service': "touch {}".format(path) } - }) + })) self.hass.services.call('shell_command', 'test_service', blocking=True) self.assertTrue(os.path.isfile(path)) + + def test_config_not_dict(self): + """ Test if config is not a dict. """ + self.assertFalse(shell_command.setup(self.hass, { + 'shell_command': ['some', 'weird', 'list'] + })) + + def test_config_not_valid_service_names(self): + """ Test if config contains invalid service names. """ + self.assertFalse(shell_command.setup(self.hass, { + 'shell_command': { + 'this is invalid because space': 'touch bla.txt' + }})) + + @patch('homeassistant.components.shell_command.subprocess.call', + side_effect=SubprocessError) + @patch('homeassistant.components.shell_command._LOGGER.error') + def test_subprocess_raising_error(self, mock_call, mock_error): + with tempfile.TemporaryDirectory() as tempdirname: + path = os.path.join(tempdirname, 'called.txt') + self.assertTrue(shell_command.setup(self.hass, { + 'shell_command': { + 'test_service': "touch {}".format(path) + } + })) + + self.hass.services.call('shell_command', 'test_service', + blocking=True) + + self.assertFalse(os.path.isfile(path)) + self.assertEqual(1, mock_error.call_count)