[BUGFIX] Do not load YAML unless dict is returned (#541)

This commit is contained in:
Jesse Bannon 2023-03-14 23:06:51 -07:00 committed by GitHub
parent e69933d525
commit f4807aa9a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 3 deletions

View file

@ -36,13 +36,18 @@ def load_yaml(file_path: str | Path) -> Dict:
try:
with open(file_path, "r", encoding="utf-8") as file:
return yaml.safe_load(file)
output = yaml.safe_load(file)
except YAMLError as yaml_exception:
logger.debug(yaml_exception)
raise InvalidYamlException(
f"'{file_path}' has invalid YAML, copy-paste it into a YAML checker to find the issue."
) from yaml_exception
if not isinstance(output, dict):
raise InvalidYamlException(f"'{file_path}' was specified but does not contain any YAML.")
return output
def dump_yaml(to_dump: Dict) -> str:
"""

View file

@ -35,17 +35,59 @@ def bad_yaml_file_path(bad_yaml) -> str:
FileHandler.delete(tmp_file.name)
@pytest.fixture
def single_int_file_path(bad_yaml) -> str:
# Do not delete the file in the context manager - for Windows compatibility
with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
tmp_file.write("0".encode("utf-8"))
tmp_file.flush()
try:
yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
@pytest.fixture
def empty_yaml_file_path(bad_yaml) -> str:
# Do not delete the file in the context manager - for Windows compatibility
with tempfile.NamedTemporaryFile(suffix=".yaml", delete=False) as tmp_file:
pass
try:
yield tmp_file.name
finally:
FileHandler.delete(tmp_file.name)
def test_load_yaml_file_not_found():
file_path = "does_not_exist.yaml"
with pytest.raises(FileNotFoundException, match=f"The file '{file_path}' does not exist."):
load_yaml(file_path=file_path)
def test_load_yaml_invalid_syntax(bad_yaml_file_path):
def test_load_yaml_invalid_syntax(bad_yaml_file_path: str):
with pytest.raises(
InvalidYamlException,
match=re.escape(
f"'{bad_yaml_file_path}' has invalid YAML, copy-paste it into a YAML checker to find the issue."
f"'{bad_yaml_file_path}' has invalid YAML, "
f"copy-paste it into a YAML checker to find the issue."
),
):
load_yaml(file_path=bad_yaml_file_path)
def test_empty_yaml(empty_yaml_file_path: str):
with pytest.raises(
InvalidYamlException,
match=re.escape(f"'{empty_yaml_file_path}' was specified but does not contain any YAML."),
):
load_yaml(file_path=empty_yaml_file_path)
def test_empty_yaml_only_int(single_int_file_path: str):
with pytest.raises(
InvalidYamlException,
match=re.escape(f"'{single_int_file_path}' was specified but does not contain any YAML."),
):
load_yaml(file_path=single_int_file_path)