Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated clone cmd to cp #137

Merged
merged 8 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions dem/cli/command/clone_cmd.py → dem/cli/command/cp_cmd.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
"""clone CLI command implementation."""
"""cp CLI command implementation."""
# dem/cli/command/rename_cmd.py

import copy
from dem.core.dev_env import DevEnv
from dem.core.platform import DevEnvLocalSetup
from dem.cli.console import stderr

def get_dev_env_to_clone(platform: DevEnvLocalSetup, dev_env_name: str) -> (DevEnv | None):
dev_env_to_clone = platform.get_dev_env_by_name(dev_env_name)
def get_dev_env_to_cp(platform: DevEnvLocalSetup, dev_env_name: str) -> (DevEnv | None):
dev_env_to_cp = platform.get_dev_env_by_name(dev_env_name)

if dev_env_to_clone is None:
if dev_env_to_cp is None:
stderr.print("[red]Error: The input Development Environment does not exist.[/]")
return None
else:
return dev_env_to_clone
return dev_env_to_cp

def check_new_dev_env_name_taken(dev_env_local_setup: DevEnvLocalSetup, new_dev_env_name: str) -> bool:
if dev_env_local_setup.get_dev_env_by_name(new_dev_env_name):
Expand All @@ -22,17 +22,17 @@ def check_new_dev_env_name_taken(dev_env_local_setup: DevEnvLocalSetup, new_dev_
else:
return False

def clone_given_dev_env(platform: DevEnvLocalSetup, dev_env_to_clone: DevEnv,
def cp_given_dev_env(platform: DevEnvLocalSetup, dev_env_to_cp: DevEnv,
new_dev_env_name: str):
new_dev_env = copy.deepcopy(dev_env_to_clone)
new_dev_env = copy.deepcopy(dev_env_to_cp)
new_dev_env.name = new_dev_env_name
platform.local_dev_envs.append(new_dev_env)
platform.flush_to_file()

def execute(dev_env_to_clone_name: str, new_dev_env_name: str) -> None:
def execute(dev_env_to_cp_name: str, new_dev_env_name: str) -> None:
platform = DevEnvLocalSetup()
dev_env_to_clone = get_dev_env_to_clone(platform, dev_env_to_clone_name)
dev_env_to_cp = get_dev_env_to_cp(platform, dev_env_to_cp_name)

if (dev_env_to_clone is not None and
if (dev_env_to_cp is not None and
check_new_dev_env_name_taken(platform,new_dev_env_name) is False):
clone_given_dev_env(platform, dev_env_to_clone, new_dev_env_name)
cp_given_dev_env(platform, dev_env_to_cp, new_dev_env_name)
10 changes: 5 additions & 5 deletions dem/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import typer, importlib.metadata
from typing import Optional
from dem import __command__, __app_name__
from dem.cli.command import info_cmd, list_cmd, pull_cmd, create_cmd, modify_cmd, delete_cmd, \
rename_cmd, clone_cmd, run_cmd, export_cmd, load_cmd, add_reg_cmd, \
from dem.cli.command import cp_cmd, info_cmd, list_cmd, pull_cmd, create_cmd, modify_cmd, delete_cmd, \
rename_cmd, run_cmd, export_cmd, load_cmd, add_reg_cmd, \
list_reg_cmd, del_reg_cmd, add_cat_cmd, list_cat_cmd, del_cat_cmd, \
add_host_cmd
from dem.cli.console import stdout, stderr
Expand Down Expand Up @@ -50,12 +50,12 @@ def pull(dev_env_name: str = typer.Argument(...,
pull_cmd.execute(dev_env_name)

@typer_cli.command()
def clone(dev_env_name: str = typer.Argument(...,help="Name of the Development Environment to clone."),
def cp(dev_env_name: str = typer.Argument(...,help="Name of the Development Environment to cp."),
new_dev_env_name: str = typer.Argument(...,help="Name of the New Development Environment.")) -> None:
"""
Clone existing Development Environment locally.
cp existing Development Environment locally.
"""
clone_cmd.execute(dev_env_name,new_dev_env_name)
cp_cmd.execute(dev_env_name,new_dev_env_name)

@typer_cli.command()
def create(dev_env_name: str = typer.Argument(...,
Expand Down
4 changes: 2 additions & 2 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ Arguments:

---

## **`dem clone DEV_ENV_NAME NEW_DEV_ENV_NAME`**
## **`dem cp DEV_ENV_NAME NEW_DEV_ENV_NAME`**

Create a copy of an existing local Development Environment.

Arguments:

`DEV_ENV_NAME` Name of the Development Environment to clone. [required]
`DEV_ENV_NAME` Name of the Development Environment to copy. [required]

`NEW_DEV_ENV_NAME` Name of the New Development Environment. [required]

Expand Down
72 changes: 36 additions & 36 deletions tests/cli/test_clone_cmd.py → tests/cli/test_cp.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Tests for the clone CLI command."""
# tests/cli/test_clone_cmd.py
"""Tests for the cp CLI command."""
# tests/cli/test_cp_cmd.py

# Unit under test:
import dem.cli.main as main
import dem.cli.command.clone_cmd as clone_cmd
import dem.cli.command.cp_cmd as cp_cmd

# Test framework
from typer.testing import CliRunner
Expand All @@ -15,30 +15,30 @@
runner = CliRunner(mix_stderr=False)

## Test cases
def test_check_dev_env_to_clone_exist():
def test_check_dev_env_to_cp_exist():
# Test setup
test_name = "dev_env_name"
fake_dev_env = MagicMock()
dev_env_local_setup = MagicMock()
dev_env_local_setup.get_dev_env_by_name.return_value = fake_dev_env

# Run unit under test
actual_dev_env = clone_cmd.get_dev_env_to_clone(dev_env_local_setup, test_name)
actual_dev_env = cp_cmd.get_dev_env_to_cp(dev_env_local_setup, test_name)

# Check expectations
assert actual_dev_env is fake_dev_env

dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(test_name)

@patch("dem.cli.command.clone_cmd.stderr.print")
def test_check_dev_env_to_clone_not_exist(mock_stderr_print):
@patch("dem.cli.command.cp_cmd.stderr.print")
def test_check_dev_env_to_cp_not_exist(mock_stderr_print):
# Test setup
fake_name = "fake_env_name"
dev_env_local_setup = MagicMock()
dev_env_local_setup.get_dev_env_by_name.return_value = None

# Run unit under test
actual_dev_env = clone_cmd.get_dev_env_to_clone(dev_env_local_setup, fake_name)
actual_dev_env = cp_cmd.get_dev_env_to_cp(dev_env_local_setup, fake_name)

# Check expectations
assert actual_dev_env is None
Expand All @@ -48,80 +48,80 @@ def test_check_dev_env_to_clone_not_exist(mock_stderr_print):

def test_check_new_dev_env_name_not_taken():
# Test setup
cloned_name = "dev_env_name"
cpd_name = "dev_env_name"
dev_env_local_setup = MagicMock()
dev_env_local_setup.get_dev_env_by_name.return_value = None

# Run unit under test
actual_dev_env = clone_cmd.check_new_dev_env_name_taken(dev_env_local_setup, cloned_name)
actual_dev_env = cp_cmd.check_new_dev_env_name_taken(dev_env_local_setup, cpd_name)

# Check expectations
assert actual_dev_env is False

dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(cloned_name)
dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(cpd_name)

@patch("dem.cli.command.clone_cmd.stderr.print")
@patch("dem.cli.command.cp_cmd.stderr.print")
def test_check_new_dev_env_name_taken(mock_stderr_print):
# Test setup
cloned_name = "dev_env_name"
cpd_name = "dev_env_name"
dev_env_local_setup = MagicMock()
fake_dev_env_to_clone = MagicMock()
dev_env_local_setup.get_dev_env_by_name.return_value = fake_dev_env_to_clone
fake_dev_env_to_cp = MagicMock()
dev_env_local_setup.get_dev_env_by_name.return_value = fake_dev_env_to_cp

# Run unit under test
actual_dev_env = clone_cmd.check_new_dev_env_name_taken(dev_env_local_setup, cloned_name)
actual_dev_env = cp_cmd.check_new_dev_env_name_taken(dev_env_local_setup, cpd_name)

# Check expectations
assert actual_dev_env is True

dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(cloned_name)
mock_stderr_print.assert_called_once_with("[red]Error: Development environment already exists with the " + cloned_name + " name.[/]")
dev_env_local_setup.get_dev_env_by_name.assert_called_once_with(cpd_name)
mock_stderr_print.assert_called_once_with("[red]Error: Development environment already exists with the " + cpd_name + " name.[/]")

def test_clone_given_dev_env():
def test_cp_given_dev_env():
# Test setup
mock_platform = MagicMock()
fake_dev_env_to_clone = MagicMock()
fake_dev_env_to_cp = MagicMock()

test_new_name = "test_cloned"
test_new_name = "test_cpd"

mock_platform.local_dev_envs = []

# Run unit under test
clone_cmd.clone_given_dev_env(mock_platform, fake_dev_env_to_clone, test_new_name)
cp_cmd.cp_given_dev_env(mock_platform, fake_dev_env_to_cp, test_new_name)

# Check expectations
assert mock_platform.local_dev_envs[0].name is test_new_name

mock_platform.flush_to_file.assert_called_once()

@patch("dem.cli.command.clone_cmd.DevEnvLocalSetup")
@patch("dem.cli.command.clone_cmd.get_dev_env_to_clone")
@patch("dem.cli.command.clone_cmd.check_new_dev_env_name_taken")
@patch("dem.cli.command.clone_cmd.clone_given_dev_env")
def test_clone(mock_clone_given_dev_env, mock_check_new_dev_env_name_taken,
mock_get_dev_env_to_clone, mock_DevEnvLocalSetup):
@patch("dem.cli.command.cp_cmd.DevEnvLocalSetup")
@patch("dem.cli.command.cp_cmd.get_dev_env_to_cp")
@patch("dem.cli.command.cp_cmd.check_new_dev_env_name_taken")
@patch("dem.cli.command.cp_cmd.cp_given_dev_env")
def test_cp(mock_cp_given_dev_env, mock_check_new_dev_env_name_taken,
mock_get_dev_env_to_cp, mock_DevEnvLocalSetup):
# Test setup
fake_local_platform = MagicMock()
mock_DevEnvLocalSetup.return_value = fake_local_platform
fake_dev_env_to_clone = MagicMock()
mock_get_dev_env_to_clone.return_value = fake_dev_env_to_clone
fake_dev_env_to_cp = MagicMock()
mock_get_dev_env_to_cp.return_value = fake_dev_env_to_cp
mock_check_new_dev_env_name_taken.return_value = False

test_dev_env_to_clone_name = "test_dev_env_to_clone_name"
test_dev_env_to_cp_name = "test_dev_env_to_cp_name"
test_new_dev_env_name = "test_new_dev_env_name"

# Run unit under test
runner_result = runner.invoke(main.typer_cli,
["clone", test_dev_env_to_clone_name, test_new_dev_env_name],
["cp", test_dev_env_to_cp_name, test_new_dev_env_name],
color=True)

# Check expectations
assert runner_result.exit_code == 0

mock_DevEnvLocalSetup.assert_called_once()
mock_get_dev_env_to_clone.assert_called_once_with(fake_local_platform,
test_dev_env_to_clone_name)
mock_get_dev_env_to_cp.assert_called_once_with(fake_local_platform,
test_dev_env_to_cp_name)
mock_check_new_dev_env_name_taken.assert_called_once_with(fake_local_platform,
test_new_dev_env_name)
mock_clone_given_dev_env.assert_called_once_with(fake_local_platform,
fake_dev_env_to_clone, test_new_dev_env_name)
mock_cp_given_dev_env.assert_called_once_with(fake_local_platform,
fake_dev_env_to_cp, test_new_dev_env_name)
Loading