Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions tests/unit/cpex/tools/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pytest
# We use typer's CliRunner for testing typer apps
import click
import typer
from typer.testing import CliRunner

# Third-Party
Expand Down Expand Up @@ -852,10 +853,12 @@ def test_install_requires_type_parameter(self):
mock_catalog = Mock()
with (
patch("cpex.tools.cli.console") as mock_console,
pytest.raises(click.exceptions.Exit) as exc_info,
patch("cpex.tools.cli._install_from_monorepo") as mock_install,
):
install("source", "", mock_catalog)
assert exc_info.value.exit_code == 2 # EXIT_INVALID_ARGS
# When install_type is None, it defaults to "monorepo" and calls _install_from_monorepo
# We need to mock it to avoid actual installation
mock_install.return_value = None
install("source", None, mock_catalog)


class TestSearchFunction:
Expand Down Expand Up @@ -1167,9 +1170,15 @@ def test_uninstall_plugin_not_found(self, temp_registry_dir):

with (
patch("cpex.tools.cli.console") as mock_console,
pytest.raises(click.exceptions.Exit) as exc_info,
patch("cpex.tools.cli.PluginRegistry") as mock_registry_class,
):
uninstall("nonexistent_plugin", mock_catalog)
# Mock empty registry
mock_registry = Mock()
mock_registry.registry.plugins = []
mock_registry_class.return_value = mock_registry

with pytest.raises(typer.Exit) as exc_info:
uninstall("nonexistent_plugin", mock_catalog)
assert exc_info.value.exit_code == 3 # EXIT_NOT_FOUND
mock_console.print.assert_called_with(":x: Plugin 'nonexistent_plugin' is not installed.")

Expand Down Expand Up @@ -1281,7 +1290,6 @@ def test_uninstall_handles_exception(self, temp_registry_dir):
patch("cpex.tools.cli.console") as mock_console,
patch("cpex.tools.cli.logger") as mock_logger,
patch("cpex.tools.cli.PluginCatalog") as mock_catalog_class,
pytest.raises(click.exceptions.Exit) as exc_info,
):
# Mock the catalog instance created inside uninstall()
mock_catalog_instance = Mock()
Expand All @@ -1294,7 +1302,8 @@ def test_uninstall_handles_exception(self, temp_registry_dir):
mock_status.__exit__ = Mock(return_value=False)
mock_console.status = Mock(return_value=mock_status)

uninstall("test_plugin", mock_catalog)
with pytest.raises(typer.Exit) as exc_info:
uninstall("test_plugin", mock_catalog)

assert exc_info.value.exit_code == 4 # EXIT_OPERATION_FAILED
mock_console.print.assert_any_call(":x: Failed to uninstall test_plugin: Uninstall failed")
Expand Down Expand Up @@ -1864,14 +1873,12 @@ def test_install_with_unsupported_type_raises_error(self):
"""Test that install raises typer.Exit for unsupported installation type."""
from cpex.tools.cli import install
from cpex.tools.catalog import PluginCatalog

mock_catalog = Mock(spec=PluginCatalog)

with (
patch("cpex.tools.cli.console") as mock_console,
pytest.raises(click.exceptions.Exit) as exc_info,
):
install("test_plugin", "unsupported_type", mock_catalog)

with patch("cpex.tools.cli.console") as mock_console:
with pytest.raises(typer.Exit) as exc_info:
install("test_plugin", "unsupported_type", mock_catalog)
assert exc_info.value.exit_code == 2 # EXIT_INVALID_ARGS


Expand Down Expand Up @@ -1992,27 +1999,27 @@ def test_uninstall_when_manifest_not_found(self, temp_registry_dir):
]
}
registry_file.write_text(json.dumps(registry_data))

mock_catalog = Mock()

with (
patch("cpex.tools.cli.inquirer.prompt", return_value={"confirm": True}),
patch("cpex.tools.cli.console") as mock_console,
patch("cpex.tools.cli.PluginCatalog") as mock_catalog_class,
pytest.raises(click.exceptions.Exit) as exc_info,
):
# Mock the catalog.find method to return None (manifest not found)
mock_catalog_instance = Mock()
mock_catalog_instance.find = Mock(return_value=None)
mock_catalog_class.return_value = mock_catalog_instance

mock_status = Mock()
mock_status.__enter__ = Mock(return_value=mock_status)
mock_status.__exit__ = Mock(return_value=False)
mock_console.status = Mock(return_value=mock_status)

uninstall("test_plugin", mock_catalog)


with pytest.raises(typer.Exit) as exc_info:
uninstall("test_plugin", mock_catalog)

assert exc_info.value.exit_code == 3 # EXIT_NOT_FOUND
# When manifest is not found, uninstall should print error and exit
# So uninstall_package should NOT be called
Expand Down
Loading