Skip to content

Commit

Permalink
test: introduce new error message with file reference
Browse files Browse the repository at this point in the history
use the new hidden attributes for nicer error messages
showing the source of the problem
  • Loading branch information
schuellerf committed Oct 8, 2024
1 parent 94839e2 commit abc2329
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/otk/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
from .transform import process_include
from .traversal import State
from .target import OSBuildTarget
from .utils import HiddenAttrDict

log = logging.getLogger(__name__)


class Omnifest:
_tree: dict[str, Any]
_tree: HiddenAttrDict[str, Any]
_ctx: CommonContext
_osbuild_ctx: OSBuildContext
_target: str
Expand All @@ -39,7 +40,7 @@ def __init__(self, path: pathlib.Path, target: str = "", *, warn_duplicated_defs
self._tree = tree

@classmethod
def ensure(cls, deserialized_data: dict[str, Any]) -> None:
def ensure(cls, deserialized_data: HiddenAttrDict[str, Any]) -> None:
"""Take a dictionary and ensure that its keys and values would be
considered an Omnifest."""

Expand All @@ -49,10 +50,11 @@ def ensure(cls, deserialized_data: dict[str, Any]) -> None:
raise ParseVersionError(f"omnifest must contain a key by the name of {NAME_VERSION!r}")

# no toplevel keys without a target or an otk directive
targetless_keys = [key for key in deserialized_data
targetless_keys = [f" * \"{key}\" in {deserialized_data.get_attribute(key, 'src')}" for key in deserialized_data
if not key.startswith(PREFIX)]
if len(targetless_keys):
raise ParseError(f"otk file contains top-level keys {targetless_keys} without a target")
targetless_keys_str = '\n'.join(targetless_keys)
raise ParseError(f"otk file contains top-level keys without a target:\n{targetless_keys_str}")

target_available = _targets(deserialized_data)
if not target_available:
Expand Down
2 changes: 1 addition & 1 deletion src/otk/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _find_data(node_list, key):
def _add_hidden_attributes(obj, key, key_data):
line_number = key_data.start_mark.line + 1
column = key_data.start_mark.column + 1
filename = key_data.start_mark.name
filename = os.path.relpath(key_data.start_mark.name, os.path.curdir)
obj.set_attribute(key, "src", f"{filename}:{line_number}")
obj.set_attribute(key, "filename", filename)
obj.set_attribute(key, "linenumber", line_number)
Expand Down
3 changes: 2 additions & 1 deletion src/otk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def _add_comments_to_list(self, obj):
ret = []

for index, value in enumerate(obj):
comment = f"# source of index {index}: {obj.get_attribute("self", "src")}"
src = obj.get_attribute("self", "src")
comment = f"# source of index {index}: {src}"
if comment:
ret.append(comment)
ret.append(self._enrich_with_comment(value))
Expand Down
3 changes: 2 additions & 1 deletion test/data/error/03-lonely-keys.err
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
otk file contains top-level keys ['targetless'] without a target
otk file contains top-level keys without a target:
* "targetless" in test/data/error/03-lonely-keys.yaml:6
3 changes: 3 additions & 0 deletions test/data/error/12-two-lonely-keys.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
otk file contains top-level keys without a target:
* "targetless" in test/data/error/12-two-lonely-keys.yaml:6
* "targetless2" in test/data/error/12-two-lonely-keys.yaml:9
10 changes: 10 additions & 0 deletions test/data/error/12-two-lonely-keys.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
otk.version: 1

otk.target.osbuild.foo:
a: 1

targetless:
key: value

targetless2:
key2: value2

0 comments on commit abc2329

Please sign in to comment.