diff --git a/testing/src/scenario/context.py b/testing/src/scenario/context.py index 411beed9b..662f6054a 100644 --- a/testing/src/scenario/context.py +++ b/testing/src/scenario/context.py @@ -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], @@ -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, diff --git a/testing/src/scenario/state.py b/testing/src/scenario/state.py index d7e24bffc..1a3551fcb 100644 --- a/testing/src/scenario/state.py +++ b/testing/src/scenario/state.py @@ -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 diff --git a/testing/tests/test_charm_spec_autoload.py b/testing/tests/test_charm_spec_autoload.py index a5b24d1ee..5c457b9e6 100644 --- a/testing/tests/test_charm_spec_autoload.py +++ b/testing/tests/test_charm_spec_autoload.py @@ -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(