Skip to content

Commit

Permalink
If the user wants to overwrite an installed Dev Env, the DEM asks for…
Browse files Browse the repository at this point in the history
… confirmation to uninstall it first.
  • Loading branch information
janosmurai committed Mar 8, 2024
1 parent 9abdfd6 commit 2ad39c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
38 changes: 32 additions & 6 deletions dem/cli/command/create_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,26 @@ def get_dev_env_descriptor_from_user(dev_env_name: str, tool_image_list: list[li

return dev_env_descriptor

def overwrite_existing_dev_env(original_dev_env: DevEnv, new_dev_env_descriptor: dict) -> None:
original_dev_env.tools = new_dev_env_descriptor["tools"]

def create_new_dev_env(platform: Platform, new_dev_env_descriptor: dict) -> DevEnv:
def create_new_dev_env(platform: Platform, new_dev_env_descriptor: dict) -> None:
""" Create a new Development Environment.
Args:
platform -- the platform
new_dev_env_descriptor -- the descriptor of the new Development Environment
"""
new_dev_env = DevEnv(new_dev_env_descriptor)
platform.local_dev_envs.append(new_dev_env)

def create_dev_env(platform: Platform, dev_env_name: str) -> DevEnv:
def create_dev_env(platform: Platform, dev_env_name: str) -> None:
""" Create a new Development Environment or overwrite an existing one.
Args:
platform -- the platform
dev_env_name -- the name of the Development Environment
Exceptions:
Abort -- if the name of the Development Environment contains whitespace characters
"""
if ' ' in dev_env_name:
stderr.print("The name of the Development Environment cannot contain whitespace characters!")
raise typer.Abort()
Expand All @@ -156,15 +168,29 @@ def create_dev_env(platform: Platform, dev_env_name: str) -> DevEnv:
typer.confirm("The input name is already used by a Development Environment. Overwrite it?",
abort=True)

if dev_env_original.is_installed:
typer.confirm("The Development Environment to overwrite is installed. Uninstall it?",
abort=True)
platform.uninstall_dev_env(dev_env_original)

tool_image_list = get_tool_image_list(platform.tool_images)
new_dev_env_descriptor = get_dev_env_descriptor_from_user(dev_env_name, tool_image_list)

if dev_env_original is not None:
overwrite_existing_dev_env(dev_env_original, new_dev_env_descriptor)
dev_env_original.tools = new_dev_env_descriptor["tools"]
else:
create_new_dev_env(platform, new_dev_env_descriptor)

def execute(platform: Platform, dev_env_name: str) -> None:
""" Create a new Development Environment.
Args:
platform -- the platform
dev_env_name -- the name of the Development Environment
Exceptions:
Abort -- if the name of the Development Environment contains whitespace characters
"""
create_dev_env(platform, dev_env_name)
platform.flush_descriptors()
stdout.print(f"The [green]{dev_env_name}[/] Development Environment has been created!")
Expand Down
58 changes: 21 additions & 37 deletions tests/cli/test_create_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from typer.testing import CliRunner
from unittest.mock import patch, MagicMock, call

from dem.core.tool_images import ToolImages

## Global test variables

# In order to test stdout and stderr separately, the stderr can't be mixed into the stdout.
Expand Down Expand Up @@ -40,22 +38,6 @@ def test_get_tool_image_list():
]
assert actual_tool_images == expected_tool_iamges

def test_overwrite_existing_dev_env():
# Test setup
mock_original_dev_env = MagicMock()
mock_original_dev_env.tools = MagicMock()

mock_tools = MagicMock()
mock_new_dev_env_descriptor = {
"tools": mock_tools
}

# Run unit under test
create_cmd.overwrite_existing_dev_env(mock_original_dev_env, mock_new_dev_env_descriptor)

# Check expectations
assert mock_original_dev_env.tools == mock_tools

@patch("dem.cli.command.create_cmd.DevEnv")
def test_create_new_dev_env(mock_DevEnvLocal):
# Test setup
Expand Down Expand Up @@ -102,17 +84,16 @@ def test_create_dev_env_new(mock_get_tool_image_list, mock_get_dev_env_descripto
mock_create_new_dev_env.assert_called_once_with(mock_dev_env_local_setup, mock_dev_env_descriptor)
mock_new_dev_env.check_image_availability.return_value = mock_dev_env_local_setup.tool_images

@patch("dem.cli.command.create_cmd.overwrite_existing_dev_env")
@patch("dem.cli.command.create_cmd.get_dev_env_descriptor_from_user")
@patch("dem.cli.command.create_cmd.get_tool_image_list")
@patch("dem.cli.command.create_cmd.typer.confirm")
def test_create_dev_env_overwrite(mock_confirm, mock_get_tool_image_list,
mock_get_dev_env_descriptor_from_user,
mock_overwrite_existing_dev_env):
mock_get_dev_env_descriptor_from_user) -> None:
# Test setup
mock_dev_env_local_setup = MagicMock()
mock_platform = MagicMock()
mock_dev_env_original = MagicMock()
mock_dev_env_local_setup.get_dev_env_by_name.return_value = mock_dev_env_original
mock_dev_env_original.is_installed = True
mock_platform.get_dev_env_by_name.return_value = mock_dev_env_original
mock_tools = MagicMock()
mock_dev_env_descriptor = {
"tools": mock_tools
Expand All @@ -126,20 +107,24 @@ def test_create_dev_env_overwrite(mock_confirm, mock_get_tool_image_list,
expected_dev_env_name = "test_dev_env"

# Run unit under test
create_cmd.create_dev_env(mock_dev_env_local_setup, expected_dev_env_name)
create_cmd.create_dev_env(mock_platform, expected_dev_env_name)

# Check expectations
mock_dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(expected_dev_env_name)
mock_confirm.assert_called_once_with("The input name is already used by a Development Environment. Overwrite it?",
abort=True)
mock_get_tool_image_list(mock_dev_env_local_setup.tool_images)
assert mock_dev_env_original.tools is mock_tools

mock_platform.get_dev_env_by_name.assert_called_once_with(expected_dev_env_name)
mock_confirm.assert_has_calls([
call("The input name is already used by a Development Environment. Overwrite it?", abort=True),
call("The Development Environment to overwrite is installed. Uninstall it?", abort=True)
])
mock_platform.uninstall_dev_env.assert_called_once_with(mock_dev_env_original)
mock_get_tool_image_list(mock_platform.tool_images)
mock_get_dev_env_descriptor_from_user.assert_called_once_with(expected_dev_env_name,
mock_tool_images)
mock_overwrite_existing_dev_env.assert_called_once_with(mock_dev_env_original, mock_dev_env_descriptor)

@patch("dem.cli.command.create_cmd.get_dev_env_descriptor_from_user")
@patch("dem.cli.command.create_cmd.typer.confirm")
def test_execute_abort(mock_confirm, mock_get_dev_env_descriptor_from_user):
def test_create_dev_env_abort(mock_confirm, mock_get_dev_env_descriptor_from_user):
# Test setup
mock_dev_env_local_setup = MagicMock()
mock_dev_env_original = MagicMock()
Expand All @@ -152,11 +137,11 @@ def test_execute_abort(mock_confirm, mock_get_dev_env_descriptor_from_user):
with pytest.raises(Exception):
create_cmd.create_dev_env(mock_dev_env_local_setup, expected_dev_env_name)

# Check expectations
mock_dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(expected_dev_env_name)
mock_confirm.assert_called_once_with("The input name is already used by a Development Environment. Overwrite it?",
abort=True)
mock_get_dev_env_descriptor_from_user.assert_not_called()
# Check expectations
mock_dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(expected_dev_env_name)
mock_confirm.assert_called_once_with("The input name is already used by a Development Environment. Overwrite it?",
abort=True)
mock_get_dev_env_descriptor_from_user.assert_not_called()

@patch("dem.cli.command.create_cmd.stdout.print")
@patch("dem.cli.command.create_cmd.create_dev_env")
Expand All @@ -181,9 +166,8 @@ def test_execute(mock_create_dev_env, mock_stdout_print):
call("Run [italic]dem install[/] to install it.")
])

@patch("dem.cli.command.create_cmd.stderr.print")
@patch("dem.cli.command.create_cmd.create_dev_env")
def test_execute_failure(mock_create_dev_env, mock_stderr_print):
def test_execute_failure(mock_create_dev_env):
# Test setup
mock_platform = MagicMock()
main.platform = mock_platform
Expand Down

0 comments on commit 2ad39c9

Please sign in to comment.