1 """Simple assertions which print the expected and received values on failure."""
5 from pathlib import Path
10 Num = T.TypeVar("Num", int, float)
13 def false(value: T.Any) -> None:
14 """Check that value is falsy."""
16 raise ValueError(f'expected "{value}" to be falsy')
19 def true(value: T.Any) -> None:
20 """Check that value is truthy."""
22 raise ValueError(f'expected "{value}" to be truthy', value)
25 def port(value: int) -> None:
26 """Check that value resembles a port."""
27 if not isinstance(value, int) or value < 1 or value > 65535:
28 raise ValueError(f'expected "{value}" to be be a port')
31 def equals(expected: Val, actual: Val) -> None:
32 """Check that the two values are equal."""
33 if expected != actual:
34 raise ValueError(f'expected "{expected}", got "{actual}"')
37 def has_prefix(text: T.AnyStr, prefix: T.AnyStr) -> None:
38 """Check that text has prefix."""
39 if not text.startswith(prefix):
40 raise ValueError(f"expected {text!r} to start with {prefix!r}")
43 def greater(value: Num, than: Num) -> None:
44 """Check that value is greater than the other value."""
46 raise ValueError(f"value {value} must be greater than {than}")
49 def in_range(value: Num, gte: Num, lte: Num) -> None:
50 """Check that value lies in the range [min, max]."""
51 if not gte >= value >= lte:
52 raise ValueError(f"value {value} must be between {gte} and {lte}")
55 def lines(text: T.AnyStr, num: int) -> None:
56 """Check that text splits into `num` lines."""
57 rows = text.splitlines()
59 raise ValueError(f"expected {num} lines, got {len(rows)}: {rows}")
62 def is_in(needle: Val, *haystacks: T.Container[Val]) -> None:
63 """Check that at least one haystack includes needle."""
64 for haystack in haystacks:
65 if needle in haystack:
67 raise ValueError(f'expected any of "{haystacks}" to include "{needle}"')
70 def not_in(needle: Val, *haystacks: T.Container[Val]) -> None:
71 """Check that all haystacks do not include needle."""
72 for haystack in haystacks:
73 if needle in haystack:
74 raise ValueError(f'expected all "{haystacks}" NOT to include "{needle}"')
77 def nodes(node, want_nodes: int) -> None:
78 """Check that node can reach exactly N nodes (including itself)."""
79 log.debug("want %d reachable nodes from tinc %s", want_nodes, node)
80 stdout, _ = node.cmd("dump", "reachable", "nodes")
81 equals(want_nodes, len(stdout.splitlines()))
84 def files_eq(path0: str, path1: str) -> None:
85 """Compare file contents, ignoring whitespace at both ends."""
86 log.debug("comparing files %s and %s", path0, path1)
88 def read(path: str) -> str:
89 log.debug("reading file %s", path)
90 with open(path, "r", encoding="utf-8") as f:
91 return f.read().strip()
93 content0 = read(path0)
94 content1 = read(path1)
96 if content0 != content1:
97 raise ValueError(f"expected files {path0} and {path1} to match")
100 def file_exists(path: T.Union[str, Path]) -> None:
101 """Check that file or directory exists."""
102 if not os.path.exists(path):
103 raise ValueError("expected path '{path}' to exist")