[Concept,05/37] u_boot_pylib: Fix cros_subprocess tests for Python 3

Message ID 20260404213020.372253-6-sjg@u-boot.org
State New
Headers
Series patman: Autolink fixes and AI-assisted patch review |

Commit Message

Simon Glass April 4, 2026, 9:28 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

The cros_subprocess test suite uses str for accumulating output data,
but communicate_filter() sends bytes. This causes a TypeError on every
test.

Change MyOperation to use bytes and update all string comparisons in
the tests to use byte literals. Add TestSubprocess to the u_boot_pylib
test suite.

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

 tools/u_boot_pylib/__main__.py        |  3 ++-
 tools/u_boot_pylib/cros_subprocess.py | 31 ++++++++++++++-------------
 2 files changed, 18 insertions(+), 16 deletions(-)
  

Patch

diff --git a/tools/u_boot_pylib/__main__.py b/tools/u_boot_pylib/__main__.py
index 5f802bd8d86..5d3dec2c709 100755
--- a/tools/u_boot_pylib/__main__.py
+++ b/tools/u_boot_pylib/__main__.py
@@ -15,6 +15,7 @@  sys.path.append(os.path.join(our_path, '..'))
 
 import argparse
 
+from u_boot_pylib import cros_subprocess
 from u_boot_pylib import test_util
 
 
@@ -31,7 +32,7 @@  def run_tests():
     result = test_util.run_test_suites(
         'u_boot_pylib', False, args.verbose, False,
         False, None, to_run, None,
-        ['u_boot_pylib.terminal'])
+        ['u_boot_pylib.terminal', cros_subprocess.TestSubprocess])
 
     sys.exit(0 if result.wasSuccessful() else 1)
 
diff --git a/tools/u_boot_pylib/cros_subprocess.py b/tools/u_boot_pylib/cros_subprocess.py
index cd614f38a64..4ac435b631c 100644
--- a/tools/u_boot_pylib/cros_subprocess.py
+++ b/tools/u_boot_pylib/cros_subprocess.py
@@ -261,9 +261,9 @@  class TestSubprocess(unittest.TestCase):
                 input_to_send: a text string to send when we first get input. We will
                     add \r\n to the string.
             """
-            self.stdout_data = ''
-            self.stderr_data = ''
-            self.combined_data = ''
+            self.stdout_data = b''
+            self.stderr_data = b''
+            self.combined_data = b''
             self.stdin_pipe = None
             self._input_to_send = input_to_send
             if input_to_send:
@@ -305,8 +305,8 @@  class TestSubprocess(unittest.TestCase):
         cmd = 'echo fred >/dev/stderr && false || echo bad'
         plist = Popen([cmd], shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
-        self.assertEqual(plist [0], 'bad\r\n')
-        self.assertEqual(plist [1], 'fred\r\n')
+        self.assertEqual(plist [0], b'bad\r\n')
+        self.assertEqual(plist [1], b'fred\r\n')
 
     def test_shell(self):
         """Check with and without shell works"""
@@ -316,7 +316,7 @@  class TestSubprocess(unittest.TestCase):
         plist = Popen([cmd], shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
         self.assertEqual(len(plist [0]), 0)
-        self.assertEqual(plist [1], 'test\r\n')
+        self.assertEqual(plist [1], b'test\r\n')
 
     def test_list_args(self):
         """Check with and without shell works using list arguments"""
@@ -324,7 +324,7 @@  class TestSubprocess(unittest.TestCase):
         cmd = ['echo', 'test', '>/dev/stderr']
         plist = Popen(cmd, shell=False).communicate_filter(oper.output)
         self._basic_check(plist, oper)
-        self.assertEqual(plist [0], ' '.join(cmd[1:]) + '\r\n')
+        self.assertEqual(plist [0], (' '.join(cmd[1:]) + '\r\n').encode())
         self.assertEqual(len(plist [1]), 0)
 
         oper = TestSubprocess.MyOperation()
@@ -333,7 +333,7 @@  class TestSubprocess(unittest.TestCase):
         cmd = ['echo', 'test', '>/dev/stderr']
         plist = Popen(cmd, shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
-        self.assertEqual(plist [0], '\r\n')
+        self.assertEqual(plist [0], b'\r\n')
 
     def test_cwd(self):
         """Check we can change directory"""
@@ -342,7 +342,7 @@  class TestSubprocess(unittest.TestCase):
             plist = Popen('pwd', shell=shell, cwd='/tmp').communicate_filter(
                 oper.output)
             self._basic_check(plist, oper)
-            self.assertEqual(plist [0], '/tmp\r\n')
+            self.assertEqual(plist [0], b'/tmp\r\n')
 
     def test_env(self):
         """Check we can change environment"""
@@ -354,7 +354,7 @@  class TestSubprocess(unittest.TestCase):
             cmd = 'echo $FRED'
             plist = Popen(cmd, shell=True, env=env).communicate_filter(oper.output)
             self._basic_check(plist, oper)
-            self.assertEqual(plist [0], add and 'fred\r\n' or '\r\n')
+            self.assertEqual(plist [0], add and b'fred\r\n' or b'\r\n')
 
     def test_extra_args(self):
         """Check we can't add extra arguments"""
@@ -374,7 +374,8 @@  class TestSubprocess(unittest.TestCase):
                 shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
         self.assertEqual(len(plist [1]), 0)
-        self.assertEqual(plist [0], prompt + 'Hello Flash\r\r\n')
+        self.assertEqual(plist [0],
+                         (prompt + 'Hello Flash\r\r\n').encode())
 
     def test_isatty(self):
         """Check that ptys appear as terminals to the subprocess"""
@@ -386,16 +387,16 @@  class TestSubprocess(unittest.TestCase):
             both_cmds += cmd % (fd, fd, fd, fd, fd)
         plist = Popen(both_cmds, shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
-        self.assertEqual(plist [0], 'terminal 1\r\n')
-        self.assertEqual(plist [1], 'terminal 2\r\n')
+        self.assertEqual(plist [0], b'terminal 1\r\n')
+        self.assertEqual(plist [1], b'terminal 2\r\n')
 
         # Now try with PIPE and make sure it is not a terminal
         oper = TestSubprocess.MyOperation()
         plist = Popen(both_cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                 shell=True).communicate_filter(oper.output)
         self._basic_check(plist, oper)
-        self.assertEqual(plist [0], 'not 1\n')
-        self.assertEqual(plist [1], 'not 2\n')
+        self.assertEqual(plist [0], b'not 1\n')
+        self.assertEqual(plist [1], b'not 2\n')
 
 if __name__ == '__main__':
     unittest.main()