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 <taimoorzaeem@gmail.com>
This commit is contained in:
committed by
Wolfgang Walther
parent
ed344c795a
commit
084a8eca55
+8
-24
@@ -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 = {
|
||||
|
||||
@@ -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}"}
|
||||
|
||||
Reference in New Issue
Block a user