Skip to content

Commit

Permalink
Update docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
cidrblock committed Dec 5, 2021
1 parent b94136e commit 7eb4ca9
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
strictness=full
docstring_style=sphinx
30 changes: 23 additions & 7 deletions src/ansible_navigator/actions/exec.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
""" :exec """
"""Run the :exec subcommand."""
import os
import shlex

from typing import List
from typing import Optional
from typing import Tuple
from typing import Union

from . import _actions as actions
from ..app import App
from ..configuration_subsystem import ApplicationConfiguration
from ..runner import Command


@actions.register
class Action(App):
""":exec"""
"""Run the :exec subcommand."""

# pylint: disable=too-few-public-methods

KEGEX = r"^e(?:xec)?$"

def __init__(self, args):
def __init__(self, args: ApplicationConfiguration):
"""Initialize the action.
:param args: The current application configuration.
"""
super().__init__(args=args, logger_name=__name__, name="exec")

@staticmethod
def _generate_command(exec_command: str, exec_shell: bool) -> Tuple:
"""Generate the command and args"""
def _generate_command(exec_command: str, exec_shell: bool) -> Tuple[str, Optional[List[str]]]:
"""Generate the command and args.
:param exec_command: The command to run
:param exec_shell: Should the command be wrapped in a shell
:returns: The command and any pass through arguments
"""
pass_through_args = None
if exec_shell and exec_command:
command = "/bin/bash"
Expand All @@ -37,7 +48,10 @@ def _generate_command(exec_command: str, exec_shell: bool) -> Tuple:
return (command, pass_through_args)

def run_stdout(self) -> Union[None, int]:
"""Run in mode stdout"""
"""Run in mode stdout.
:returns: The return code or None
"""
self._logger.debug("exec requested in stdout mode")
response = self._run_runner()
if response:
Expand All @@ -48,8 +62,10 @@ def run_stdout(self) -> Union[None, int]:
def _run_runner(self) -> Optional[Tuple]:
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
"""spin up runner"""
"""Spin up runner.
:return: The stdout, stderr and return code from runner
"""
if isinstance(self._args.set_environment_variable, dict):
set_envvars = {**self._args.set_environment_variable}
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ class Internals(SimpleNamespace):
choices=[True, False],
cli_parameters=CliParameters(short="--exshell"),
settings_file_path_override="exec.shell",
short_description=(
"Specify the exec command should be run in a shell."
),
short_description=("Specify the exec command should be run in a shell."),
subcommands=["exec"],
value=EntryValue(default=True),
),
Expand Down
2 changes: 2 additions & 0 deletions src/ansible_navigator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,13 @@ def debug_log(directory: str, found: bool, description: str):
exit_messages.append(ExitMessage(message=exit_msg))
return messages, exit_messages, None


def divmod_int(numerator: Union[int, float], denominator: Union[int, float]) -> Tuple[int, int]:
"""Return the result of divmod, as a tuple of integers."""
quotient, remainder = divmod(numerator, denominator)
return int(quotient), int(remainder)


def human_time(seconds: int) -> str:
"""Convert seconds into human readable 00d00h00m00s format."""
sign_string = "-" if seconds < 0 else ""
Expand Down
1 change: 1 addition & 0 deletions tests/integration/actions/exec/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Integration tests for the exec subcommand."""
27 changes: 20 additions & 7 deletions tests/integration/actions/exec/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
""" base class for exec interactive and stdout tests
"""
"""The base class for exec interactive and stdout tests."""

import difflib
import json
import os

from typing import Generator

import pytest

from ..._common import fixture_path_from_request
Expand All @@ -18,16 +20,22 @@


class BaseClass:
"""base class for interactive/stdout exec tests"""
"""The base class for interactive/stdout exec tests."""

UPDATE_FIXTURES = False
PANE_HEIGHT = 25
PANE_WIDTH = 300
CONFIG_FILE = None

@pytest.fixture(scope="module", name="tmux_session")
def fixture_tmux_session(self, request):
"""tmux fixture for this module"""
def fixture_tmux_session(
self, request: pytest.FixtureRequest
) -> Generator[TmuxSession, None, None]:
"""Tmux fixture for this module.
:param request: The request for this fixture
:yields: A tmux session
"""
params = {
"unique_test_id": request.node.nodeid,
"pane_height": self.PANE_HEIGHT,
Expand All @@ -40,12 +48,17 @@ def fixture_tmux_session(self, request):
with TmuxSession(**params) as tmux_session:
yield tmux_session

def test(self, request, tmux_session, step):
def test(self, request: pytest.FixtureRequest, tmux_session: TmuxSession, step: Step):
# pylint:disable=unused-argument
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-arguments
"""test interactive/stdout exec"""
"""Test interactive/stdout exec.
:param request: The test request
:param tmux_session: The tmux session
:param step: A step within a series of tests
:raises ValueError: If test mode isn't set
"""
if step.search_within_response is SearchFor.PROMPT:
search_within_response = tmux_session.cli_prompt
else:
Expand Down
18 changes: 11 additions & 7 deletions tests/integration/actions/exec/test_stdout_config_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" exec stdout tests using tmux """
import pytest
"""Tests for exec, mode stdout, parameters set using cli."""

