X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=test%2Fintegration%2Ftestlib%2Futil.py;h=344958e3fed293275bafc0892c5dd5906540a9c0;hb=021293e0d03de8d29b22104a8f9bef625b135640;hp=8102bca2762c1d6afc67acee33fc7bbe38aaee6d;hpb=9a012e485a2ed5ea5a28903d93bc625767bb20b2;p=tinc diff --git a/test/integration/testlib/util.py b/test/integration/testlib/util.py index 8102bca2..344958e3 100755 --- a/test/integration/testlib/util.py +++ b/test/integration/testlib/util.py @@ -7,6 +7,8 @@ import random import string import socket import typing as T +import tempfile +from pathlib import Path from . import check from .log import log @@ -31,6 +33,23 @@ def random_port() -> int: log.debug("could not bind to random port %d", port, exc_info=ex) +def temp_file(content: str) -> str: + """Create a temporary file and write text content into it.""" + file = tempfile.mktemp() + with open(file, "w", encoding="utf-8") as f: + f.write(content) + return file + + +def remove_file(path: T.Union[str, Path]) -> bool: + """Try to remove file without failing if it does not exist.""" + try: + os.remove(path) + return True + except FileNotFoundError: + return False + + def random_string(k: int) -> str: """Generate a random alphanumeric string of length k.""" return "".join(random.choices(_ALPHA_NUMERIC, k=k))