X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=test%2Fintegration%2Ftestlib%2Fcheck.py;h=3b7dec170286fbebd79d35d46281cf176be6df8d;hb=67e09d80143c4dcad182730a7b73a3d1183e67da;hp=1d1dfb0f8767e7bbfbe7790b26f81b469cf4bbc7;hpb=a5c6c6ea1ab657d83a4d8b064ac9bfa9c16adf63;p=tinc diff --git a/test/integration/testlib/check.py b/test/integration/testlib/check.py index 1d1dfb0f..3b7dec17 100755 --- a/test/integration/testlib/check.py +++ b/test/integration/testlib/check.py @@ -1,6 +1,8 @@ """Simple assertions which print the expected and received values on failure.""" +import os.path import typing as T +from pathlib import Path from .log import log @@ -8,6 +10,12 @@ Val = T.TypeVar("Val") Num = T.TypeVar("Num", int, float) +def blank(value: T.AnyStr) -> None: + """Check that value is an empty or blank string.""" + if not isinstance(value, str) or value.strip(): + raise ValueError(f'expected "{value!r}" to be a blank string') + + def false(value: T.Any) -> None: """Check that value is falsy.""" if value: @@ -50,6 +58,13 @@ def in_range(value: Num, gte: Num, lte: Num) -> None: raise ValueError(f"value {value} must be between {gte} and {lte}") +def lines(text: T.AnyStr, num: int) -> None: + """Check that text splits into `num` lines.""" + rows = text.splitlines() + if len(rows) != num: + raise ValueError(f"expected {num} lines, got {len(rows)}: {rows}") + + def is_in(needle: Val, *haystacks: T.Container[Val]) -> None: """Check that at least one haystack includes needle.""" for haystack in haystacks: @@ -86,3 +101,9 @@ def files_eq(path0: str, path1: str) -> None: if content0 != content1: raise ValueError(f"expected files {path0} and {path1} to match") + + +def file_exists(path: T.Union[str, Path]) -> None: + """Check that file or directory exists.""" + if not os.path.exists(path): + raise ValueError("expected path '{path}' to exist")