From 084a8eca554e32006845fac8266912e1dddd7afa Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Mon, 18 May 2026 17:33:41 +0500 Subject: [PATCH] test(io): move utility functions to util.py The functions `drain_stdout` and `match_log` should be in `util.py` so they can be reused in other modules. Signed-off-by: Taimoor Zaeem --- test/io/test_io.py | 32 ++++++++------------------------ test/io/util.py | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/test/io/test_io.py b/test/io/test_io.py index f66e4377a..9dc126932 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -8,7 +8,14 @@ import time import pytest from config import CONFIGSDIR, FIXTURES, SECRET -from util import Thread, jwtauthheader, parse_server_timings_header, relativeSeconds +from util import ( + Thread, + jwtauthheader, + parse_server_timings_header, + relativeSeconds, + drain_stdout, + match_log, +) from postgrest import ( freeport, is_ipv6, @@ -23,29 +30,6 @@ from postgrest import ( ) -def match_log(output, matchers): - ito = iter(output) - itm = iter(matchers) - nextMatcher = next(itm, None) - while nextMatcher is not None and (line := next(ito, None)) is not None: - if re.match(nextMatcher, line) is not None: - nextMatcher = next(itm, None) - if nextMatcher is not None: - raise AssertionError( - f"Expected log line matching {nextMatcher} not found in output" - ) - - -def drain_stdout(proc): - lines = [] - while True: - chunk = proc.read_stdout(nlines=20) - if not chunk: - break - lines.extend(chunk) - return lines - - def test_connect_with_dburi(dburi, defaultenv): "Connecting with db-uri instead of LIPQ* environment variables should work." defaultenv_without_libpq = { diff --git a/test/io/util.py b/test/io/util.py index a375de2da..6e0de0747 100644 --- a/test/io/util.py +++ b/test/io/util.py @@ -1,3 +1,4 @@ +import re import threading import jwt from datetime import datetime, timedelta, timezone @@ -22,6 +23,29 @@ class Thread(threading.Thread): raise self._exception +def match_log(output, matchers): + ito = iter(output) + itm = iter(matchers) + nextMatcher = next(itm, None) + while nextMatcher is not None and (line := next(ito, None)) is not None: + if re.match(nextMatcher, line) is not None: + nextMatcher = next(itm, None) + if nextMatcher is not None: + raise AssertionError( + f"Expected log line matching {nextMatcher} not found in output" + ) + + +def drain_stdout(proc): + lines = [] + while True: + chunk = proc.read_stdout(nlines=20) + if not chunk: + break + lines.extend(chunk) + return lines + + def authheader(token): "Bearer token HTTP authorization header." return {"Authorization": f"Bearer {token}"}