Add ability to specify a custom env for start_standing_subprocess (#530)
diff --git a/mobly/utils.py b/mobly/utils.py
index a9f065c..bb2f316 100644
--- a/mobly/utils.py
+++ b/mobly/utils.py
@@ -283,7 +283,7 @@
return return_vals
-def start_standing_subprocess(cmd, shell=False):
+def start_standing_subprocess(cmd, shell=False, env=None):
"""Starts a long-running subprocess.
This is not a blocking call and the subprocess started by it should be
@@ -296,6 +296,9 @@
cmd: string, the command to start the subprocess with.
shell: bool, True to run this command through the system shell,
False to invoke it directly. See subprocess.Proc() docs.
+ env: dict, a custom environment to run the standing subprocess. If not
+ specified, inherits the current environment. See subprocess.Popen()
+ docs.
Returns:
The subprocess that was started.
@@ -306,7 +309,8 @@
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
- shell=shell)
+ shell=shell,
+ env=env)
# Leaving stdin open causes problems for input, e.g. breaking the
# code.inspect() shell (http://stackoverflow.com/a/25512460/1612937), so
# explicitly close it assuming it is not needed for standing subprocesses.
diff --git a/tests/mobly/utils_test.py b/tests/mobly/utils_test.py
index 605dc8c..b9b0c22 100755
--- a/tests/mobly/utils_test.py
+++ b/tests/mobly/utils_test.py
@@ -53,6 +53,31 @@
p.stderr.close()
p.wait()
+ @mock.patch('subprocess.Popen')
+ def test_start_standing_subproc_without_env(self, mock_Popen):
+ p = utils.start_standing_subprocess(self.sleep_cmd)
+ mock_Popen.assert_called_with(
+ self.sleep_cmd,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=False,
+ env=None,
+ )
+
+ @mock.patch('subprocess.Popen')
+ def test_start_standing_subproc_with_custom_env(self, mock_Popen):
+ mock_env = mock.MagicMock(spec=dict)
+ p = utils.start_standing_subprocess(self.sleep_cmd, env=mock_env)
+ mock_Popen.assert_called_with(
+ self.sleep_cmd,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ shell=False,
+ env=mock_env,
+ )
+
def test_stop_standing_subproc(self):
p = utils.start_standing_subprocess([self.sleep_cmd, '4'])
p1 = psutil.Process(p.pid)