Skip to content

Commit

Permalink
Fix (CLI): Avoid validation failures with --help
Browse files Browse the repository at this point in the history
Signed-off-by: OjusWiZard <[email protected]>
  • Loading branch information
OjusWiZard committed Oct 17, 2024
1 parent 6364a6a commit 89d0016
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
16 changes: 9 additions & 7 deletions aea/cli/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
# Copyright 2021-2024 Valory AG
# Copyright 2018-2020 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,6 +22,7 @@

import os
import shutil
import sys
from functools import update_wrapper
from pathlib import Path
from typing import Any, Callable, Dict, Tuple, Union, cast
Expand Down Expand Up @@ -178,12 +179,13 @@ def check_aea_project(

def wrapper(*args: Any, **kwargs: Any) -> Callable:
to_local_registry = cast(bool, kwargs.get("to_local_registry"))
_check_aea_project(
args,
check_aea_version=check_aea_version,
check_finger_prints=check_finger_prints,
to_local_registry=to_local_registry,
)
if not ("--help" in sys.argv or "-h" in sys.argv):
_check_aea_project(
args,
check_aea_version=check_aea_version,
check_finger_prints=check_finger_prints,
to_local_registry=to_local_registry,
)
return f(*args, **kwargs)

return update_wrapper(wrapper, f)
Expand Down
61 changes: 60 additions & 1 deletion tests/test_cli/test_scaffold/test_skills.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2021-2023 Valory AG
# Copyright 2021-2024 Valory AG
# Copyright 2018-2019 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,6 +24,7 @@
import os
import re
import shutil
import subprocess # nosec
import tempfile
import unittest.mock
from pathlib import Path
Expand All @@ -37,6 +38,7 @@
from aea.cli import cli
from aea.cli.packages import get_package_manager
from aea.configurations.base import DEFAULT_SKILL_CONFIG_FILE, DEFAULT_VERSION
from aea.configurations.constants import DEFAULT_AEA_CONFIG_FILE, PACKAGES, SKILL
from aea.configurations.data_types import PackageId
from aea.configurations.loader import make_jsonschema_base_uri
from aea.package_manager.v1 import PackageManagerV1
Expand Down Expand Up @@ -553,3 +555,60 @@ def teardown_class(cls):
shutil.rmtree(cls.t)
except (OSError, IOError):
pass


class TestScaffoldSkillHelpWithInvalidDirectory:
"""Test --help works when validation fails."""

@classmethod
def setup_class(cls):
"""Set the test up."""
cls.runner = CliRunner()
cls.agent_name = "myagent"
cls.resource_name = "myresource"
cls.cwd = os.getcwd()
cls.t = tempfile.mkdtemp()
dir_path = Path(PACKAGES)
tmp_dir = cls.t / dir_path
src_dir = cls.cwd / Path(ROOT_DIR, dir_path)
shutil.copytree(str(src_dir), str(tmp_dir))

os.chdir(cls.t)
result = cls.runner.invoke(
cli,
[*CLI_LOG_OPTION, "init", "--local", "--author", AUTHOR],
)
assert result.exit_code == 0
result = cls.runner.invoke(
cli,
[*CLI_LOG_OPTION, "create", "--local", cls.agent_name],
standalone_mode=False,
)
assert result.exit_code == 0

os.remove(Path(cls.t) / cls.agent_name / DEFAULT_AEA_CONFIG_FILE)

def test_exit_code_equal_to_0(self):
"""Test that --help works."""
result = subprocess.run( # nosec
[
"aea",
"scaffold",
SKILL,
"--help",
],
cwd=self.t,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 0, result.stderr

@classmethod
def teardown_class(cls):
"""Tear the test down."""
cls.patch.stop()
os.chdir(cls.cwd)
try:
shutil.rmtree(cls.t)
except (OSError, IOError):
pass

0 comments on commit 89d0016

Please sign in to comment.