From 78887c5d5cdc48acc6647ca130917956af60f361 Mon Sep 17 00:00:00 2001 From: Alex Harvey Date: Thu, 1 Jun 2017 23:50:04 -0700 Subject: [PATCH] =?UTF-8?q?Start=20of=20migration=20framework,=20to=20allo?= =?UTF-8?q?w=20moving=20of=20files=20in=20the=20config=20=E2=80=A6=20(#774?= =?UTF-8?q?0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Start of migration framework, to allow moving of files in the config directory to be hidden, ios.conf used as the first one to undergo this change. * Update const.py * Update test_config.py * improvement to syntax --- homeassistant/components/ios.py | 2 +- homeassistant/components/notify/ios.py | 2 +- homeassistant/config.py | 10 ++++++ homeassistant/const.py | 2 +- tests/test_config.py | 50 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/ios.py b/homeassistant/components/ios.py index 26a462bb2a8..13ccee9df3e 100644 --- a/homeassistant/components/ios.py +++ b/homeassistant/components/ios.py @@ -167,7 +167,7 @@ IDENTIFY_SCHEMA = vol.Schema({ vol.Optional(ATTR_PUSH_SOUNDS): list }, extra=vol.ALLOW_EXTRA) -CONFIGURATION_FILE = 'ios.conf' +CONFIGURATION_FILE = '.ios.conf' CONFIG_FILE = {ATTR_DEVICES: {}} diff --git a/homeassistant/components/notify/ios.py b/homeassistant/components/notify/ios.py index 469f1f3e61f..8609e1dabee 100644 --- a/homeassistant/components/notify/ios.py +++ b/homeassistant/components/notify/ios.py @@ -85,7 +85,7 @@ class iOSNotificationService(BaseNotificationService): for target in targets: if target not in ios.enabled_push_ids(): - _LOGGER.error("The target (%s) does not exist in ios.conf.", + _LOGGER.error("The target (%s) does not exist in .ios.conf.", targets) return diff --git a/homeassistant/config.py b/homeassistant/config.py index d0d7cedf370..f59bb131c8f 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -36,6 +36,10 @@ VERSION_FILE = '.HA_VERSION' CONFIG_DIR_NAME = '.homeassistant' DATA_CUSTOMIZE = 'hass_customize' +FILE_MIGRATION = [ + ["ios.conf", ".ios.conf"], + ] + DEFAULT_CORE_CONFIG = ( # Tuples (attribute, default, auto detect property, description) (CONF_NAME, 'Home', None, 'Name of the location where Home Assistant is ' @@ -292,6 +296,12 @@ def process_ha_config_upgrade(hass): with open(version_path, 'wt') as outp: outp.write(__version__) + _LOGGER.info('Migrating old system config files to new locations') + for oldf, newf in FILE_MIGRATION: + if os.path.isfile(hass.config.path(oldf)): + _LOGGER.info('Migrating %s to %s', oldf, newf) + os.rename(hass.config.path(oldf), hass.config.path(newf)) + @callback def async_log_exception(ex, domain, config, hass): diff --git a/homeassistant/const.py b/homeassistant/const.py index 18c73bdb9b7..aad46a4fd08 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -2,7 +2,7 @@ """Constants used by Home Assistant components.""" MAJOR_VERSION = 0 MINOR_VERSION = 46 -PATCH_VERSION = '0.dev0' +PATCH_VERSION = '0.dev1' __short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION) __version__ = '{}.{}'.format(__short_version__, PATCH_VERSION) REQUIRED_PYTHON_VER = (3, 4, 2) diff --git a/tests/test_config.py b/tests/test_config.py index 6d0932cd9a2..2686b597554 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -301,6 +301,56 @@ class TestConfig(unittest.TestCase): assert mock_os.path.isdir.call_count == 0 assert mock_shutil.rmtree.call_count == 0 + @mock.patch('homeassistant.config.shutil') + @mock.patch('homeassistant.config.os') + def test_migrate_file_on_upgrade(self, mock_os, mock_shutil): + """Test migrate of config files on upgrade.""" + ha_version = '0.7.0' + + mock_os.path.isdir = mock.Mock(return_value=True) + + mock_open = mock.mock_open() + + def mock_isfile(filename): + return True + + with mock.patch('homeassistant.config.open', mock_open, create=True), \ + mock.patch('homeassistant.config.os.path.isfile', mock_isfile): + opened_file = mock_open.return_value + # pylint: disable=no-member + opened_file.readline.return_value = ha_version + + self.hass.config.path = mock.Mock() + + config_util.process_ha_config_upgrade(self.hass) + + assert mock_os.rename.call_count == 1 + + @mock.patch('homeassistant.config.shutil') + @mock.patch('homeassistant.config.os') + def test_migrate_no_file_on_upgrade(self, mock_os, mock_shutil): + """Test not migrating config files on upgrade.""" + ha_version = '0.7.0' + + mock_os.path.isdir = mock.Mock(return_value=True) + + mock_open = mock.mock_open() + + def mock_isfile(filename): + return False + + with mock.patch('homeassistant.config.open', mock_open, create=True), \ + mock.patch('homeassistant.config.os.path.isfile', mock_isfile): + opened_file = mock_open.return_value + # pylint: disable=no-member + opened_file.readline.return_value = ha_version + + self.hass.config.path = mock.Mock() + + config_util.process_ha_config_upgrade(self.hass) + + assert mock_os.rename.call_count == 0 + def test_loading_configuration(self): """Test loading core config onto hass object.""" self.hass.config = mock.Mock()