Skip to content

Commit

Permalink
Add test on install command (#110)
Browse files Browse the repository at this point in the history
* Add test on install command

* Test on impl

* nit

* nit
  • Loading branch information
huchenlei authored Jul 10, 2024
1 parent 3aef517 commit 756d377
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
43 changes: 22 additions & 21 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,27 @@ def main():
app()


def mutually_exclusive_group_options():
group = []

def callback(_ctx: typer.Context, param: typer.CallbackParam, value: str):
class MutuallyExclusiveValidator:
def __init__(self):
self.group = []

def reset_for_testing(self):
self.group.clear()

def validate(self, _ctx: typer.Context, param: typer.CallbackParam, value: str):
# Add cli option to group if it was called with a value
if value is not None and param.name not in group:
group.append(param.name)
if len(group) > 1:
if value is not None and param.name not in self.group:
self.group.append(param.name)
if len(self.group) > 1:
raise typer.BadParameter(
f"option `{param.name}` is mutually exclusive with option `{group.pop()}`"
f"option `{param.name}` is mutually exclusive with option `{self.group.pop()}`"
)
return value

return callback


exclusivity_callback = mutually_exclusive_group_options()
g_exclusivity = MutuallyExclusiveValidator()
g_gpu_exclusivity = MutuallyExclusiveValidator()


@app.command(help="Display help for commands")
Expand All @@ -74,7 +78,7 @@ def entry(
typer.Option(
show_default=False,
help="Path to ComfyUI workspace",
callback=exclusivity_callback,
callback=g_exclusivity.validate,
),
] = None,
recent: Annotated[
Expand All @@ -83,7 +87,7 @@ def entry(
show_default=False,
is_flag=True,
help="Execute from recent path",
callback=exclusivity_callback,
callback=g_exclusivity.validate,
),
] = None,
here: Annotated[
Expand All @@ -92,7 +96,7 @@ def entry(
show_default=False,
is_flag=True,
help="Execute from current path",
callback=exclusivity_callback,
callback=g_exclusivity.validate,
),
] = None,
skip_prompt: Annotated[
Expand Down Expand Up @@ -143,9 +147,6 @@ def entry(
# logging.info(f"scan_dir took {end_time - start_time:.2f} seconds to run")


gpu_exclusivity_callback = mutually_exclusive_group_options()


@app.command(help="Download and install ComfyUI and ComfyUI-Manager")
@tracking.track_command()
def install(
Expand Down Expand Up @@ -176,7 +177,7 @@ def install(
typer.Option(
show_default=False,
help="Install for Nvidia gpu",
callback=gpu_exclusivity_callback,
callback=g_gpu_exclusivity.validate,
),
] = None,
cuda_version: Annotated[
Expand All @@ -187,15 +188,15 @@ def install(
typer.Option(
show_default=False,
help="Install for AMD gpu",
callback=gpu_exclusivity_callback,
callback=g_gpu_exclusivity.validate,
),
] = None,
m_series: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for Mac M-Series gpu",
callback=gpu_exclusivity_callback,
callback=g_gpu_exclusivity.validate,
),
] = None,
intel_arc: Annotated[
Expand All @@ -204,15 +205,15 @@ def install(
hidden=True,
show_default=False,
help="(Beta support) install for Intel Arc gpu, based on https://github.com/comfyanonymous/ComfyUI/pull/3439",
callback=gpu_exclusivity_callback,
callback=g_gpu_exclusivity.validate,
),
] = None,
cpu: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Install for CPU",
callback=gpu_exclusivity_callback,
callback=g_gpu_exclusivity.validate,
),
] = None,
commit: Annotated[
Expand Down
2 changes: 1 addition & 1 deletion comfy_cli/workspace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_workspace_path(self) -> Tuple[str, WorkspaceType]:
if found_comfy_repo:
return comfy_repo.working_dir, WorkspaceType.CURRENT_DIR
else:
return current_directory + "/ComfyUI", WorkspaceType.CURRENT_DIR
return os.path.join(current_directory, "ComfyUI"), WorkspaceType.CURRENT_DIR

# Check the current directory for a ComfyUI
if self.use_here is None:
Expand Down
51 changes: 51 additions & 0 deletions tests/comfy_cli/command/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import pytest
from unittest.mock import patch
from typer.testing import CliRunner

from comfy_cli.cmdline import app, g_exclusivity, g_gpu_exclusivity


@pytest.fixture(scope="function")
def runner():
g_exclusivity.reset_for_testing()
g_gpu_exclusivity.reset_for_testing()
return CliRunner()


@pytest.fixture(scope="function")
def mock_execute():
with patch("comfy_cli.command.install.execute") as mock:
yield mock


@pytest.fixture(scope="function")
def mock_prompt_select_enum():
def mocked_prompt_select_enum(
question: str, choices: list, force_prompting: bool = False
):
return choices[0]

with patch(
"comfy_cli.ui.prompt_select_enum",
new=mocked_prompt_select_enum,
) as mock:
yield mock


@pytest.mark.parametrize(
"cmd",
[
["--here", "install"],
["--workspace", "./ComfyUI", "install"],
],
)
def test_install_here(cmd, runner, mock_execute, mock_prompt_select_enum):
result = runner.invoke(app, cmd)
assert result.exit_code == 0, result.stdout

args, kwargs = mock_execute.call_args
url, manager_url, comfy_path, *_ = args
assert url == "https://github.com/comfyanonymous/ComfyUI"
assert manager_url == "https://github.com/ltdrdata/ComfyUI-Manager"
assert comfy_path == os.path.join(os.getcwd(), "ComfyUI")
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest

0 comments on commit 756d377

Please sign in to comment.