[PATCH v2 03/41] test/py: Allow passing input to a program

Simon Glass sjg at chromium.org
Sun Oct 24 01:25:57 CEST 2021


When running a program on the host, allow input to be passed in as stdin.
This is needed for running sfdisk, for example.

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

(no changes since v1)

 test/py/multiplexed_log.py | 8 +++++---
 test/py/u_boot_utils.py    | 5 +++--
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/test/py/multiplexed_log.py b/test/py/multiplexed_log.py
index 545a7743022..9325fae46d2 100644
--- a/test/py/multiplexed_log.py
+++ b/test/py/multiplexed_log.py
@@ -109,7 +109,7 @@ class RunAndLog(object):
         """Clean up any resources managed by this object."""
         pass
 
-    def run(self, cmd, cwd=None, ignore_errors=False):
+    def run(self, cmd, cwd=None, ignore_errors=False, stdin=None):
         """Run a command as a sub-process, and log the results.
 
         The output is available at self.output which can be useful if there is
@@ -123,6 +123,7 @@ class RunAndLog(object):
                 function will simply return if the command cannot be executed
                 or exits with an error code, otherwise an exception will be
                 raised if such problems occur.
+            stdin: Input string to pass to the command as stdin (or None)
 
         Returns:
             The output as a string.
@@ -135,8 +136,9 @@ class RunAndLog(object):
 
         try:
             p = subprocess.Popen(cmd, cwd=cwd,
-                stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
-            (stdout, stderr) = p.communicate()
+                stdin=subprocess.PIPE if stdin else None,
+                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+            (stdout, stderr) = p.communicate(input=stdin)
             if stdout is not None:
                 stdout = stdout.decode('utf-8')
             if stderr is not None:
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py
index e816c7fbb6a..f44442e0c77 100644
--- a/test/py/u_boot_utils.py
+++ b/test/py/u_boot_utils.py
@@ -154,7 +154,7 @@ def wait_until_file_open_fails(fn, ignore_errors):
         return
     raise Exception('File can still be opened')
 
-def run_and_log(u_boot_console, cmd, ignore_errors=False):
+def run_and_log(u_boot_console, cmd, ignore_errors=False, stdin=None):
     """Run a command and log its output.
 
     Args:
@@ -166,6 +166,7 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False):
             will simply return if the command cannot be executed or exits with
             an error code, otherwise an exception will be raised if such
             problems occur.
+        stdin: Input string to pass to the command as stdin (or None)
 
     Returns:
         The output as a string.
@@ -173,7 +174,7 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False):
     if isinstance(cmd, str):
         cmd = cmd.split()
     runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
-    output = runner.run(cmd, ignore_errors=ignore_errors)
+    output = runner.run(cmd, ignore_errors=ignore_errors, stdin=stdin)
     runner.close()
     return output
 
-- 
2.33.0.1079.g6e70778dc9-goog



More information about the U-Boot mailing list