Default legacy templates to true (#42511)

* Default legacy templates to true

* Disable legacy_templates in tests

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Paulus Schoutsen 2020-10-28 16:58:16 +01:00 committed by Franck Nijhof
parent 5b85776566
commit a34d06e363
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
3 changed files with 113 additions and 88 deletions

View file

@ -1530,7 +1530,7 @@ class Config:
self.safe_mode: bool = False self.safe_mode: bool = False
# Use legacy template behavior # Use legacy template behavior
self.legacy_templates: bool = False self.legacy_templates: bool = True
def distance(self, lat: float, lon: float) -> Optional[float]: def distance(self, lat: float, lon: float) -> Optional[float]:
"""Calculate distance from Home Assistant. """Calculate distance from Home Assistant.

View file

@ -207,6 +207,7 @@ async def async_test_home_assistant(loop):
hass.config.units = METRIC_SYSTEM hass.config.units = METRIC_SYSTEM
hass.config.media_dirs = {"local": get_test_config_dir("media")} hass.config.media_dirs = {"local": get_test_config_dir("media")}
hass.config.skip_pip = True hass.config.skip_pip = True
hass.config.legacy_templates = False
hass.config_entries = config_entries.ConfigEntries(hass, {}) hass.config_entries = config_entries.ConfigEntries(hass, {})
hass.config_entries._entries = [] hass.config_entries._entries = []

View file

@ -885,30 +885,50 @@ class TestServiceRegistry(unittest.TestCase):
self.hass.block_till_done() self.hass.block_till_done()
class TestConfig(unittest.TestCase): def test_config_defaults():
"""Test configuration methods.""" """Test config defaults."""
hass = Mock()
config = ha.Config(hass)
assert config.hass is hass
assert config.latitude == 0
assert config.longitude == 0
assert config.elevation == 0
assert config.location_name == "Home"
assert config.time_zone == dt_util.UTC
assert config.internal_url is None
assert config.external_url is None
assert config.config_source == "default"
assert config.skip_pip is False
assert config.components == set()
assert config.api is None
assert config.config_dir is None
assert config.allowlist_external_dirs == set()
assert config.allowlist_external_urls == set()
assert config.media_dirs == {}
assert config.safe_mode is False
assert config.legacy_templates is True
# pylint: disable=invalid-name
def setUp(self):
"""Set up things to be run when tests are started."""
self.config = ha.Config(None)
assert self.config.config_dir is None
def test_path_with_file(self): def test_config_path_with_file():
"""Test get_config_path method.""" """Test get_config_path method."""
self.config.config_dir = "/test/ha-config" config = ha.Config(None)
assert self.config.path("test.conf") == "/test/ha-config/test.conf" config.config_dir = "/test/ha-config"
assert config.path("test.conf") == "/test/ha-config/test.conf"
def test_path_with_dir_and_file(self):
def test_config_path_with_dir_and_file():
"""Test get_config_path method.""" """Test get_config_path method."""
self.config.config_dir = "/test/ha-config" config = ha.Config(None)
assert self.config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf" config.config_dir = "/test/ha-config"
assert config.path("dir", "test.conf") == "/test/ha-config/dir/test.conf"
def test_as_dict(self):
def test_config_as_dict():
"""Test as dict.""" """Test as dict."""
self.config.config_dir = "/test/ha-config" config = ha.Config(None)
self.config.hass = MagicMock() config.config_dir = "/test/ha-config"
type(self.config.hass.state).value = PropertyMock(return_value="RUNNING") config.hass = MagicMock()
type(config.hass.state).value = PropertyMock(return_value="RUNNING")
expected = { expected = {
"latitude": 0, "latitude": 0,
"longitude": 0, "longitude": 0,
@ -929,14 +949,16 @@ class TestConfig(unittest.TestCase):
"internal_url": None, "internal_url": None,
} }
assert expected == self.config.as_dict() assert expected == config.as_dict()
def test_is_allowed_path(self):
def test_config_is_allowed_path():
"""Test is_allowed_path method.""" """Test is_allowed_path method."""
config = ha.Config(None)
with TemporaryDirectory() as tmp_dir: with TemporaryDirectory() as tmp_dir:
# The created dir is in /tmp. This is a symlink on OS X # The created dir is in /tmp. This is a symlink on OS X
# causing this test to fail unless we resolve path first. # causing this test to fail unless we resolve path first.
self.config.allowlist_external_dirs = {os.path.realpath(tmp_dir)} config.allowlist_external_dirs = {os.path.realpath(tmp_dir)}
test_file = os.path.join(tmp_dir, "test.jpg") test_file = os.path.join(tmp_dir, "test.jpg")
with open(test_file, "w") as tmp_file: with open(test_file, "w") as tmp_file:
@ -944,9 +966,9 @@ class TestConfig(unittest.TestCase):
valid = [test_file, tmp_dir, os.path.join(tmp_dir, "notfound321")] valid = [test_file, tmp_dir, os.path.join(tmp_dir, "notfound321")]
for path in valid: for path in valid:
assert self.config.is_allowed_path(path) assert config.is_allowed_path(path)
self.config.allowlist_external_dirs = {"/home", "/var"} config.allowlist_external_dirs = {"/home", "/var"}
invalid = [ invalid = [
"/hass/config/secure", "/hass/config/secure",
@ -956,14 +978,16 @@ class TestConfig(unittest.TestCase):
test_file, test_file,
] ]
for path in invalid: for path in invalid:
assert not self.config.is_allowed_path(path) assert not config.is_allowed_path(path)
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
self.config.is_allowed_path(None) config.is_allowed_path(None)
def test_is_allowed_external_url(self):
def test_config_is_allowed_external_url():
"""Test is_allowed_external_url method.""" """Test is_allowed_external_url method."""
self.config.allowlist_external_urls = [ config = ha.Config(None)
config.allowlist_external_urls = [
"http://x.com/", "http://x.com/",
"https://y.com/bla/", "https://y.com/bla/",
"https://z.com/images/1.jpg/", "https://z.com/images/1.jpg/",
@ -977,7 +1001,7 @@ class TestConfig(unittest.TestCase):
"https://z.com/images/1.jpg", "https://z.com/images/1.jpg",
] ]
for url in valid: for url in valid:
assert self.config.is_allowed_external_url(url) assert config.is_allowed_external_url(url)
invalid = [ invalid = [
"https://a.co", "https://a.co",
@ -986,7 +1010,7 @@ class TestConfig(unittest.TestCase):
"https://z.com/images", "https://z.com/images",
] ]
for url in invalid: for url in invalid:
assert not self.config.is_allowed_external_url(url) assert not config.is_allowed_external_url(url)
async def test_event_on_update(hass): async def test_event_on_update(hass):