From 598d1786622cca66d0d175ab8576c2b146e03f26 Mon Sep 17 00:00:00 2001 From: Andrey Maslennikov Date: Thu, 20 Jul 2023 16:41:13 +0200 Subject: [PATCH 1/6] Add type annotations for backend/base.py --- testinfra/backend/base.py | 67 ++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index a1a3be2b..2720e4b6 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -17,6 +17,7 @@ import shlex import subprocess import urllib.parse +from typing import Optional logger = logging.getLogger("testinfra") @@ -26,25 +27,25 @@ class CommandResult: def __init__( self, - backend, - exit_status, - command, - stdout_bytes, - stderr_bytes, - stdout=None, - stderr=None, + backend: "BaseBackend", + exit_status: int, + command: bytes, + stdout_bytes: bytes, + stderr_bytes: bytes, + stdout: Optional[str] = None, + stderr: Optional[str] = None, ): - self.exit_status = exit_status - self._stdout_bytes = stdout_bytes - self._stderr_bytes = stderr_bytes - self._stdout = stdout - self._stderr = stderr - self.command = command - self._backend = backend + self.exit_status: int = exit_status + self._stdout_bytes: bytes = stdout_bytes + self._stderr_bytes: bytes = stderr_bytes + self._stdout: Optional[str] = stdout + self._stderr: Optional[str] = stderr + self.command: bytes = command + self._backend: BaseBackend = backend super().__init__() @property - def succeeded(self): + def succeeded(self) -> bool: """Returns whether the command was successful >>> host.run("true").succeeded @@ -53,7 +54,7 @@ def succeeded(self): return self.exit_status == 0 @property - def failed(self): + def failed(self) -> bool: """Returns whether the command failed >>> host.run("false").failed @@ -62,7 +63,7 @@ def failed(self): return self.exit_status != 0 @property - def rc(self): + def rc(self) -> int: """Gets the returncode of a command >>> host.run("true").rc @@ -71,30 +72,30 @@ def rc(self): return self.exit_status @property - def stdout(self): + def stdout(self) -> str: if self._stdout is None: self._stdout = self._backend.decode(self._stdout_bytes) return self._stdout @property - def stderr(self): + def stderr(self) -> str: if self._stderr is None: self._stderr = self._backend.decode(self._stderr_bytes) return self._stderr @property - def stdout_bytes(self): + def stdout_bytes(self) -> bytes: if self._stdout_bytes is None: self._stdout_bytes = self._backend.encode(self._stdout) return self._stdout_bytes @property - def stderr_bytes(self): + def stderr_bytes(self) -> bytes: if self._stderr_bytes is None: self._stderr_bytes = self._backend.encode(self._stderr) return self._stderr_bytes - def __repr__(self): + def __repr__(self) -> str: return ( "CommandResult(command={!r}, exit_status={}, stdout={!r}, " "stderr={!r})" ).format( @@ -112,11 +113,11 @@ class BaseBackend(metaclass=abc.ABCMeta): HAS_RUN_ANSIBLE = False NAME: str - def __init__(self, hostname, sudo=False, sudo_user=None, *args, **kwargs): + def __init__(self, hostname, sudo: bool = False, sudo_user=None, *args, **kwargs): self._encoding = None self._host = None self.hostname = hostname - self.sudo = sudo + self.sudo: bool = sudo self.sudo_user = sudo_user super().__init__() @@ -245,7 +246,7 @@ def parse_containerspec(containerspec): user, name = name.split("@", 1) return name, user - def get_encoding(self) -> str: + def get_encoding(self): encoding = None for python in ("python3", "python"): cmd = self.run( @@ -271,19 +272,27 @@ def encoding(self): self._encoding = self.get_encoding() return self._encoding - def decode(self, data): + def decode(self, data: bytes) -> str: try: return data.decode("ascii") except UnicodeDecodeError: return data.decode(self.encoding) - def encode(self, data): + def encode(self, data: str) -> bytes: try: return data.encode("ascii") except UnicodeEncodeError: return data.encode(self.encoding) - def result(self, *args, **kwargs): - result = CommandResult(self, *args, **kwargs) + def result(self, *args, **kwargs) -> CommandResult: + result = CommandResult( + backend=self, + exit_status=kwargs.get("exit_status", 0), + command=kwargs.get("command", b""), + stdout_bytes=kwargs.get("stdout_bytes", b""), + stderr_bytes=kwargs.get("stderr_bytes", b""), + stdout=kwargs.get("stdout"), + stderr=kwargs.get("stderr"), + ) logger.debug("RUN %s", result) return result From 26528d77494a82b5229deac80094283459146539 Mon Sep 17 00:00:00 2001 From: Andrey Maslennikov Date: Fri, 21 Jul 2023 18:16:28 +0200 Subject: [PATCH 2/6] Attempt to fix errors --- testinfra/backend/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 2720e4b6..148825a5 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -286,7 +286,7 @@ def encode(self, data: str) -> bytes: def result(self, *args, **kwargs) -> CommandResult: result = CommandResult( - backend=self, + backend=kwargs.get("backend", self), exit_status=kwargs.get("exit_status", 0), command=kwargs.get("command", b""), stdout_bytes=kwargs.get("stdout_bytes", b""), From 2a4f56215a9606bf03bd71471d3ccd7e0755dbb6 Mon Sep 17 00:00:00 2001 From: Andrey Maslennikov Date: Fri, 21 Jul 2023 19:42:28 +0200 Subject: [PATCH 3/6] Fix lint error reported by local run --- testinfra/backend/base.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 148825a5..15051579 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -17,7 +17,7 @@ import shlex import subprocess import urllib.parse -from typing import Optional +from typing import Any, Optional logger = logging.getLogger("testinfra") @@ -113,7 +113,14 @@ class BaseBackend(metaclass=abc.ABCMeta): HAS_RUN_ANSIBLE = False NAME: str - def __init__(self, hostname, sudo: bool = False, sudo_user=None, *args, **kwargs): + def __init__( + self, + hostname: str, + sudo: bool = False, + sudo_user: bool = False, + *args: Any, + **kwargs: Any, + ): self._encoding = None self._host = None self.hostname = hostname @@ -284,7 +291,7 @@ def encode(self, data: str) -> bytes: except UnicodeEncodeError: return data.encode(self.encoding) - def result(self, *args, **kwargs) -> CommandResult: + def result(self, *args: Any, **kwargs: Any) -> CommandResult: result = CommandResult( backend=kwargs.get("backend", self), exit_status=kwargs.get("exit_status", 0), From c2cc3025838385a52cc37b228ab68f658c4e0946 Mon Sep 17 00:00:00 2001 From: Andrey Maslennikov Date: Fri, 21 Jul 2023 19:19:09 +0000 Subject: [PATCH 4/6] Actual test and linter fixes --- testinfra/backend/base.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 15051579..18fd0b03 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -117,7 +117,7 @@ def __init__( self, hostname: str, sudo: bool = False, - sudo_user: bool = False, + sudo_user: Optional[bool] = None, *args: Any, **kwargs: Any, ): @@ -125,7 +125,7 @@ def __init__( self._host = None self.hostname = hostname self.sudo: bool = sudo - self.sudo_user = sudo_user + self.sudo_user: Optional[bool] = sudo_user super().__init__() def set_host(self, host): @@ -292,14 +292,6 @@ def encode(self, data: str) -> bytes: return data.encode(self.encoding) def result(self, *args: Any, **kwargs: Any) -> CommandResult: - result = CommandResult( - backend=kwargs.get("backend", self), - exit_status=kwargs.get("exit_status", 0), - command=kwargs.get("command", b""), - stdout_bytes=kwargs.get("stdout_bytes", b""), - stderr_bytes=kwargs.get("stderr_bytes", b""), - stdout=kwargs.get("stdout"), - stderr=kwargs.get("stderr"), - ) + result = CommandResult(self, *args, **kwargs) logger.debug("RUN %s", result) return result From f2850d63e3587ac744df7bf7c4df8fcd6eff3267 Mon Sep 17 00:00:00 2001 From: Philippe Pepiot Date: Fri, 21 Jul 2023 23:17:28 +0200 Subject: [PATCH 5/6] Update testinfra/backend/base.py --- testinfra/backend/base.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 18fd0b03..3d5f6733 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -35,13 +35,13 @@ def __init__( stdout: Optional[str] = None, stderr: Optional[str] = None, ): - self.exit_status: int = exit_status - self._stdout_bytes: bytes = stdout_bytes - self._stderr_bytes: bytes = stderr_bytes - self._stdout: Optional[str] = stdout - self._stderr: Optional[str] = stderr - self.command: bytes = command - self._backend: BaseBackend = backend + self.exit_status = exit_status + self._stdout_bytes = stdout_bytes + self._stderr_bytes = stderr_bytes + self._stdout = stdout + self._stderr = stderr + self.command = command + self._backend = backend super().__init__() @property From 5b342644062ad1ff5394a61bf95b58641479aaf4 Mon Sep 17 00:00:00 2001 From: Philippe Pepiot Date: Fri, 21 Jul 2023 23:18:39 +0200 Subject: [PATCH 6/6] Update testinfra/backend/base.py --- testinfra/backend/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testinfra/backend/base.py b/testinfra/backend/base.py index 3d5f6733..1d7a84cc 100644 --- a/testinfra/backend/base.py +++ b/testinfra/backend/base.py @@ -124,8 +124,8 @@ def __init__( self._encoding = None self._host = None self.hostname = hostname - self.sudo: bool = sudo - self.sudo_user: Optional[bool] = sudo_user + self.sudo = sudo + self.sudo_user = sudo_user super().__init__() def set_host(self, host):