From 44f37d3030c34c21eabeed7d02dde1e05ecd841e Mon Sep 17 00:00:00 2001 From: OjusWiZard Date: Wed, 16 Oct 2024 19:17:00 +0530 Subject: [PATCH] Fix (scaffold): Cleanup on validation failure Signed-off-by: OjusWiZard --- autonomy/cli/scaffold_fsm.py | 3 +-- .../test_cli/test_scaffold_fsm.py | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/autonomy/cli/scaffold_fsm.py b/autonomy/cli/scaffold_fsm.py index e3428ed39d..d175ec689e 100644 --- a/autonomy/cli/scaffold_fsm.py +++ b/autonomy/cli/scaffold_fsm.py @@ -96,6 +96,7 @@ def fsm(ctx: Context, registry: str, skill_name: str, spec: str) -> None: raise click.ClickException("Skill name must end with '_abci'") ctx.registry_type = registry + scaffold_fsm = ScaffoldABCISkill(ctx, skill_name, Path(spec)) # check abstract_round_abci is in dependencies; if not, add it _add_abstract_round_abci_if_not_present(ctx) @@ -105,8 +106,6 @@ def fsm(ctx: Context, registry: str, skill_name: str, spec: str) -> None: preserve_cwd = ctx.cwd scaffold_item(ctx, SKILL, skill_name) ctx.cwd = preserve_cwd - - scaffold_fsm = ScaffoldABCISkill(ctx, skill_name, Path(spec)) scaffold_fsm.do_scaffolding() if ctx.config[TO_LOCAL_REGISTRY_FLAG]: diff --git a/tests/test_autonomy/test_cli/test_scaffold_fsm.py b/tests/test_autonomy/test_cli/test_scaffold_fsm.py index 9402601018..c11fd895b1 100644 --- a/tests/test_autonomy/test_cli/test_scaffold_fsm.py +++ b/tests/test_autonomy/test_cli/test_scaffold_fsm.py @@ -32,13 +32,16 @@ import click.testing import pytest +import yaml from aea.cli.utils.config import get_default_author_from_cli_config from aea.package_manager.base import PACKAGES_FILE from aea.test_tools.test_cases import AEATestCaseMany -# trigger population of autonomy commands import autonomy.cli.core # noqa +# trigger population of autonomy commands +from autonomy.analyse.abci.app_spec import DFASpecificationError + from packages.valory import skills from packages.valory.skills.abstract_round_abci.base import _MetaPayload @@ -149,6 +152,26 @@ def test_scaffold_fsm( self.t / self.agent_name / "skills" / skill_name / fsm_spec_file.name ).exists(), "spec file not copied in scaffolded skill" + def test_failure_cleanup(self) -> None: + """Test that directory doesn't exist if scaffold fails.""" + with open(fsm_specifications[0], "r") as f: + original_raw_spec = f.read() + f.seek(0) + spec = yaml.safe_load(f) + + spec["final_states"].append(spec["start_states"][0]) # making the spec wrong + + with open(fsm_specifications[0], "w") as f: + yaml.dump(spec, f) + + with pytest.raises(DFASpecificationError): + self.scaffold_fsm(fsm_specifications[0], skill_name="test_skill_abci") + + assert not (self.t / self.agent_name / "skills" / "test_skill_abci").exists() + + with open(fsm_specifications[0], "w") as f: + f.write(original_raw_spec) + def test_failure_due_to_name(self) -> None: """Test failure due to name.""" with pytest.raises(click.ClickException):