Skip to content

Commit

Permalink
Merge branch 'mr/cardao/implement-qualifiers-fuzz-matching' into 'mas…
Browse files Browse the repository at this point in the history
…ter'

Improve qualifiers error

Closes e3-adacore#64

See merge request it/e3-core!20
  • Loading branch information
leocardao committed Aug 13, 2024
2 parents 5c5c1d5 + 9d29df4 commit 5d7f4e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
27 changes: 26 additions & 1 deletion src/e3/anod/qualifiers_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from difflib import get_close_matches
from typing import TYPE_CHECKING
from hashlib import sha1
from e3.anod.error import AnodError
Expand Down Expand Up @@ -708,7 +709,31 @@ def compute_qualifier_values(
)
if invalid_keys:
invalid_keys_str = ", ".join(invalid_keys)
raise AnodError(f"{self.origin}: Invalid qualifier(s): {invalid_keys_str}")
error_msg = f"{self.origin}: Invalid qualifier(s): {invalid_keys_str}\n"

if self.qualifier_decls:
probable_qualifiers = [
repr(
get_close_matches(
key, self.qualifier_decls.keys(), n=1, cutoff=0
)[0]
)
for key in invalid_keys
]
if len(probable_qualifiers) == 1:
error_msg += f"Did you mean {probable_qualifiers[0]}?\n"
else:
error_msg += (
f"Did you mean {', '.join(probable_qualifiers[:-1])} or "
f"{probable_qualifiers[-1]}?\n"
)

error_msg += (
f"Use `anod help {self.anod_instance.name}` to get a list of valid "
"qualifiers"
)

raise AnodError(error_msg)

# Update default dict with user values
result.update(
Expand Down
26 changes: 23 additions & 3 deletions tests/tests_e3/anod/test_qualifier_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ class AnodDummy(Anod):
name = "dummy_spec"
enable_name_generator = True

class AnodDummyWithQual(Anod):
name = "dummy_spec"
enable_name_generator = True

def declare_qualifiers_and_components(self, qm: QualifiersManager) -> None:
qm.declare_tag_qualifier(
name="test",
description="test",
)

anod_dummy = AnodDummy("", kind="build")

# Add a qualifier after parse
Expand Down Expand Up @@ -165,15 +175,24 @@ class AnodDummy(Anod):
with pytest.raises(AnodError) as err:
qualifiers_manager.parse({})
assert str(err.value) == (
"build(name=dummy_spec, qual={}): " "Missing qualifier(s): mandatory_qual"
"build(name=dummy_spec, qual={}): Missing qualifier(s): mandatory_qual"
)

# Use of undeclared qualifier
with pytest.raises(AnodError) as err:
AnodDummy("invalid_qual", kind="build")
assert str(err.value) == (
"build(name=dummy_spec, qual={'invalid_qual': ''}): "
"Invalid qualifier(s): invalid_qual"
"Invalid qualifier(s): invalid_qual\n"
"Use `anod help dummy_spec` to get a list of valid qualifiers"
)
with pytest.raises(AnodError) as err:
AnodDummyWithQual("tst", kind="build")
assert str(err.value) == (
"build(name=dummy_spec, qual={'tst': ''}): "
"Invalid qualifier(s): tst\n"
"Did you mean 'test'?\n"
"Use `anod help dummy_spec` to get a list of valid qualifiers"
)

# Pass a key_value qualifier with a value not in choices
Expand Down Expand Up @@ -203,7 +222,8 @@ class AnodDummy(Anod):
qualifiers_manager.parse({"test_qual": "val1"})
assert str(err.value) == (
"build(name=dummy_spec, qual={'test_qual': 'val1'}): "
"Invalid qualifier(s): test_qual"
"Invalid qualifier(s): test_qual\n"
"Use `anod help dummy_spec` to get a list of valid qualifiers"
)

# use a not declared qualifier in component
Expand Down

0 comments on commit 5d7f4e2

Please sign in to comment.