mirror of
https://github.com/esphome/esphome.git
synced 2026-09-27 06:50:22 +00:00
[yaml_util][substitutions] Repair bad tests (#19468)
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
---
|
||||
substitutions:
|
||||
name: original
|
||||
|
||||
wifi: !include
|
||||
file: includes/broken_included.yaml.txt
|
||||
vars:
|
||||
name: my_custom_ssid
|
||||
|
||||
esphome:
|
||||
# should be substituted as 'original',
|
||||
# not overwritten by vars in the !include above
|
||||
name: ${name}
|
||||
name_add_mac_suffix: true
|
||||
libraries: !include {file: includes/list.yaml, vars: {var1: Wire}}
|
||||
|
||||
esp8266:
|
||||
board: !include {file: includes/scalar.yaml, vars: {var1: nodemcu}}
|
||||
@@ -1,5 +0,0 @@
|
||||
---
|
||||
# yamllint disable-line
|
||||
ssid: ${name}
|
||||
# yamllint disable-line
|
||||
fdf: error
|
||||
@@ -1,12 +0,0 @@
|
||||
esphome:
|
||||
name: test
|
||||
|
||||
esp32:
|
||||
board: esp32dev
|
||||
|
||||
wifi:
|
||||
ap: ~
|
||||
|
||||
image:
|
||||
- id: its_a_bug
|
||||
file: "mdi:bug"
|
||||
@@ -730,6 +730,34 @@ def test_resolve_package_max_depth_exceeded(tmp_path: Path) -> None:
|
||||
processor.resolve_package(package_config, substitutions.ContextVars(), [])
|
||||
|
||||
|
||||
def test_include_non_existent_file(tmp_path: Path) -> None:
|
||||
"""!include with a file that cannot be opened raises cv.Invalid."""
|
||||
main_file = tmp_path / "main.yaml"
|
||||
main_file.write_text("result: !include non_existent_include_file.yaml\n")
|
||||
|
||||
config = yaml_util.load_yaml(main_file)
|
||||
with pytest.raises(
|
||||
cv.Invalid, match=r"Error including file 'non_existent_include_file.yaml'"
|
||||
) as exc_info:
|
||||
substitutions.do_substitution_pass(config)
|
||||
assert "main.yaml" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_include_broken_file(tmp_path: Path) -> None:
|
||||
"""!include with a file that cannot be parsed raises cv.Invalid."""
|
||||
broken_file = tmp_path / "broken_file.yaml"
|
||||
broken_file.write_text("{garbage\n")
|
||||
main_file = tmp_path / "main.yaml"
|
||||
main_file.write_text("result: !include broken_file.yaml\n")
|
||||
|
||||
config = yaml_util.load_yaml(main_file)
|
||||
with pytest.raises(
|
||||
cv.Invalid, match=r"Error including file 'broken_file.yaml'"
|
||||
) as exc_info:
|
||||
substitutions.do_substitution_pass(config)
|
||||
assert "main.yaml" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_include_filename_substitution_undefined_var(tmp_path: Path) -> None:
|
||||
"""!include with an undefined substitution variable raises cv.Invalid.
|
||||
|
||||
|
||||
@@ -54,34 +54,12 @@ def test_include_with_vars(fixture_path: Path) -> None:
|
||||
assert actual["wifi"]["ssid"] == "my_custom_ssid"
|
||||
|
||||
|
||||
def test_loading_a_broken_yaml_file(fixture_path):
|
||||
"""Ensure we fallback to pure python to give good errors."""
|
||||
yaml_file = fixture_path / "yaml_util" / "broken_includetest.yaml"
|
||||
|
||||
try:
|
||||
yaml_util.load_yaml(yaml_file)
|
||||
except EsphomeError as err:
|
||||
assert "broken_included.yaml" in str(err)
|
||||
|
||||
|
||||
def test_loading_a_yaml_file_with_a_missing_component(fixture_path):
|
||||
"""Ensure we show the filename for a yaml file with a missing component."""
|
||||
yaml_file = fixture_path / "yaml_util" / "missing_comp.yaml"
|
||||
|
||||
try:
|
||||
yaml_util.load_yaml(yaml_file)
|
||||
except EsphomeError as err:
|
||||
assert "missing_comp.yaml" in str(err)
|
||||
|
||||
|
||||
def test_loading_a_missing_file(fixture_path):
|
||||
"""We throw EsphomeError when loading a missing file."""
|
||||
yaml_file = fixture_path / "yaml_util" / "missing.yaml"
|
||||
|
||||
try:
|
||||
with pytest.raises(EsphomeError, match=r"missing.yaml"):
|
||||
yaml_util.load_yaml(yaml_file)
|
||||
except EsphomeError as err:
|
||||
assert "missing.yaml" in str(err)
|
||||
|
||||
|
||||
def test_parsing_with_custom_loader(fixture_path):
|
||||
|
||||
Reference in New Issue
Block a user