Expose `cwd` in subprocess util. (#575)
diff --git a/mobly/utils.py b/mobly/utils.py
index 26b23c2..08b1977 100644
--- a/mobly/utils.py
+++ b/mobly/utils.py
@@ -292,6 +292,7 @@
stderr=None,
shell=False,
timeout=None,
+ cwd=None,
env=None):
"""Runs a command in a subprocess.
@@ -312,6 +313,13 @@
False to invoke it directly. See subprocess.Popen() docs.
timeout: float, the number of seconds to wait before timing out.
If not specified, no timeout takes effect.
+ cwd: string, the path to change the child's current directory to before
+ it is executed. Note that this directory is not considered when
+ searching the executable, so you can't specify the program's path
+ relative to cwd.
+ env: dict, a mapping that defines the environment variables for the
+ new process. Default behavior is inheriting the current process'
+ environment.
Returns:
A 3-tuple of the consisting of the return code, the std output, and the
@@ -329,7 +337,7 @@
if stderr is None:
stderr = subprocess.PIPE
process = psutil.Popen(
- cmd, stdout=stdout, stderr=stderr, shell=shell, env=env)
+ cmd, stdout=stdout, stderr=stderr, shell=shell, cwd=cwd, env=env)
timer = None
timer_triggered = threading.Event()
if timeout and timeout > 0:
@@ -521,6 +529,7 @@
results.append(line.strip())
return results
+
def cli_cmd_to_string(args):
"""Converts a cmd arg list to string.
diff --git a/tests/mobly/utils_test.py b/tests/mobly/utils_test.py
index f56b399..79dce6e 100755
--- a/tests/mobly/utils_test.py
+++ b/tests/mobly/utils_test.py
@@ -75,6 +75,7 @@
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False,
+ cwd=None,
env=None,
)
mock_Timer.assert_not_called()
@@ -104,6 +105,7 @@
stdout=mock_stdout,
stderr=mock_stderr,
shell=mock_shell,
+ cwd=None,
env=mock_env,
)
mock_Timer.assert_called_with(1234, mock.ANY)