From c3f9454103462e2cfcc5a9120b893f4ea250650a Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jul 2018 10:27:34 -0600 Subject: binman: Move capture_sys_output() to test_util This function is useful in various tests. Move it into the common test utility module. Signed-off-by: Simon Glass --- tools/patman/test_util.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tools/patman') diff --git a/tools/patman/test_util.py b/tools/patman/test_util.py index 1a33c997c4c..0e79af871ab 100644 --- a/tools/patman/test_util.py +++ b/tools/patman/test_util.py @@ -3,12 +3,19 @@ # Copyright (c) 2016 Google, Inc # +from contextlib import contextmanager import glob import os import sys import command +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + + def RunTestCoverage(prog, filter_fname, exclude_list, build_dir, required=None): """Run tests and check that we get 100% coverage @@ -62,3 +69,17 @@ def RunTestCoverage(prog, filter_fname, exclude_list, build_dir, required=None): ok = False if not ok: raise ValueError('Test coverage failure') + + +# Use this to suppress stdout/stderr output: +# with capture_sys_output() as (stdout, stderr) +# ...do something... +@contextmanager +def capture_sys_output(): + capture_out, capture_err = StringIO(), StringIO() + old_out, old_err = sys.stdout, sys.stderr + try: + sys.stdout, sys.stderr = capture_out, capture_err + yield capture_out, capture_err + finally: + sys.stdout, sys.stderr = old_out, old_err -- cgit v1.2.3