chore(tests): add more flexibility to where we want to print mustcontain output for testing response

This commit is contained in:
RhodeCode Admin 2023-11-07 09:40:37 +01:00
parent d7670b4e0c
commit ce10169caa

View file

@ -18,6 +18,7 @@
import threading
import time
import sys
import logging
import os.path
import subprocess
@ -31,7 +32,7 @@ from urllib.parse import unquote_plus
import webob
from webtest.app import TestResponse, TestApp
from webtest.compat import print_stderr
import pytest
@ -51,6 +52,10 @@ from rhodecode.tests import login_user_session
log = logging.getLogger(__name__)
def print_to_func(value, print_to=sys.stderr):
print(value, file=print_to)
class CustomTestResponse(TestResponse):
def _save_output(self, out):
@ -60,7 +65,7 @@ class CustomTestResponse(TestResponse):
def mustcontain(self, *strings, **kw):
"""
Assert that the response contains all of the strings passed
Assert that the response contains all the strings passed
in as arguments.
Equivalent to::
@ -68,6 +73,8 @@ class CustomTestResponse(TestResponse):
assert string in res
"""
print_body = kw.pop('print_body', False)
print_to = kw.pop('print_to', sys.stderr)
if 'no' in kw:
no = kw['no']
del kw['no']
@ -82,18 +89,18 @@ class CustomTestResponse(TestResponse):
for s in strings:
if s not in self:
print_stderr(f"Actual response (no {s!r}):")
print_stderr(f"body output saved as `{f}`")
print_to_func(f"Actual response (no {s!r}):", print_to=print_to)
print_to_func(f"body output saved as `{f}`", print_to=print_to)
if print_body:
print_stderr(str(self))
print_to_func(str(self), print_to=print_to)
raise IndexError(f"Body does not contain string {s!r}, body output saved as {f}")
for no_s in no:
if no_s in self:
print_stderr(f"Actual response (has {no_s!r})")
print_stderr(f"body output saved as `{f}`")
print_to_func(f"Actual response (has {no_s!r})", print_to=print_to)
print_to_func(f"body output saved as `{f}`", print_to=print_to)
if print_body:
print_stderr(str(self))
print_to_func(str(self), print_to=print_to)
raise IndexError(f"Body contains bad string {no_s!r}, body output saved as {f}")
def assert_response(self):