diff --git a/dem/cli/command/info_cmd.py b/dem/cli/command/info_cmd.py index dad5cb9..94f2d16 100644 --- a/dem/cli/command/info_cmd.py +++ b/dem/cli/command/info_cmd.py @@ -15,29 +15,68 @@ ToolImage.LOCAL_AND_REGISTRY: "Local and Registry", } -def print_local_dev_env_info(platform: Platform, dev_env: DevEnv) -> None: - """ Print information about the given local Development Environment. +def print_status(platform: Platform, dev_env: DevEnv) -> None: + """ Print the status of the Development Environment. Args: - dev_env -- the Development Environment to print information about + platform -- the platform + dev_env -- the Development Environment to print the status of """ - tool_info_table = Table() - tool_info_table.add_column("Image") - tool_info_table.add_column("Availability") - - for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name): - tool_info_table.add_row(tool_image.name, - image_status_messages[tool_image.availability]) if dev_env.is_installed: status = "[green]Installed[/]" if dev_env.name == platform.default_dev_env_name: status = status.replace("[/]", " | Default[/]") else: status = "Not installed" - stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n") + stdout.print(f"Status: {status}\n") + +def print_tools_info_table(dev_env: DevEnv, is_local: bool, platform: Platform = None) -> None: + """ Print information about the tools in the Development Environment. + + Args: + dev_env -- the Development Environment to print information about + is_local -- flag to indicate if the Development Environment is local + platform -- the platform (only needed if is_local is True) + """ + tool_info_table = Table(title="Tools") + tool_info_table.add_column("Image") + tool_info_table.add_column("Availability") + + for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name): + tool_info_table.add_row(tool_image.name, + image_status_messages[tool_image.availability]) + if is_local: + print_status(platform, dev_env) stdout.print(tool_info_table) +def print_tasks_info_table(dev_env: DevEnv) -> None: + """ Print information about the tasks in the Development Environment. + + Args: + dev_env -- the Development Environment to print information about + """ + task_table = Table(title="Tasks") + task_table.add_column("Task") + task_table.add_column("Command") + + for task_name, command in dev_env.tasks.items(): + task_table.add_row(task_name, command) + + stdout.print(task_table) + +def print_local_dev_env_info(platform: Platform, dev_env: DevEnv) -> None: + """ Print information about the given local Development Environment. + + Args: + platform -- the platform + dev_env -- the Development Environment to print information about + """ + stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n") + print_tools_info_table(dev_env, True, platform) + if dev_env.tasks: + print_tasks_info_table(dev_env) + if dev_env.is_installed and dev_env.get_tool_image_status() == DevEnv.Status.REINSTALL_NEEDED: stderr.print("\n[red]Error: Incomplete local install! The Dev Env must be reinstalled![/]") @@ -71,16 +110,11 @@ def print_cat_dev_env_info(dev_env: DevEnv, cat_name: str) -> None: dev_env -- the Development Environment to print information about cat_name -- the name of the catalog the Development Environment belongs to """ - tool_info_table = Table() - tool_info_table.add_column("Image") - tool_info_table.add_column("Availability") - - for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name): - tool_info_table.add_row(tool_image.name, - image_status_messages[tool_image.availability]) stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n") stdout.print(f"Catalog: {cat_name}\n") - stdout.print(tool_info_table) + print_tools_info_table(dev_env, False) + if dev_env.tasks: + print_tasks_info_table(dev_env) if dev_env.get_tool_image_status() == DevEnv.Status.UNAVAILABLE_IMAGE: stderr.print("\n[red]Error: Required image could not be found in the registry![/]") diff --git a/tests/cli/test_info_cmd.py b/tests/cli/test_info_cmd.py index 1c52a5a..ec853df 100644 --- a/tests/cli/test_info_cmd.py +++ b/tests/cli/test_info_cmd.py @@ -16,138 +16,182 @@ runner = CliRunner(mix_stderr=False) @patch("dem.cli.command.info_cmd.stdout.print") -@patch("dem.cli.command.info_cmd.Table") -def test_print_local_dev_env_info_installed_default(mock_Table: MagicMock, - mock_stdout_print: MagicMock) -> None: +def test_print_status_not_installed(mock_stdout_print: MagicMock) -> None: # Setup + mock_platform = MagicMock() mock_dev_env = MagicMock() + mock_dev_env.is_installed = False mock_dev_env.name = "test_dev_env" - mock_table = MagicMock() - mock_Table.return_value = mock_table - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.LOCAL_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] + # Run the test + info_cmd.print_status(mock_platform, mock_dev_env) + + # Verify the output + mock_stdout_print.assert_called_once_with("Status: Not installed\n") + +@patch("dem.cli.command.info_cmd.stdout.print") +def test_print_status_installed(mock_stdout_print: MagicMock) -> None: + # Setup + mock_platform = MagicMock() + mock_dev_env = MagicMock() mock_dev_env.is_installed = True + mock_dev_env.name = "test_dev_env" + mock_platform.default_dev_env_name = "another_dev_env" + + # Run the test + info_cmd.print_status(mock_platform, mock_dev_env) + + # Verify the output + mock_stdout_print.assert_called_once_with("Status: [green]Installed[/]\n") +@patch("dem.cli.command.info_cmd.stdout.print") +def test_print_status_installed_default(mock_stdout_print: MagicMock) -> None: + # Setup mock_platform = MagicMock() + mock_dev_env = MagicMock() + mock_dev_env.is_installed = True + mock_dev_env.name = "test_dev_env" mock_platform.default_dev_env_name = "test_dev_env" # Run the test - info_cmd.print_local_dev_env_info(mock_platform, mock_dev_env) + info_cmd.print_status(mock_platform, mock_dev_env) # Verify the output - mock_Table.assert_called_once() - mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Local"), - call("tool_image2", "Local and Registry")]) - mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call("Status: [green]Installed | Default[/]\n"), - call(mock_table)]) + mock_stdout_print.assert_called_once_with("Status: [green]Installed | Default[/]\n") @patch("dem.cli.command.info_cmd.stdout.print") +@patch("dem.cli.command.info_cmd.print_status") @patch("dem.cli.command.info_cmd.Table") -def test_print_local_dev_env_info_not_installed(mock_Table: MagicMock, - mock_stdout_print: MagicMock) -> None: +def test_print_tools_info_table(mock_Table: MagicMock, mock_print_status: MagicMock, + mock_stdout_print: MagicMock) -> None: # Setup mock_dev_env = MagicMock() - mock_dev_env.name = "test_dev_env" + mock_tool_image1 = MagicMock() + mock_tool_image1.name = "tool1" + mock_tool_image1.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY + mock_tool_image2 = MagicMock() + mock_tool_image2.name = "tool2" + mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_ONLY + mock_tool_image3 = MagicMock() + mock_tool_image3.name = "tool3" + mock_tool_image3.availability = info_cmd.ToolImage.REGISTRY_ONLY + mock_tool_image4 = MagicMock() + mock_tool_image4.name = "tool4" + mock_tool_image4.availability = info_cmd.ToolImage.NOT_AVAILABLE + mock_dev_env.tool_images = [mock_tool_image2, mock_tool_image4, mock_tool_image3, mock_tool_image1] + mock_table = MagicMock() mock_Table.return_value = mock_table - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.LOCAL_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] - mock_dev_env.is_installed = False + mock_platform = MagicMock() # Run the test - info_cmd.print_local_dev_env_info(MagicMock(), mock_dev_env) + info_cmd.print_tools_info_table(mock_dev_env, True, mock_platform) # Verify the output - mock_Table.assert_called_once() + mock_Table.assert_called_once_with(title="Tools") mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Local"), - call("tool_image2", "Local and Registry")]) - mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call("Status: Not installed\n"), - call(mock_table)]) + mock_table.add_row.assert_has_calls([call("tool1", "Local and Registry"), + call("tool2", "Local"), + call("tool3", "Registry"), + call("tool4", "[red]Error: not available![/]")]) + mock_print_status.assert_called_once_with(mock_platform, mock_dev_env) + mock_stdout_print.assert_called_once_with(mock_table) -@patch("dem.cli.command.info_cmd.stdout.print") -@patch("dem.cli.command.info_cmd.stderr.print") @patch("dem.cli.command.info_cmd.Table") -def test_print_local_dev_env_info_reinstall_needed(mock_Table: MagicMock, - mock_stderr_print: MagicMock, - mock_stdout_print: MagicMock) -> None: +@patch("dem.cli.command.info_cmd.stdout.print") +def test_print_tasks_info_table(mock_stdout_print: MagicMock, mock_Table: MagicMock) -> None: # Setup mock_dev_env = MagicMock() - mock_dev_env.name = "test_dev_env" + mock_dev_env.tasks = { + "task1": "command1", + "task2": "command2" + } mock_table = MagicMock() mock_Table.return_value = mock_table - - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.LOCAL_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] + + # Run the test + info_cmd.print_tasks_info_table(mock_dev_env) + + # Verify the output + mock_Table.assert_called_once_with(title="Tasks") + mock_table.add_column.assert_has_calls([call("Task"), call("Command")]) + mock_table.add_row.assert_has_calls([call("task1", "command1"), call("task2", "command2")]) + mock_stdout_print.assert_called_once_with(mock_table) + +@patch("dem.cli.command.info_cmd.print_tasks_info_table") +@patch("dem.cli.command.info_cmd.print_tools_info_table") +@patch("dem.cli.command.info_cmd.stdout.print") +def test_print_local_dev_env_info(mock_stdout_print: MagicMock, + mock_print_tools_info_table: MagicMock, + mock_print_tasks_info_table: MagicMock) -> None: + # Setup + mock_dev_env = MagicMock() + mock_dev_env.name = "test_dev_env" + mock_dev_env.tasks = MagicMock() + mock_dev_env.is_installed = True + mock_dev_env.get_tool_image_status.return_value = info_cmd.DevEnv.Status.OK + + mock_platform = MagicMock() + + # Run the test + info_cmd.print_local_dev_env_info(mock_platform, mock_dev_env) + + # Verify the output + mock_stdout_print.assert_called_once_with("\n[bold]Development Environment: test_dev_env[/]\n") + mock_print_tools_info_table.assert_called_once_with(mock_dev_env, True, mock_platform) + mock_print_tasks_info_table.assert_called_once_with(mock_dev_env) + +@patch("dem.cli.command.info_cmd.print_tasks_info_table") +@patch("dem.cli.command.info_cmd.print_tools_info_table") +@patch("dem.cli.command.info_cmd.stdout.print") +@patch("dem.cli.command.info_cmd.stderr.print") +def test_print_local_dev_env_info_reinstall_needed(mock_stderr_print: MagicMock, + mock_stdout_print: MagicMock, + mock_print_tools_info_table: MagicMock, + mock_print_tasks_info_table: MagicMock) -> None: + # Setup + mock_dev_env = MagicMock() + mock_dev_env.name = "test_dev_env" + mock_dev_env.tasks = MagicMock() mock_dev_env.is_installed = True mock_dev_env.get_tool_image_status.return_value = info_cmd.DevEnv.Status.REINSTALL_NEEDED + mock_platform = MagicMock() + # Run the test - info_cmd.print_local_dev_env_info(MagicMock(), mock_dev_env) + info_cmd.print_local_dev_env_info(mock_platform, mock_dev_env) # Verify the output - mock_Table.assert_called_once() - mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Local"), - call("tool_image2", "Local and Registry")]) - mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call("Status: [green]Installed[/]\n"), - call(mock_table)]) + mock_stdout_print.assert_called_once_with("\n[bold]Development Environment: test_dev_env[/]\n") + mock_print_tools_info_table.assert_called_once_with(mock_dev_env, True, mock_platform) + mock_print_tasks_info_table.assert_called_once_with(mock_dev_env) mock_stderr_print.assert_called_once_with("\n[red]Error: Incomplete local install! The Dev Env must be reinstalled![/]") +@patch("dem.cli.command.info_cmd.print_tasks_info_table") +@patch("dem.cli.command.info_cmd.print_tools_info_table") @patch("dem.cli.command.info_cmd.stdout.print") @patch("dem.cli.command.info_cmd.stderr.print") -@patch("dem.cli.command.info_cmd.Table") -def test_print_local_dev_env_info_unavailable_image(mock_Table: MagicMock, - mock_stderr_print: MagicMock, - mock_stdout_print: MagicMock) -> None: +def test_print_local_dev_env_info_unavailable_image(mock_stderr_print: MagicMock, + mock_stdout_print: MagicMock, + mock_print_tools_info_table: MagicMock, + mock_print_tasks_info_table: MagicMock) -> None: # Setup mock_dev_env = MagicMock() mock_dev_env.name = "test_dev_env" - mock_table = MagicMock() - mock_Table.return_value = mock_table - - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.LOCAL_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] - mock_dev_env.is_installed = False + mock_dev_env.tasks = MagicMock() + mock_dev_env.is_installed = True mock_dev_env.get_tool_image_status.return_value = info_cmd.DevEnv.Status.UNAVAILABLE_IMAGE + mock_platform = MagicMock() + # Run the test - info_cmd.print_local_dev_env_info(MagicMock(), mock_dev_env) + info_cmd.print_local_dev_env_info(mock_platform, mock_dev_env) # Verify the output - mock_Table.assert_called_once() - mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Local"), - call("tool_image2", "Local and Registry")]) - mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call("Status: Not installed\n"), - call(mock_table)]) + mock_stdout_print.assert_called_once_with("\n[bold]Development Environment: test_dev_env[/]\n") + mock_print_tools_info_table.assert_called_once_with(mock_dev_env, True, mock_platform) + mock_print_tasks_info_table.assert_called_once_with(mock_dev_env) mock_stderr_print.assert_called_once_with("\n[red]Error: Required image could not be found either locally or in the registry![/]") @patch("dem.cli.command.info_cmd.print_local_dev_env_info") @@ -180,69 +224,46 @@ def test_local_info_unknown_dev_env(mock_stderr_print: MagicMock) -> None: mock_platform.get_dev_env_by_name.assert_called_once_with("unknown_dev_env") mock_stderr_print.assert_called_once_with("[red]Error: Unknown Development Environment: unknown_dev_env[/]\n") +@patch("dem.cli.command.info_cmd.print_tasks_info_table") +@patch("dem.cli.command.info_cmd.print_tools_info_table") @patch("dem.cli.command.info_cmd.stdout.print") -@patch("dem.cli.command.info_cmd.Table") -def test_print_cat_dev_env_info(mock_Table: MagicMock, - mock_stdout_print: MagicMock) -> None: +def test_print_cat_dev_env_info(mock_stdout_print: MagicMock, + mock_print_tools_info_table: MagicMock, + mock_print_tasks_info_table: MagicMock) -> None: # Setup mock_dev_env = MagicMock() mock_dev_env.name = "test_dev_env" - mock_table = MagicMock() - mock_Table.return_value = mock_table - - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.REGISTRY_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] + mock_dev_env.tasks = MagicMock() mock_dev_env.get_tool_image_status.return_value = info_cmd.DevEnv.Status.OK # Run the test info_cmd.print_cat_dev_env_info(mock_dev_env, "test_cat") # Verify the output - mock_Table.assert_called_once() - mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Registry"), - call("tool_image2", "Local and Registry")]) mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call(f"Catalog: test_cat\n"), - call(mock_table)]) + call(f"Catalog: test_cat\n")]) + mock_print_tools_info_table.assert_called_once_with(mock_dev_env, False) + mock_print_tasks_info_table.assert_called_once_with(mock_dev_env) +@patch("dem.cli.command.info_cmd.print_tools_info_table") @patch("dem.cli.command.info_cmd.stderr.print") @patch("dem.cli.command.info_cmd.stdout.print") -@patch("dem.cli.command.info_cmd.Table") -def test_print_cat_dev_env_info_unavailable_image(mock_Table: MagicMock, - mock_stdout_print: MagicMock, - mock_stderr_print: MagicMock) -> None: +def test_print_cat_dev_env_info_unavailable_image(mock_stdout_print: MagicMock, + mock_stderr_print: MagicMock, + mock_print_tools_info_table: MagicMock) -> None: # Setup mock_dev_env = MagicMock() mock_dev_env.name = "test_dev_env" - mock_table = MagicMock() - mock_Table.return_value = mock_table - - mock_tool_image1 = MagicMock() - mock_tool_image1.name = "tool_image1" - mock_tool_image1.availability = info_cmd.ToolImage.REGISTRY_ONLY - mock_tool_image2 = MagicMock() - mock_tool_image2.name = "tool_image2" - mock_tool_image2.availability = info_cmd.ToolImage.LOCAL_AND_REGISTRY - mock_dev_env.tool_images = [mock_tool_image1, mock_tool_image2] + mock_dev_env.tasks = {} mock_dev_env.get_tool_image_status.return_value = info_cmd.DevEnv.Status.UNAVAILABLE_IMAGE # Run the test info_cmd.print_cat_dev_env_info(mock_dev_env, "test_cat") # Verify the output - mock_Table.assert_called_once() - mock_table.add_column.assert_has_calls([call("Image"), call("Availability")]) - mock_table.add_row.assert_has_calls([call("tool_image1", "Registry"), - call("tool_image2", "Local and Registry")]) mock_stdout_print.assert_has_calls([call("\n[bold]Development Environment: test_dev_env[/]\n"), - call(f"Catalog: test_cat\n"), - call(mock_table)]) + call(f"Catalog: test_cat\n")]) + mock_print_tools_info_table.assert_called_once_with(mock_dev_env, False) mock_stderr_print.assert_called_once_with("\n[red]Error: Required image could not be found in the registry![/]") @patch("dem.cli.command.info_cmd.print_cat_dev_env_info")