Skip to content

Commit

Permalink
Add docstrings and type hints to testinfra verifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Nov 26, 2024
1 parent 4a66f71 commit 025abfe
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 24 deletions.
8 changes: 0 additions & 8 deletions .config/pydoclint-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ src/molecule/dependency/shell.py
DOC107: Method `Shell.__init__`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `Shell.__init__`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [config: ].
--------------------
src/molecule/verifier/testinfra.py
DOC106: Method `Testinfra.__init__`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
DOC107: Method `Testinfra.__init__`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC101: Method `Testinfra._get_tests`: Docstring contains fewer arguments than in function signature.
DOC106: Method `Testinfra._get_tests`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
DOC107: Method `Testinfra._get_tests`: The option `--arg-type-hints-in-signature` is `True` but not all args in the signature have type hints
DOC103: Method `Testinfra._get_tests`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [action_args: ].
--------------------
tests/conftest.py
DOC101: Function `reset_pytest_vars`: Docstring contains fewer arguments than in function signature.
DOC106: Function `reset_pytest_vars`: The option `--arg-type-hints-in-signature` is `True` but there are no argument type hints in the signature
Expand Down
26 changes: 26 additions & 0 deletions src/molecule/verifier/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,46 @@


class VerifierSchemaName(TypedDict):
"""Verifier schema name definition.
Attributes:
type: schema type.
allowed: list of allowed verifier names.
"""

type: str
allowed: list[str]


class VerifierSchema(TypedDict):
"""Verifier schema.
Attributes:
name: Schema name container.
"""

name: VerifierSchemaName


class VerifierDef(TypedDict):
"""Verifier schema container.
Attributes:
type: Schema type.
schema: Schema container.
"""

type: str
schema: VerifierSchema


class Schema(TypedDict):
"""Verifier schema definition.
Attributes:
verifier: Verifier schema container.
"""

verifier: VerifierDef


Expand Down
78 changes: 62 additions & 16 deletions src/molecule/verifier/testinfra.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@
import logging
import os

from pathlib import Path
from typing import TYPE_CHECKING

from molecule import util
from molecule.api import Verifier


if TYPE_CHECKING:
from molecule.config import Config
from molecule.verifier.base import Schema


LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -89,7 +97,7 @@ class Testinfra(Verifier):
.. _`Testinfra`: https://testinfra.readthedocs.io
"""

def __init__(self, config=None) -> None: # type: ignore[no-untyped-def] # noqa: ANN001
def __init__(self, config: Config) -> None:
"""Set up the requirements to execute ``testinfra`` and returns None.
Args:
Expand All @@ -100,11 +108,21 @@ def __init__(self, config=None) -> None: # type: ignore[no-untyped-def] # noqa
self._tests = [] # type: ignore[var-annotated]

@property
def name(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def name(self) -> str:
"""Name of the verifier.
Returns:
The name of the verifier.
"""
return "testinfra"

@property
def default_options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def default_options(self) -> dict[str, str]:
"""Get default CLI arguments provided to ``cmd``.
Returns:
The default verifier options.
"""
d = self._config.driver.testinfra_options
d["p"] = "no:cacheprovider"
if self._config.debug:
Expand All @@ -118,7 +136,12 @@ def default_options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
# NOTE(retr0h): Override the base classes' options() to handle
# ``ansible-galaxy`` one-off.
@property
def options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def options(self) -> dict[str, str]:
"""The computed options for this verifier.
Returns:
The combined dictionary of default options and those specified in the config.
"""
o = self._config.config["verifier"]["options"]
# NOTE(retr0h): Remove verbose options added by the user while in
# debug.
Expand All @@ -128,14 +151,24 @@ def options(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
return util.merge_dicts(self.default_options, o)

@property
def default_env(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def default_env(self): # noqa: ANN201
"""Get default env variables provided to ``cmd``.
Returns:
The default verifier environment variables.
"""
env = util.merge_dicts(os.environ, self._config.env)
env = util.merge_dicts(env, self._config.provisioner.env)

return env # noqa: RET504

@property
def additional_files_or_dirs(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def additional_files_or_dirs(self) -> list[str]:
"""Additional paths related to the verifier.
Returns:
List of files and directories to use with this verifier.
"""
files_list = []
c = self._config.config
for f in c["verifier"]["additional_files_or_dirs"]:
Expand All @@ -147,8 +180,8 @@ def additional_files_or_dirs(self): # type: ignore[no-untyped-def] # noqa: ANN

return files_list

def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
"""Bake a ``testinfra`` command so it's ready to execute and returns None."""
def bake(self) -> None:
"""Bake a ``testinfra`` command so it's ready to execute."""
options = self.options
verbose_flag = util.verbose_flag(options)
args = verbose_flag
Expand All @@ -160,22 +193,27 @@ def bake(self): # type: ignore[no-untyped-def] # noqa: ANN201
*args,
]

def execute(self, action_args=None): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D102
def execute(self, action_args: list[str] | None = None) -> None:
"""Execute ``cmd``.
Args:
action_args: list of arguments to be passed.
"""
if not self.enabled:
msg = "Skipping, verifier is disabled."
LOG.warning(msg)
return

if self._config:
self._tests = self._get_tests(action_args) # type: ignore[no-untyped-call]
self._tests = self._get_tests(action_args)
else:
self._tests = []
if not len(self._tests) > 0:
msg = "Skipping, no tests found."
LOG.warning(msg)
return

self.bake() # type: ignore[no-untyped-call]
self.bake()

msg = f"Executing Testinfra tests found in {self.directory}/..."
LOG.info(msg)
Expand All @@ -184,19 +222,22 @@ def execute(self, action_args=None): # type: ignore[no-untyped-def] # noqa: AN
self._testinfra_command, # type: ignore[arg-type]
env=self.env,
debug=self._config.debug,
cwd=self._config.scenario.directory,
cwd=Path(self._config.scenario.directory),
)
if result.returncode == 0:
msg = "Verifier completed successfully."
LOG.info(msg)
else:
util.sysexit(result.returncode)

def _get_tests(self, action_args=None): # type: ignore[no-untyped-def] # noqa: ANN001, ANN202
"""Walk the verifier's directory for tests and returns a list.
def _get_tests(self, action_args: list[str] | None = None) -> list[str]:
"""Walk the verifier's directory for tests.
Args:
action_args: List of paths to search.
Returns:
list
List of test files.
"""
if action_args:
tests = []
Expand All @@ -221,7 +262,12 @@ def _get_tests(self, action_args=None): # type: ignore[no-untyped-def] # noqa:
+ self.additional_files_or_dirs,
)

def schema(self): # type: ignore[no-untyped-def] # noqa: ANN201, D102
def schema(self) -> Schema:
"""Return validation schema.
Returns:
Verifier schema.
"""
return {
"verifier": {
"type": "dict",
Expand Down

0 comments on commit 025abfe

Please sign in to comment.