Mqtt fan fail deprecated options for classic speeds (#58992)

* Fail deprecated options

* new removed validator

* correct module_name - add tests

* Add test cant find module cv.removed

* module name from stack+1

* Remove error from log. Just throw.

* assert on thrown exception text

* cleanup formatting remove KeyStyleAdapter

* format the replacement_key and update test

* deprecated vs removed - add raise_if_present opt

* doc string update

* is deprecated
This commit is contained in:
Jan Bouwhuis
2021-11-04 17:54:27 +01:00
committed by GitHub
parent 7945facf1e
commit c3fc19915e
3 changed files with 152 additions and 38 deletions

View File

@@ -712,6 +712,46 @@ def test_deprecated_with_no_optionals(caplog, schema):
assert test_data == output
def test_deprecated_or_removed_param_and_raise(caplog, schema):
"""
Test removed or deprecation options and fail the config validation by raising an exception.
Expected behavior:
- Outputs the appropriate deprecation or removed from support error if key is detected
"""
removed_schema = vol.All(cv.deprecated("mars", raise_if_present=True), schema)
test_data = {"mars": True}
with pytest.raises(vol.Invalid) as excinfo:
removed_schema(test_data)
assert (
"The 'mars' option is deprecated, please remove it from your configuration"
in str(excinfo.value)
)
assert len(caplog.records) == 0
test_data = {"venus": True}
output = removed_schema(test_data.copy())
assert len(caplog.records) == 0
assert test_data == output
deprecated_schema = vol.All(cv.removed("mars"), schema)
test_data = {"mars": True}
with pytest.raises(vol.Invalid) as excinfo:
deprecated_schema(test_data)
assert (
"The 'mars' option was removed, please remove it from your configuration"
in str(excinfo.value)
)
assert len(caplog.records) == 0
test_data = {"venus": True}
output = deprecated_schema(test_data.copy())
assert len(caplog.records) == 0
assert test_data == output
def test_deprecated_with_replacement_key(caplog, schema):
"""
Test deprecation behaves correctly when only a replacement key is provided.
@@ -846,17 +886,43 @@ def test_deprecated_cant_find_module():
default=False,
)
with patch("inspect.getmodule", return_value=None):
# This used to raise.
cv.removed(
"mars",
default=False,
)
def test_deprecated_logger_with_config_attributes(caplog):
def test_deprecated_or_removed_logger_with_config_attributes(caplog):
"""Test if the logger outputs the correct message if the line and file attribute is available in config."""
file: str = "configuration.yaml"
line: int = 54
replacement = f"'mars' option near {file}:{line} is deprecated"
# test as deprecated option
replacement_key = "jupiter"
option_status = "is deprecated"
replacement = f"'mars' option near {file}:{line} {option_status}, please replace it with '{replacement_key}'"
config = OrderedDict([("mars", "blah")])
setattr(config, "__config_file__", file)
setattr(config, "__line__", line)
cv.deprecated("mars", replacement_key="jupiter", default=False)(config)
cv.deprecated("mars", replacement_key=replacement_key, default=False)(config)
assert len(caplog.records) == 1
assert replacement in caplog.text
caplog.clear()
assert len(caplog.records) == 0
# test as removed option
option_status = "was removed"
replacement = f"'mars' option near {file}:{line} {option_status}, please remove it from your configuration"
config = OrderedDict([("mars", "blah")])
setattr(config, "__config_file__", file)
setattr(config, "__line__", line)
cv.removed("mars", default=False, raise_if_present=False)(config)
assert len(caplog.records) == 1
assert replacement in caplog.text