diff mbox series

[U-Boot,v2,21/29] binman: Move capture_sys_output() to test_util

Message ID 20180706162742.186950-22-sjg@chromium.org
State Accepted
Commit c3f9454103462e2cfcc5a9120b893f4ea250650a
Delegated to: Simon Glass
Headers show
Series binman: Add more tests and support for updating the device tree | expand

Commit Message

Simon Glass July 6, 2018, 4:27 p.m. UTC
This function is useful in various tests. Move it into the common test
utility module.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2: None

 tools/binman/elf_test.py   | 22 ++--------------------
 tools/binman/image_test.py |  2 +-
 tools/patman/test_util.py  | 21 +++++++++++++++++++++
 3 files changed, 24 insertions(+), 21 deletions(-)

Comments

Simon Glass July 9, 2018, 7:53 p.m. UTC | #1
On 6 July 2018 at 10:27, Simon Glass <sjg@chromium.org> wrote:
> This function is useful in various tests. Move it into the common test
> utility module.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v2: None
>
>  tools/binman/elf_test.py   | 22 ++--------------------
>  tools/binman/image_test.py |  2 +-
>  tools/patman/test_util.py  | 21 +++++++++++++++++++++
>  3 files changed, 24 insertions(+), 21 deletions(-)

Applied to u-boot-dm.
diff mbox series

Patch

diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py
index fb6e451cf0..9c8f1feca8 100644
--- a/tools/binman/elf_test.py
+++ b/tools/binman/elf_test.py
@@ -4,33 +4,15 @@ 
 #
 # Test for the elf module
 
-from contextlib import contextmanager
 import os
 import sys
 import unittest
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 import elf
+import test_util
 
 binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
 
-# 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
-
 
 class FakeEntry:
     def __init__(self, contents_size):
@@ -110,7 +92,7 @@  class TestElf(unittest.TestCase):
         entry = FakeEntry(20)
         section = FakeSection()
         elf_fname = os.path.join(binman_dir, 'test', 'u_boot_binman_syms')
-        with capture_sys_output() as (stdout, stderr):
+        with test_util.capture_sys_output() as (stdout, stderr):
             syms = elf.LookupAndWriteSymbols(elf_fname, entry, section)
         elf.debug = False
         self.assertTrue(len(stdout.getvalue()) > 0)
diff --git a/tools/binman/image_test.py b/tools/binman/image_test.py
index 45dd2378c8..3775e1afb0 100644
--- a/tools/binman/image_test.py
+++ b/tools/binman/image_test.py
@@ -7,7 +7,7 @@ 
 import unittest
 
 from image import Image
-from elf_test import capture_sys_output
+from test_util import capture_sys_output
 
 class TestImage(unittest.TestCase):
     def testInvalidFormat(self):
diff --git a/tools/patman/test_util.py b/tools/patman/test_util.py
index 1a33c997c4..0e79af871a 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