Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test helpers, rewrite integration tests #100

Merged
merged 9 commits into from
May 25, 2021
2 changes: 1 addition & 1 deletion riotfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
pys=[3.6, 3.7, 3.8, 3.9],
pkgs={
"pytest": latest,
"pytest": "==6.1.2",
jd marked this conversation as resolved.
Show resolved Hide resolved
"pytest-cov": latest,
"mock": latest,
},
Expand Down
73 changes: 73 additions & 0 deletions tests/proctest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import dataclasses
import os
import subprocess
import sys
import tempfile
import typing as t


if t.TYPE_CHECKING or sys.version_info[:2] >= (3, 9):
_T_CompletedProcess = subprocess.CompletedProcess[str]
else:
_T_CompletedProcess = subprocess.CompletedProcess
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved

_T_Path = t.Union[str, "os.PathLike[t.Any]"]


@dataclasses.dataclass(frozen=True)
class ProcResult:
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
proc: _T_CompletedProcess = dataclasses.field(repr=False)

@property
def stdout(self) -> str:
return self.proc.stdout

@property
def stderr(self) -> str:
return self.proc.stderr

@property
def returncode(self) -> int:
return self.proc.returncode


class TestDir:
def __init__(self):
self._tmpdir = tempfile.TemporaryDirectory()
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved
self.cwd = self._tmpdir.name

def __fspath__(self):
"""Implement Pathlike interface."""
return self.cwd

def cd(self, path: _T_Path) -> None:
res_path = path if os.path.isabs(path) else os.path.join(self.cwd, path)
self.cwd = res_path

def mkdir(self, path: _T_Path) -> None:
res_path = path if os.path.isabs(path) else os.path.join(self.cwd, path)
os.mkdir(res_path)

def mkfile(self, path: _T_Path, contents: str) -> None:
res_path = path if os.path.isabs(path) else os.path.join(self.cwd, path)
with open(res_path, "w") as f:
f.write(contents)

def run(
self,
args: t.Union[str, t.Sequence[str]],
env: t.Optional[t.Dict[str, str]] = None,
cwd: t.Optional[_T_Path] = None,
) -> ProcResult:
if isinstance(args, str):
args = args.split(" ")
Kyle-Verhoog marked this conversation as resolved.
Show resolved Hide resolved

p = subprocess.run(
args,
env=env,
encoding=sys.getdefaultencoding(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.cwd,
)
return ProcResult(p)
Loading