[jhbuild/more-py3-fixes: 8/8] pprint_output: properly decode the output



commit b10d273823bbb20c02828402ed8ebb0bb47bc589
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Wed Sep 25 19:29:20 2019 +0200

    pprint_output: properly decode the output
    
    Always pass text to the formatter function.
    
    This resulted in the tinderbox formatter not stripping line endings replacing
    them with extra <br> tags.
    
    Also adds a test.

 jhbuild/frontends/tinderbox.py |  7 +++----
 jhbuild/utils/cmds.py          | 11 +++++++----
 tests/test_main.py             | 14 ++++++++++++++
 3 files changed, 24 insertions(+), 8 deletions(-)
---
diff --git a/jhbuild/frontends/tinderbox.py b/jhbuild/frontends/tinderbox.py
index 9cb31054..f1b51db5 100644
--- a/jhbuild/frontends/tinderbox.py
+++ b/jhbuild/frontends/tinderbox.py
@@ -24,9 +24,9 @@ import logging
 import sys
 
 from jhbuild.utils import cmds
-from jhbuild.utils import sysid, _, udecode, open_text
+from jhbuild.utils import sysid, _, open_text
 from jhbuild.errors import CommandError, FatalError
-from jhbuild.utils.compat import string_types, text_type
+from jhbuild.utils.compat import string_types
 from . import buildscript
 
 index_header = '''<html>
@@ -132,8 +132,7 @@ buildlog_footer = '''
 '''
 
 def escape(string):
-    if not isinstance(string, text_type):
-        string = udecode(string)
+    assert isinstance(string, string_types)
     string = string.replace('&', '&amp;').replace('<','&lt;').replace(
             '>','&gt;').replace('\n','<br/>').replace(
             '\t','&nbsp;&nbsp;&nbsp;&nbsp;')
diff --git a/jhbuild/utils/cmds.py b/jhbuild/utils/cmds.py
index 52fd2986..71bbb7b4 100644
--- a/jhbuild/utils/cmds.py
+++ b/jhbuild/utils/cmds.py
@@ -177,6 +177,9 @@ def pprint_output(pipe, format_line):
     if not getattr(sys.stdin, "closed", True):
         read_set.append(sys.stdin)
 
+    def format_line_text(data, *args):
+        return format_line(udecode(data), *args)
+
     out_data = err_data = b''
     try:
         while read_set:
@@ -192,7 +195,7 @@ def pprint_output(pipe, format_line):
                 out_data += out_chunk
                 while b'\n' in out_data:
                     pos = out_data.find(b'\n')
-                    format_line(out_data[:pos+1], False)
+                    format_line_text(out_data[:pos+1], False)
                     out_data = out_data[pos+1:]
 
             if pipe.stderr in rlist:
@@ -203,7 +206,7 @@ def pprint_output(pipe, format_line):
                 err_data += err_chunk
                 while b'\n' in err_data:
                     pos = err_data.find(b'\n')
-                    format_line(err_data[:pos+1], True)
+                    format_line_text(err_data[:pos+1], True)
                     err_data = err_data[pos+1:]
 
             # safeguard against tinderbox that close stdin
@@ -214,9 +217,9 @@ def pprint_output(pipe, format_line):
 
         # flush the remainder of stdout/stderr data lacking newlines
         if out_data:
-            format_line(out_data, False)
+            format_line_text(out_data, False)
         if err_data:
-            format_line(err_data, True)
+            format_line_text(err_data, True)
 
     except KeyboardInterrupt:
         # interrupt received.  Send SIGINT to child process.
diff --git a/tests/test_main.py b/tests/test_main.py
index 3ada3085..ff8959c9 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -55,6 +55,7 @@ import jhbuild.moduleset
 import jhbuild.utils.cmds
 import jhbuild.versioncontrol.tarball
 from jhbuild.utils.sxml import sxml_to_string
+from jhbuild.utils.cmds import pprint_output
 
 from . import mock
 
@@ -125,6 +126,19 @@ class ModulesetXMLTest(unittest.TestCase):
             shutil.rmtree(temp_dir)
 
 
+class CmdTestCase(unittest.TestCase):
+
+    def test_pprint_output(self):
+        try:
+            p = subprocess.Popen(
+                ["echo", "foo\nbar"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        except OSError:
+            raise unittest.SkipTest("no echo command")
+        arguments = []
+        pprint_output(p, lambda *args: arguments.append(args))
+        self.assertEqual(arguments, [('foo\n', False), ('bar\n', False)])
+
+
 class _TestConfig(jhbuild.config.Config):
 
     # The Config base class calls setup_env() in the constructor, but


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]