Skip to content

Commit

Permalink
Give better error if unexpected Yaml format
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Jaegervall <[email protected]>
  • Loading branch information
erikbosch committed Oct 23, 2024
1 parent c5ef757 commit ad3648a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/vss_tools/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)
from vss_tools.tree import ModelValidationException, VSSNode, build_tree
from vss_tools.units_quantities import load_quantities, load_units
from vss_tools.vspec import InvalidSpecDuplicatedEntryException, load_vspec
from vss_tools.vspec import InvalidSpecDuplicatedEntryException, InvalidSpecException, load_vspec


class NameViolationException(Exception):
Expand Down Expand Up @@ -215,7 +215,7 @@ def get_trees(
try:
types_root = get_types_root(types, unique_include_dirs)
vspec_data = load_vspec(unique_include_dirs, [vspec] + list(overlays))
except InvalidSpecDuplicatedEntryException as e:
except (InvalidSpecDuplicatedEntryException, InvalidSpecException) as e:
log.critical(e)
exit(1)

Expand Down
8 changes: 8 additions & 0 deletions src/vss_tools/vspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class IncludeNotFoundException(Exception):
pass


class InvalidSpecException(Exception):
pass


class InvalidSpecDuplicatedEntryException(Exception):
pass

Expand Down Expand Up @@ -80,6 +84,10 @@ def __init__(
if self.data is None:
self.data = {}

for key, value in self.data.items():
if not isinstance(value, dict):
raise InvalidSpecException(f"{self.source.absolute()}, Invalid key value: {key}={value}")

if prefix:
tmp_data = {}
for k, v in self.data.items():
Expand Down
32 changes: 32 additions & 0 deletions tests/vspec/test_faulty_vspec_format/test_faulty_vspec_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2022 Contributors to COVESA
#
# This program and the accompanying materials are made available under the
# terms of the Mozilla Public License 2.0 which is available at
# https://www.mozilla.org/en-US/MPL/2.0/
#
# SPDX-License-Identifier: MPL-2.0

import subprocess
from pathlib import Path

import pytest

HERE = Path(__file__).resolve().parent
TEST_UNITS = HERE / ".." / "test_units.yaml"
TEST_QUANT = HERE / ".." / "test_quantities.yaml"


@pytest.mark.parametrize(
"filename, error",
[
("test_single_level.vspec", "Invalid key value: A=None"),
("test_string_as_child.vspec", "Invalid key value: A=string"),
],
)
def test_error(tmp_path, filename, error):
spec = HERE / filename
output = tmp_path / "out.json"
cmd = f"vspec export json -u {TEST_UNITS} -q {TEST_QUANT} --vspec {spec} --output {output}"
process = subprocess.run(cmd.split(), capture_output=True, text=True)
assert process.returncode != 0
assert error in process.stdout

0 comments on commit ad3648a

Please sign in to comment.