Skip to content

Commit

Permalink
Merge pull request #1855 from jemrobinson/1853-add-dsh-help-test
Browse files Browse the repository at this point in the history
Add test for help function
  • Loading branch information
JimMadge authored May 7, 2024
2 parents 94f0394 + 35637a3 commit 9f8e124
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 59 deletions.
14 changes: 2 additions & 12 deletions data_safe_haven/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
from .admin import admin_command_group
from .config import config_command_group
from .context import context_command_group
from .deploy import deploy_command_group
from .teardown import teardown_command_group
from .cli import application

__all__ = [
"admin_command_group",
"context_command_group",
"config_command_group",
"deploy_command_group",
"teardown_command_group",
]
__all__ = ["application"]
87 changes: 43 additions & 44 deletions data_safe_haven/cli.py → data_safe_haven/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@
import typer

from data_safe_haven import __version__
from data_safe_haven.commands import (
admin_command_group,
config_command_group,
context_command_group,
deploy_command_group,
teardown_command_group,
)
from data_safe_haven.exceptions import DataSafeHavenError
from data_safe_haven.utility import LoggingSingleton

from .admin import admin_command_group
from .config import config_command_group
from .context import context_command_group
from .deploy import deploy_command_group
from .teardown import teardown_command_group

# Create the application
application = typer.Typer(
context_settings={"help_option_names": ["-h", "--help"]},
invoke_without_command=True,
name="dsh",
no_args_is_help=True,
)


# Custom application callback
# This is executed before
@application.callback()
def callback(
output: Annotated[
Optional[pathlib.Path], # noqa: UP007
Expand Down Expand Up @@ -52,45 +62,34 @@ def callback(
raise typer.Exit()


def main() -> None:
"""Command line entrypoint for Data Safe Haven application"""

# Create the application
application = typer.Typer(
context_settings={"help_option_names": ["-h", "--help"]},
invoke_without_command=True,
no_args_is_help=True,
)

# Register arguments to the main executable
application.callback()(callback)
# Register command groups
application.add_typer(
admin_command_group,
name="admin",
help="Perform administrative tasks for a Data Safe Haven deployment.",
)
application.add_typer(
config_command_group,
name="config",
help="Manage Data Safe Haven configuration.",
)
application.add_typer(
context_command_group, name="context", help="Manage Data Safe Haven contexts."
)
application.add_typer(
deploy_command_group,
name="deploy",
help="Deploy a Data Safe Haven component.",
)
application.add_typer(
teardown_command_group,
name="teardown",
help="Tear down a Data Safe Haven component.",
)

# Register command groups
application.add_typer(
admin_command_group,
name="admin",
help="Perform administrative tasks for a Data Safe Haven deployment.",
)
application.add_typer(
config_command_group,
name="config",
help="Manage Data Safe Haven configuration.",
)
application.add_typer(
context_command_group, name="context", help="Manage Data Safe Haven contexts."
)
application.add_typer(
deploy_command_group,
name="deploy",
help="Deploy a Data Safe Haven component.",
)
application.add_typer(
teardown_command_group,
name="teardown",
help="Tear down a Data Safe Haven component.",
)

# Start the application
def main() -> None:
"""Run the application and log any exceptions"""
try:
application()
except DataSafeHavenError as exc:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies = [
]

[project.scripts]
dsh = "data_safe_haven.cli:main"
dsh = "data_safe_haven.commands.cli:main"

[tool.hatch.version]
path = "data_safe_haven/version.py"
Expand Down
27 changes: 27 additions & 0 deletions tests/commands/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from data_safe_haven.commands import application


class TestHelp:
def result_checker(self, result):
assert result.exit_code == 0
assert "Usage: dsh [OPTIONS] COMMAND [ARGS]..." in result.stdout
assert "Arguments to the main executable" in result.stdout
assert "│ --output" in result.stdout
assert "│ --verbosity" in result.stdout
assert "│ --version" in result.stdout
assert "│ --install-completion" in result.stdout
assert "│ --show-completion" in result.stdout
assert "│ --help" in result.stdout
assert "│ admin" in result.stdout
assert "│ config" in result.stdout
assert "│ context" in result.stdout
assert "│ deploy" in result.stdout
assert "│ teardown" in result.stdout

def test_help(self, runner):
result = runner.invoke(application, ["--help"])
self.result_checker(result)

def test_help_short_code(self, runner):
result = runner.invoke(application, ["-h"])
self.result_checker(result)
2 changes: 1 addition & 1 deletion tests/commands/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_safe_haven.commands import config_command_group
from data_safe_haven.commands.config import config_command_group
from data_safe_haven.config import Config
from data_safe_haven.external import AzureApi

Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_safe_haven.commands import context_command_group
from data_safe_haven.commands.context import context_command_group
from data_safe_haven.context_infrastructure import ContextInfrastructure


Expand Down

0 comments on commit 9f8e124

Please sign in to comment.