From 2ce3752f90d0b4f27fa723a0bb7723187949f972 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 26 Jul 2018 18:08:49 -0700 Subject: [PATCH] Add a test library This adds empty __init__.py to tests so that a lib.py library of helper functions can be imported from the tests. Add captured_output to use with composer-cli tests to capture stdout/err output from the functions. (cherry picked from commit eeae331ba0af2609214fafbfad197c2b931e3675) --- tests/__init__.py | 0 tests/composer/__init__.py | 0 tests/lib.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/composer/__init__.py create mode 100644 tests/lib.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/composer/__init__.py b/tests/composer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/lib.py b/tests/lib.py new file mode 100644 index 00000000..d77c3460 --- /dev/null +++ b/tests/lib.py @@ -0,0 +1,29 @@ +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import sys +from contextlib import contextmanager +from StringIO import StringIO + +@contextmanager +def captured_output(): + new_out, new_err = StringIO(), StringIO() + old_out, old_err = sys.stdout, sys.stderr + try: + sys.stdout, sys.stderr = new_out, new_err + yield sys.stdout, sys.stderr + finally: + sys.stdout, sys.stderr = old_out, old_err