-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uninstall command implementation with tests
- Loading branch information
1 parent
a558e7a
commit 21df9ed
Showing
10 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""uninstall CLI command implementation.""" | ||
# dem/cli/command/uninstall_cmd.py | ||
|
||
from dem.core.dev_env import DevEnv | ||
from dem.core.platform import DevEnvLocalSetup | ||
from dem.cli.console import stderr, stdout | ||
|
||
|
||
def try_to_uninstall_dev_env(dev_env_to_uninstall: DevEnv, is_dev_env_installed: str, platform: DevEnvLocalSetup) -> bool: | ||
if is_dev_env_installed == "True": | ||
return platform.try_to_remove_tool_images(dev_env_to_uninstall) | ||
else: | ||
return False | ||
|
||
|
||
def execute(platform: DevEnvLocalSetup, dev_env_name: str) -> None: | ||
dev_env_to_uninstall = platform.get_dev_env_by_name(dev_env_name) | ||
is_dev_env_installed = platform.get_dev_env_status_by_name(dev_env_name) | ||
|
||
|
||
if dev_env_to_uninstall is None: | ||
stderr.print("[red]Error: The [bold]" + dev_env_name + "[/bold] Development Environment doesn't exist.") | ||
else: | ||
if True == try_to_uninstall_dev_env(dev_env_to_uninstall,is_dev_env_installed,platform): | ||
stdout.print("[green]Successfully deleted the " + dev_env_name + "![/]") | ||
platform.update_dev_env_status_in_json(dev_env_to_uninstall) | ||
|
||
else: | ||
stderr.print("[red]Error: The [bold]" + dev_env_name + "[/bold] Development Environment uninstall failed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"""Tests for the uninstall command.""" | ||
|
||
|
||
# Unit under test: | ||
import dem.cli.main as main | ||
import dem.cli.command.uninstall_cmd as uninstall_cmd | ||
|
||
# Test framework | ||
from typer.testing import CliRunner | ||
from unittest.mock import patch, MagicMock, call | ||
|
||
import docker.errors | ||
|
||
## Global test variables | ||
runner = CliRunner() | ||
|
||
|
||
@patch("dem.cli.command.uninstall_cmd.stderr.print") | ||
def test_uninstall_dev_env_invalid_name(mock_stderr_print): | ||
# Test setup | ||
test_invalid_name = "fake_dev_env_name" | ||
|
||
mock_platform = MagicMock() | ||
mock_platform.get_dev_env_by_name.return_value = None | ||
main.platform = mock_platform | ||
|
||
# Run unit under test | ||
runner_result = runner.invoke(main.typer_cli, ["uninstall", test_invalid_name], color=True) | ||
|
||
# Check expectations | ||
assert 0 == runner_result.exit_code | ||
|
||
mock_platform.get_dev_env_by_name.assert_called_once_with(test_invalid_name) | ||
mock_stderr_print.assert_called_once_with("[red]Error: The [bold]" + test_invalid_name + "[/bold] Development Environment doesn't exist.") | ||
|
||
|
||
@patch("dem.cli.command.uninstall_cmd.stdout.print") | ||
def test_uninstall_dev_env_valid_name(mock_stdout_print): | ||
# Test setup | ||
fake_dev_env_to_uninstall = MagicMock() | ||
fake_dev_env_to_uninstall.name = "dev_env" | ||
fake_dev_env_to_uninstall.installed = "True" | ||
mock_platform = MagicMock() | ||
mock_platform.get_dev_env_by_name.return_value = [fake_dev_env_to_uninstall] | ||
mock_platform.get_dev_env_status_by_name.return_value = "True" | ||
main.platform = mock_platform | ||
mock_platform.try_to_remove_tool_images.return_value = True | ||
|
||
|
||
# Run unit under test | ||
runner_result = runner.invoke(main.typer_cli, ["uninstall", fake_dev_env_to_uninstall.name ], color=True) | ||
|
||
# Check expectations | ||
assert 0 == runner_result.exit_code | ||
|
||
mock_platform.get_dev_env_by_name.assert_called_once_with(fake_dev_env_to_uninstall.name ) | ||
mock_stdout_print.assert_called_once_with("[green]Successfully deleted the " + fake_dev_env_to_uninstall.name + "![/]") | ||
|
||
|
||
@patch("dem.cli.command.uninstall_cmd.stderr.print") | ||
def test_uninstall_dev_env_valid_name_not_installed(mock_stderr_print): | ||
# Test setup | ||
fake_dev_env_to_uninstall = MagicMock() | ||
fake_dev_env_to_uninstall.name = "dev_env" | ||
fake_dev_env_to_uninstall.installed = "False" | ||
mock_platform = MagicMock() | ||
mock_platform.get_dev_env_by_name.return_value = [fake_dev_env_to_uninstall] | ||
mock_platform.get_dev_env_status_by_name.return_value = "False" | ||
main.platform = mock_platform | ||
|
||
|
||
# Run unit under test | ||
runner_result = runner.invoke(main.typer_cli, ["uninstall", fake_dev_env_to_uninstall.name ], color=True) | ||
|
||
# Check expectations | ||
assert 0 == runner_result.exit_code | ||
|
||
mock_platform.get_dev_env_by_name.assert_called_once_with(fake_dev_env_to_uninstall.name ) | ||
mock_stderr_print.assert_called_once_with("[red]Error: The [bold]" + fake_dev_env_to_uninstall.name + "[/bold] Development Environment uninstall failed") | ||
|
||
|
||
@patch("dem.cli.command.uninstall_cmd.stderr.print") | ||
def test_uninstall_dev_env_valid_name_failed(mock_stderr_print): | ||
# Test setup | ||
fake_dev_env_to_uninstall = MagicMock() | ||
fake_dev_env_to_uninstall.name = "dev_env" | ||
fake_dev_env_to_uninstall.installed = "True" | ||
mock_platform = MagicMock() | ||
mock_platform.get_dev_env_by_name.return_value = [fake_dev_env_to_uninstall] | ||
mock_platform.get_dev_env_status_by_name.return_value = "True" | ||
mock_platform.try_to_remove_tool_images.return_value = False | ||
main.platform = mock_platform | ||
|
||
|
||
# Run unit under test | ||
runner_result = runner.invoke(main.typer_cli, ["uninstall", fake_dev_env_to_uninstall.name ], color=True) | ||
|
||
# Check expectations | ||
assert 0 == runner_result.exit_code | ||
|
||
mock_platform.get_dev_env_by_name.assert_called_once_with(fake_dev_env_to_uninstall.name ) | ||
mock_stderr_print.assert_called_once_with("[red]Error: The [bold]" + fake_dev_env_to_uninstall.name + "[/bold] Development Environment uninstall failed") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters