Skip to content

Commit

Permalink
The load command renamed to import. Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmurai committed Apr 16, 2024
1 parent 0a04b36 commit 8ae8c9d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 110 deletions.
53 changes: 53 additions & 0 deletions dem/cli/command/import_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""import CLI command implementation."""
# dem/cli/command/import_cmd.py

from dem.core.dev_env import DevEnv
from dem.core.platform import Platform
from dem.cli.console import stderr
import json, os
import typer

def import_dev_env_from_json(platform: Platform,path_to_dev_env: str) -> None:
""" Import a Development Environment from a JSON file.
Args:
platform (Platform): The platform instance.
path_to_dev_env (str): The path to the JSON file.
Raises:
typer.Abort: If the Development Environment already exists, or the JSON format is
invalid.
"""
raw_file = open(path_to_dev_env, "r")

try:
dev_env_descriptor_to_import = json.load(raw_file)

if platform.get_dev_env_by_name(dev_env_descriptor_to_import["name"]) is not None:
stderr.print("[red]Error: The Development Environment already exists.[/]")
raise typer.Abort()
else:
new_dev_env: DevEnv = DevEnv(dev_env_descriptor_to_import)
platform.local_dev_envs.append(new_dev_env)
except json.decoder.JSONDecodeError:
stderr.print("[red]Error: invalid json format.[/]")
raise typer.Abort()
else:
raw_file.close()

def execute(platform: Platform, path_to_dev_env: str) -> None:
""" Execute the import command.
Args:
platform (Platform): The platform instance.
path_to_dev_env (str): The path to the JSON file.
Raises:
typer.Abort: If the Development Environment already exists, or the JSON format is
invalid.
"""
if os.path.exists(path_to_dev_env):
import_dev_env_from_json(platform,path_to_dev_env)
platform.flush_descriptors()
else:
stderr.print("[red]Error: The input file does not exist.[/]")
42 changes: 0 additions & 42 deletions dem/cli/command/load_cmd.py

This file was deleted.

10 changes: 5 additions & 5 deletions dem/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing_extensions import Annotated
import os
from dem import __command__, __app_name__
from dem.cli.command import cp_cmd, info_cmd, list_cmd, create_cmd, modify_cmd, delete_cmd, \
rename_cmd, run_cmd, export_cmd, load_cmd, clone_cmd, add_reg_cmd, \
from dem.cli.command import cp_cmd, import_cmd, info_cmd, list_cmd, create_cmd, modify_cmd, delete_cmd, \
rename_cmd, run_cmd, export_cmd, clone_cmd, add_reg_cmd, \
list_reg_cmd, del_reg_cmd, add_cat_cmd, list_cat_cmd, del_cat_cmd, \
add_host_cmd, uninstall_cmd, install_cmd, assign_cmd, init_cmd, \
list_host_cmd, del_host_cmd, list_tools_cmd
Expand Down Expand Up @@ -166,13 +166,13 @@ def export(dev_env_name: Annotated[str, typer.Argument(help="Name of the Develop
else:
raise InternalError("Error: The platform hasn't been initialized properly!")

@typer_cli.command()
def load(path_to_dev_env: Annotated[str, typer.Argument(help="Path to the Dev Env to import.")]) -> None:
@typer_cli.command("import")
def import_(path_to_dev_env: Annotated[str, typer.Argument(help="Path to the Dev Env to import.")]) -> None:
"""
Import the Development Environment.
"""
if platform:
load_cmd.execute(platform, path_to_dev_env)
import_cmd.execute(platform, path_to_dev_env)
else:
raise InternalError("Error: The platform hasn't been initialized properly!")

Expand Down
38 changes: 19 additions & 19 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ directory will be used.

---

## **`dem import PATH_TO_DEV_ENV`**

Imports a Development Environment descriptor.

:information_source: After the import, the Development Environment can be installed with the `install`
command.

!!! Note

The file to import only contains the Development Environment descriptor. To install the Dev Env
the DEM needs access to all the registries where the required images are stored.

Arguments:

`PATH_TO_DEV_ENV` Path of the JSON file to import. Can be an absolute path or a relative path to the
current directory.

---

## **`dem info DEV_ENV_NAME [OPTIONS] [*CATALOG_NAMES]`**

Get information about the specified Development Environment available locally or in the catalogs.
Expand Down Expand Up @@ -219,25 +238,6 @@ Examples:

---

## **`dem load PATH_TO_DEV_ENV`**

Imports a Development Environment.

!!! Note

The file to import only contains the Development Environment descriptor. For a successful import
the DEM needs access to all the registries where the required images are stored.

:information_source: After the import, the Development Environment can be installed with the `install` command.

Arguments:

`PATH_TO_DEV_ENV` Path of the JSON file to import. Can be an absolute path or a relative path to the
current directory.

---


## **`dem modify DEV_ENV_NAME`**

Modify a Development Environment descriptor available from the local descriptor storage (catalog).
Expand Down
86 changes: 43 additions & 43 deletions tests/cli/test_load_cmd.py → tests/cli/test_import_cmd.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Tests for the load CLI command."""
# tests/cli/test_load_cmd.py
"""Tests for the import CLI command."""
# tests/cli/test_import_cmd.py

# Unit under test:
from mock import patch
import dem.cli.main as main
import dem.cli.command.load_cmd as load_cmd
import dem.cli.command.import_cmd as import_cmd

# Test framework
import io
from typer.testing import CliRunner
from unittest.mock import patch, MagicMock
from pytest import raises

from rich.console import Console
import json
Expand All @@ -19,16 +20,16 @@
# the stdout.
runner = CliRunner(mix_stderr=False)

@patch("dem.cli.command.load_cmd.json.load")
@patch("dem.cli.command.load_cmd.open")
def test_load_dev_env_to_dev_env_json_already_exist(mock_open, mock_json):
@patch("dem.cli.command.import_cmd.stderr.print")
@patch("dem.cli.command.import_cmd.json.load")
@patch("dem.cli.command.import_cmd.open")
def test_import_dev_env_from_json_already_exist(mock_open: MagicMock, mock_json: MagicMock,
mock_stderr_print: MagicMock):
# Test setup
mock_platform = MagicMock()
fake_opened_file = MagicMock()
mock_open.return_value = fake_opened_file

mock_json = MagicMock()

path="/home/cica.json"

test_name = "test_name"
Expand All @@ -38,14 +39,21 @@ def test_load_dev_env_to_dev_env_json_already_exist(mock_open, mock_json):
mock_json.return_value = test_dev_env

mock_platform.get_dev_env_by_name.return_value = MagicMock()
retval_dev_env_cant_load = load_cmd.load_dev_env_to_dev_env_json(mock_platform,path)

assert retval_dev_env_cant_load is False

@patch("dem.cli.command.load_cmd.DevEnv")
@patch("dem.cli.command.load_cmd.json.load")
@patch("dem.cli.command.load_cmd.open")
def test_load_dev_env_to_dev_env_json(mock_open, mock_json, mock_DevEnvLocal: MagicMock):
# Run unit under test
with raises(import_cmd.typer.Abort):
import_cmd.import_dev_env_from_json(mock_platform,path)

# Check expectations
mock_open.assert_called_once_with(path, "r")
mock_json.assert_called_once_with(fake_opened_file)
mock_platform.get_dev_env_by_name.assert_called()
mock_stderr_print.assert_called_once_with("[red]Error: The Development Environment already exists.[/]")

@patch("dem.cli.command.import_cmd.DevEnv")
@patch("dem.cli.command.import_cmd.json.load")
@patch("dem.cli.command.import_cmd.open")
def test_import_dev_env_from_json(mock_open, mock_json, mock_DevEnvLocal: MagicMock):
# Test setup
mock_platform = MagicMock()
fake_opened_file = MagicMock()
Expand All @@ -64,17 +72,16 @@ def test_load_dev_env_to_dev_env_json(mock_open, mock_json, mock_DevEnvLocal: Ma
mock_new_dev_env.tools = MagicMock()

# Run unit under test
retval_dev_env_load = load_cmd.load_dev_env_to_dev_env_json(mock_platform,path)
import_cmd.import_dev_env_from_json(mock_platform,path)

# Check expectations
assert retval_dev_env_load is True
mock_open.assert_called_once_with(path, "r")
mock_platform.get_dev_env_by_name.assert_called()
mock_platform.get_dev_env_by_name.assert_called_once_with(test_name)
mock_DevEnvLocal.assert_called_once_with(test_dev_env)
fake_opened_file.close.assert_called()

@patch("dem.cli.command.load_cmd.open",MagicMock())
@patch("dem.cli.command.load_cmd.json.load")
@patch("dem.cli.command.import_cmd.open",MagicMock())
@patch("dem.cli.command.import_cmd.json.load")
def test_json_decode_error(mock_json):
# Test setup
mock_platform = MagicMock()
Expand All @@ -85,21 +92,12 @@ def test_json_decode_error(mock_json):
mock_platform.get_dev_env_by_name.return_value = MagicMock()

# Run unit under test
retval_dev_env_cant_load = load_cmd.load_dev_env_to_dev_env_json(mock_platform,path)

# Check expectations
assert retval_dev_env_cant_load is False

def test_wo_path():
# Run unit under test
runner_result = runner.invoke(main.typer_cli, ["load"])

# Check expectations
assert 2 == runner_result.exit_code
with raises(import_cmd.typer.Abort):
import_cmd.import_dev_env_from_json(mock_platform,path)

def test_with_invalid_file():
# Run unit under test
runner_result = runner.invoke(main.typer_cli, ["load", "asd"])
runner_result = runner.invoke(main.typer_cli, ["import", "asd"])

# Check expectations
assert 0 == runner_result.exit_code
Expand All @@ -108,19 +106,21 @@ def test_with_invalid_file():
console.print("[red]Error: The input file does not exist.[/]")
assert console.file.getvalue() == runner_result.stderr

@patch("dem.cli.command.load_cmd.load_dev_env_to_dev_env_json")
@patch("dem.cli.command.load_cmd.check_is_file_exist")
def test_execution(mock_file_check,mock_load_dev_env):
@patch("dem.cli.command.import_cmd.import_dev_env_from_json")
@patch("dem.cli.command.import_cmd.os.path.exists")
def test_execution(mock_os_path_exists: MagicMock, mock_import_dev_env_from_json: MagicMock) -> None:
# Test setup
mock_platform = MagicMock()
main.platform = mock_platform

mock_file_check.return_value = True
mock_load_dev_env.return_value = True
# Run unit under test
runner_result = runner.invoke(main.typer_cli, ["load", "asd"])
assert 0 == runner_result.exit_code
mock_os_path_exists.return_value = True

def test_check_is_file_exist_param_is_none():
# Run unit under test
actual_file_exist = load_cmd.check_is_file_exist(None)
runner_result = runner.invoke(main.typer_cli, ["import", "asd"])

# Check expectations
assert actual_file_exist is False
assert 0 == runner_result.exit_code

mock_os_path_exists.assert_called_with("asd")
mock_import_dev_env_from_json.assert_called_once_with(mock_platform, "asd")
mock_platform.flush_descriptors.assert_called_once()
2 changes: 1 addition & 1 deletion tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_platform_not_initialized() -> None:
main.cp: [test_dev_env_name, test_dev_env_name],
main.create: [test_dev_env_name],
main.export: [test_dev_env_name],
main.load: [test_path],
main.import_: [test_path],
main.clone: [test_dev_env_name],
main.rename: [test_dev_env_name, test_dev_env_name],
main.modify: [test_dev_env_name],
Expand Down

0 comments on commit 8ae8c9d

Please sign in to comment.