Skip to content

Commit

Permalink
test: introduce new error with filereference
Browse files Browse the repository at this point in the history
use the new hidden attributes for nicer error messages
  • Loading branch information
schuellerf committed Oct 8, 2024
1 parent fbe6154 commit 3d6103c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
9 changes: 5 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 @@ -40,7 +41,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 @@ -50,10 +51,10 @@ 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")
raise ParseError(f"otk file contains top-level keys without a target:\n{'\n'.join(targetless_keys)}")

target_available = _targets(deserialized_data)
if not target_available:
Expand Down
12 changes: 5 additions & 7 deletions src/otk/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 Expand Up @@ -253,7 +253,7 @@ def process_defines(ctx: Context, state: State, tree: Any) -> None:
ctx.define(state.define_subkey(key), value)


def process_include(ctx: Context, state: State, path: pathlib.Path, explain: bool = False) -> dict:
def process_include(ctx: Context, state: State, path: pathlib.Path, explain: bool = False) -> HiddenAttrDict:
"""
Load a yaml file and send it to resolve() for processing.
"""
Expand All @@ -263,10 +263,8 @@ def process_include(ctx: Context, state: State, path: pathlib.Path, explain: boo
path = (cur_path / pathlib.Path(path)).resolve()
log.info("resolving %s", path)

if explain:
# callbacks to store information about the source of all data in the yaml files
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, hidden_attr_dict_constructor)
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, hidden_attr_list_constructor)
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, hidden_attr_dict_constructor)
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, hidden_attr_list_constructor)

try:
with path.open(encoding="utf8") as fp:
Expand All @@ -286,7 +284,7 @@ def process_include(ctx: Context, state: State, path: pathlib.Path, explain: boo

if data is not None:
return resolve(ctx, state.copy(path=path), data)
return {}
return HiddenAttrDict()


def op(ctx: Context, state: State, tree: Any, key: str) -> Any:
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 3d6103c

Please sign in to comment.