Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: explicit sequential execution; extra args param for run/deploy commands #557

Merged
merged 13 commits into from
Aug 23, 2024
Prev Previous commit
Next Next commit
test: adding extra snapshot tests for extra args and sequential flag
aorumbayev committed Aug 21, 2024
commit e0f4da6235227b340626f8816444d8476ca3c20a
4 changes: 4 additions & 0 deletions src/algokit/core/project/run.py
Original file line number Diff line number Diff line change
@@ -211,6 +211,8 @@ def run_command(*, command: ProjectCommand, from_workspace: bool = False, extra_
if is_verbose:
log_msg = f"Command Executed: '{' '.join(cmd)}'\nOutput: {result.output}\n"
if index == len(command.commands) - 1:
if extra_args:
log_msg += f"Extra Args: '{' '.join(extra_args)}'\n"
log_msg += f"✅ {command.project_name}: '{' '.join(cmd)}' executed successfully."
logger.info(log_msg)

@@ -239,6 +241,8 @@ def _execute_command(cmd: ProjectCommand) -> None:
try:
run_command(command=cmd, from_workspace=True, extra_args=extra_args)
executed_commands = " && ".join(" ".join(command) for command in cmd.commands)
if extra_args:
executed_commands += f" {' '.join(extra_args)}"
logger.info(f"✅ {cmd.project_name}: '{executed_commands}' executed successfully.")
except Exception as e:
logger.error(f"❌ {cmd.project_name}: {e}")
39 changes: 39 additions & 0 deletions tests/project/deploy/test_deploy.py
Original file line number Diff line number Diff line change
@@ -468,3 +468,42 @@ def test_deploy_dispenser_alias(
assert passed_env_vars[env_var_name] == from_private_key(dummy_account_pk) # type: ignore[no-untyped-call]

verify(result.output, options=NamerFactory.with_parameters(alias))


def test_deploy_with_extra_args(tmp_path_factory: TempPathFactory, proc_mock: ProcMock, which_mock: WhichMock) -> None:
config_with_deploy = """
[project.deploy]
command = "command_a"
""".strip()

cwd = tmp_path_factory.mktemp("cwd")
(cwd / ALGOKIT_CONFIG).write_text(config_with_deploy, encoding="utf-8")
(cwd / ".env").touch()

cmd_resolved = which_mock.add("command_a")
proc_mock.set_output([cmd_resolved], ["command executed"])

extra_args = ["--arg1 value1 --arg2 value2"]
result = invoke(["project", "deploy", "localnet", "--", *extra_args], cwd=cwd)

assert result.exit_code == 0
assert proc_mock.called[0].command == [cmd_resolved, *extra_args]
verify(result.output)


def test_deploy_with_extra_args_and_custom_command(
tmp_path_factory: TempPathFactory, proc_mock: ProcMock, which_mock: WhichMock
) -> None:
cwd = tmp_path_factory.mktemp("cwd")
(cwd / ".env").touch()

custom_command = "custom_command"
cmd_resolved = which_mock.add(custom_command)
proc_mock.set_output([cmd_resolved], ["custom command executed"])

extra_args = ["--custom-arg1 custom-value1 --custom-arg2 custom-value2"]
result = invoke(["project", "deploy", "localnet", "--command", custom_command, "--", *extra_args], cwd=cwd)

assert result.exit_code == 0
assert proc_mock.called[0].command == [cmd_resolved, *extra_args]
verify(result.output)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
DEBUG: Deploying from project directory: {current_working_directory}
DEBUG: Loading deploy command from project config
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
Using deploy command: /bin/command_a --arg1 value1 --arg2 value2
Loading deployment environment variables...
DEBUG: Using default environment config for algod and indexer for network localnet
Deploying smart contracts from AlgoKit compliant repository 🚀
DEBUG: Running '/bin/command_a --arg1 value1 --arg2 value2' in '{current_working_directory}'
/bin/command_a: command executed
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
DEBUG: No .algokit.toml file found in the project directory.
DEBUG: Deploying from project directory: {current_working_directory}
DEBUG: Loading deploy command from project config
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
DEBUG: No .algokit.toml file found in the project directory.
Using deploy command: /bin/custom_command --custom-arg1 custom-value1 --custom-arg2 custom-value2
Loading deployment environment variables...
DEBUG: Using default environment config for algod and indexer for network localnet
Deploying smart contracts from AlgoKit compliant repository 🚀
DEBUG: Running '/bin/custom_command --custom-arg1 custom-value1 --custom-arg2 custom-value2' in '{current_working_directory}'
/bin/custom_command: custom command executed
123 changes: 123 additions & 0 deletions tests/project/run/test_run.py
Original file line number Diff line number Diff line change
@@ -477,3 +477,126 @@ def test_run_command_help_works_without_path_resolution(
verify(_format_output(result.output))

assert invoke("project run hello", cwd=cwd).exit_code == 1


def test_run_command_from_workspace_with_sequential_flag(
tmp_path_factory: TempPathFactory, which_mock: WhichMock, proc_mock: ProcMock
) -> None:
cwd = tmp_path_factory.mktemp("cwd") / "algokit_project"
projects = []
for i in range(1, 6):
projects.append(
{
"dir": f"project{i}",
"type": "contract",
"name": f"contract_project_{i}",
"command": f"hello{i}",
"description": "Prints hello",
}
)
_create_workspace_project(
workspace_dir=cwd,
projects=projects,
mock_command=True,
which_mock=which_mock,
proc_mock=proc_mock,
)

result = invoke("project run hello --sequential", cwd=cwd)
assert result.exit_code == 0
order_of_execution = [line for line in result.output.split("\n") if line.startswith("✅")]
for i in range(5):
assert f"contract_project_{i + 1}" in order_of_execution[i]


def test_run_command_from_workspace_with_order_and_sequential_flag(
tmp_path_factory: TempPathFactory, which_mock: WhichMock, proc_mock: ProcMock
) -> None:
cwd = tmp_path_factory.mktemp("cwd") / "algokit_project"
projects = []
for i in range(1, 6):
projects.append(
{
"dir": f"project{i}",
"type": "contract",
"name": f"contract_project_{i}",
"command": f"hello{i}",
"description": "Prints hello",
}
)
_create_workspace_project(
workspace_dir=cwd,
projects=projects,
mock_command=True,
which_mock=which_mock,
proc_mock=proc_mock,
custom_project_order=["contract_project_4"],
)

result = invoke("project run hello --sequential", cwd=cwd)
assert result.exit_code == 0
order_of_execution = [line for line in result.output.split("\n") if line.startswith("✅")]
assert "contract_project_4" in order_of_execution[0]


def test_run_command_from_standalone_with_extra_args(
tmp_path_factory: TempPathFactory, which_mock: WhichMock, proc_mock: ProcMock
) -> None:
"""
Verifies successful command execution within a standalone project with extra arguments.
"""
cwd = tmp_path_factory.mktemp("cwd") / "algokit_project"
cwd.mkdir()

which_mock.add("echo")
proc_mock.set_output(["echo", "Hello", "extra", "args"], ["Hello extra args"])
_create_project_config(cwd, "contract", "contract_project", "echo Hello", "Prints hello with extra args")

result = invoke("project run hello -- extra args", cwd=cwd)

assert result.exit_code == 0
verify(_format_output(result.output))
assert "Hello extra args" in result.output


def test_run_command_from_workspace_with_extra_args(
tmp_path_factory: TempPathFactory, which_mock: WhichMock, proc_mock: ProcMock
) -> None:
"""
Verifies successful command execution within a workspace project with extra arguments.
"""
cwd = tmp_path_factory.mktemp("cwd") / "algokit_project"
projects = [
{
"dir": "project1",
"type": "contract",
"name": "contract_project",
"command": "echo Hello",
"description": "Prints hello with extra args",
},
]
_create_workspace_project(
workspace_dir=cwd, projects=projects, mock_command=True, which_mock=which_mock, proc_mock=proc_mock
)

which_mock.add("echo")
proc_mock.set_output(["echo", "Hello", "extra", "args"], ["Hello extra args"])

result = invoke("project run hello -- extra args", cwd=cwd)

assert result.exit_code == 0
verify(_format_output(result.output))
assert "Hello extra args" in result.output


def test_run_command_from_workspace_with_extra_args_and_project_filter(cwd_with_workspace_sequential: Path) -> None:
"""
Verifies successful command execution within a workspace project with extra arguments and project filtering.
"""
result = invoke(
"project run hello --project-name 'contract_project' -- extra args", cwd=cwd_with_workspace_sequential
)

assert result.exit_code == 0
verify(_format_output(result.output))
assert "frontend_project" not in result.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Running `hello` command in {current_working_directory}...
Command Executed: 'echo Hello'
Output: STDOUT
STDERR
Extra Args: 'extra args'
✅ contract_project: 'echo Hello' executed successfully.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Running commands sequentially.
⏳ contract_project: 'hello' command in progress...
✅ contract_project: 'echo Hello extra args' executed successfully.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Running commands sequentially.
⏳ contract_project: 'hello' command in progress...
✅ contract_project: 'command_a extra args' executed successfully.