Start of migration framework, to allow moving of files in the config … (#7740)

* 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
This commit is contained in:
Alex Harvey 2017-06-01 23:50:04 -07:00 committed by Paulus Schoutsen
parent 3a92bd78ea
commit 78887c5d5c
5 changed files with 63 additions and 3 deletions

View File

@ -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: {}}

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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()