import pytest

from .base import BaseClass

Expand All @@ -11,15 +11,15 @@


class StdoutCommand(Command):
"""stdout command"""
"""A stdout command."""

mode = "stdout"
subcommand = "exec"
preclear = True


class ShellCommand(Step):
"""a shell command"""
"""A shell command."""

search_within_response = SearchFor.PROMPT

Expand Down Expand Up @@ -63,13 +63,17 @@ class ShellCommand(Step):
steps = add_indicies(stdout_tests)


def step_id(value):
"""return the test id from the test step object"""
def step_id(value: ShellCommand) -> str:
"""Return the test id from the test step object.
:param value: The data fro the test iteration
:returns: An id for the test
"""
return f"{value.comment} {value.user_input}"


@pytest.mark.parametrize("step", steps, ids=step_id)
class Test(BaseClass):
"""run the tests"""
"""Run the tests for exec, mode stdout, parameters set using cli."""

UPDATE_FIXTURES = False
17 changes: 11 additions & 6 deletions tests/integration/actions/exec/test_stdout_config_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" exec stdout tests using tmux """
"""Tests for exec, mode stdout, parameters set using config file."""

import pytest


Expand All @@ -12,15 +13,15 @@


class StdoutCommand(Command):
"""stdout command"""
"""A stdout command."""

mode = "stdout"
subcommand = "exec"
preclear = True


class ShellCommand(Step):
"""a shell command"""
"""A shell command."""

search_within_response = SearchFor.PROMPT

Expand All @@ -47,14 +48,18 @@ class ShellCommand(Step):
steps = add_indicies(stdout_tests)


def step_id(value):
"""return the test id from the test step object"""
def step_id(value: ShellCommand) -> str:
"""Return the test id from the test step object.
:param value: The data fro the test iteration
:returns: An id for the test
"""
return f"{value.comment} {value.user_input}"


@pytest.mark.parametrize("step", steps, ids=step_id)
class Test(BaseClass):
"""run the tests"""
"""Run the tests for exec, mode stdout, parameters set using config file."""

CONFIG_FILE = TEST_CONFIG_FILE
UPDATE_FIXTURES = False
25 changes: 14 additions & 11 deletions tests/unit/actions/test_exec.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Some simple tests for exec command and param generation
"""

"""Some simple tests for exec command and param generation."""

from typing import List
from typing import NamedTuple

import pytest

from ansible_navigator.actions.exec import Action as exec_action
from ansible_navigator.actions.exec import Action as ExecAction


class CommandTestData(NamedTuple):
"""the artifact files test data object"""
"""The artifact files test data object."""

name: str
command: str
Expand All @@ -20,7 +19,11 @@ class CommandTestData(NamedTuple):


def id_from_data(value):
"""return the name from the test data object"""
"""Return the name from the test data object.
:param value: The value from which the test id will be extracted
:returns: The test id
"""
return f" {value.name} "


Expand Down Expand Up @@ -74,13 +77,13 @@ def id_from_data(value):


@pytest.mark.parametrize("data", command_test_data, ids=id_from_data)
def test_artifact_path(data):
"""Test the generation of the command and params"""
def test_artifact_path(data: CommandTestData):
"""Test the generation of the command and params.
:param data: The test data
"""
# pylint: disable=protected-access
command, params = exec_action._generate_command(
exec_command=data.command, exec_shell=data.shell
)
command, params = ExecAction._generate_command(exec_command=data.command, exec_shell=data.shell)
comment = data, command, params
assert command == data.result_command, comment
assert params == data.result_params, comment

0 comments on commit 7eb4ca9

Please sign in to comment.