Skip to content

Commit

Permalink
added error if meta not passed when config or actions are
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Dec 10, 2024
1 parent 0ad731a commit 480932d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
15 changes: 12 additions & 3 deletions testing/src/scenario/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ def test_foo():
Use this when calling :meth:`run` to specify the event to emit.
"""

# def test_foo():
# ctx = Context(MyCharm, config={"foo": "bar"})
# ctx.run(..., State())

def __init__(
self,
charm_type: type[CharmType],
Expand Down Expand Up @@ -503,13 +507,18 @@ def __init__(
spec: _CharmSpec[CharmType] = _CharmSpec.autoload(charm_type)
except MetadataNotFoundError as e:
raise ContextSetupError(
f"Cannot setup scenario with `charm_type`={charm_type}. "
f"Did you forget to pass `meta` to this Context?",
f"Cannot automatically setup scenario with `charm_type`={charm_type}. "
"This is likely because this charm has some nonstandard repository setup and Scenario "
"can't find the charmcraft|(metadata|config|actions) yamls in their usual location. "
"Please parse and provide their contents manually via the `meta`, `config` "
"and `actions` Context parameters.",
) from e

else:
if not meta:
meta = {"name": str(charm_type.__name__)}
raise ContextSetupError(
"Scenario doesn't support overriding `actions|config` without overriding `meta` too."
)
spec = _CharmSpec(
charm_type=charm_type,
meta=meta,
Expand Down
3 changes: 2 additions & 1 deletion testing/src/scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,8 @@ def _load_metadata(charm_root: pathlib.Path):
def autoload(charm_type: type[CharmBase]) -> _CharmSpec[CharmType]:
"""Construct a ``_CharmSpec`` object by looking up the metadata from the charm's repo root.
Will attempt to load the metadata off the ``charmcraft.yaml`` file
Will attempt to load the metadata off the ``charmcraft.yaml`` file.
On failure, will fall back to the legacy ``metadata.yaml/actions.yaml/config.yaml`` files.
"""
charm_source_path = pathlib.Path(inspect.getfile(charm_type))
charm_root = charm_source_path.parent.parent
Expand Down
17 changes: 17 additions & 0 deletions testing/tests/test_charm_spec_autoload.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ def test_no_meta_raises(tmp_path, legacy):
Context(charm)


@pytest.mark.parametrize("legacy", (True, False))
@pytest.mark.parametrize(
"params",
(
{"actions": {"foo": "bar"}},
{"config": {"foo": "bar"}},
),
)
def test_partial_meta_raises(tmp_path, legacy, params):
with create_tempcharm(
tmp_path, legacy=legacy, meta={"type": "charm", "name": "sergio"}
) as charm:
# metadata not found:
with pytest.raises(ContextSetupError):
Context(charm, **params)


@pytest.mark.parametrize("legacy", (True, False))
def test_relations_ok(tmp_path, legacy):
with create_tempcharm(
Expand Down

0 comments on commit 480932d

Please sign in to comment.