add exec to shell subcommand

This commit is contained in:
Evgenii Alekseev 2022-11-10 18:46:35 +02:00
parent 791ce4f242
commit 41080c5ff8
3 changed files with 19 additions and 1 deletions

View File

@ -762,6 +762,7 @@ def _set_shell_parser(root: SubParserAction) -> argparse.ArgumentParser:
parser = root.add_parser("shell", help="envoke python shell",
description="drop into python shell while having created application",
formatter_class=_formatter)
parser.add_argument("code", help="instead of dropping into shell, just execute the specified code", nargs="?")
parser.add_argument("-v", "--verbose", help=argparse.SUPPRESS, action="store_true")
parser.set_defaults(handler=handlers.Shell, lock=None, report=False)
return parser

View File

@ -56,4 +56,7 @@ class Shell(Handler):
# licensed by https://creativecommons.org/licenses/by-sa/3.0
path = Path(sys.prefix) / "share" / "ahriman" / "templates" / "shell"
StringPrinter(path.read_text(encoding="utf8")).print(verbose=False)
if args.code is None:
code.interact(local=locals())
else:
code.InteractiveConsole(locals=locals()).runcode(args.code)

View File

@ -17,6 +17,7 @@ def _default_args(args: argparse.Namespace) -> argparse.Namespace:
Returns:
argparse.Namespace: generated arguments for these test cases
"""
args.code = None
args.verbose = False
return args
@ -33,6 +34,19 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc
application_mock.assert_called_once_with(local=pytest.helpers.anyvar(int))
def test_run_eval(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command
"""
args = _default_args(args)
args.code = """print("hello world")"""
mocker.patch("ahriman.models.repository_paths.RepositoryPaths.tree_create")
application_mock = mocker.patch("code.InteractiveConsole.runcode")
Shell.run(args, "x86_64", configuration, report=False, unsafe=False)
application_mock.assert_called_once_with(args.code)
def test_run_verbose(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
"""
must run command with verbose option