| 1 |
import os |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
|
| 5 |
this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 6 |
|
| 7 |
test_script = os.path.join( |
| 8 |
this_dir, |
| 9 |
'fixtures', |
| 10 |
'execute_process', |
| 11 |
'stdout_stderr_ordering.py') |
| 12 |
python = sys.executable |
| 13 |
|
| 14 |
|
| 15 |
def convert_file_linesep_with_pty_linesep(string): |
| 16 |
return string.replace(b""" |
| 17 |
""", b"\r\n") |
| 18 |
|
| 19 |
|
| 20 |
@unittest.skipIf(sys.platform.startswith("win"), "Windows not supported") |
| 21 |
class TestProcessUtilsExecuteNoPty(unittest.TestCase): |
| 22 |
def test__execute_process_pty_combined_unbuffered(self): |
| 23 |
from osrf_pycommon.process_utils import execute_process_pty |
| 24 |
exc_pty = execute_process_pty._execute_process_pty |
| 25 |
|
| 26 |
# Test ordering with stdout and stderr combined and Python unbuffered |
| 27 |
cmd = [python, "-u", test_script] |
| 28 |
result = b"" |
| 29 |
for out, err, ret in exc_pty(cmd, None, None, False, True): |
| 30 |
if out is not None: |
| 31 |
result += out |
| 32 |
if err is not None: |
| 33 |
result += err |
| 34 |
if ret is not None: |
| 35 |
break |
| 36 |
expected = convert_file_linesep_with_pty_linesep(b"""\ |
| 37 |
out 1 |
| 38 |
err 1 |
| 39 |
out 2 |
| 40 |
""") |
| 41 |
self.assertEqual(expected, result) |
| 42 |
|
| 43 |
def test__execute_process_pty_unbuffered(self): |
| 44 |
from osrf_pycommon.process_utils import execute_process_pty |
| 45 |
exc_pty = execute_process_pty._execute_process_pty |
| 46 |
|
| 47 |
# Test ordering with stdout and stderr combined and Python unbuffered |
| 48 |
cmd = [python, "-u", test_script] |
| 49 |
result = b"" |
| 50 |
for out, err, ret in exc_pty(cmd, None, None, False, False): |
| 51 |
if out is not None: |
| 52 |
result += out |
| 53 |
if err is not None: |
| 54 |
result += err |
| 55 |
if ret is not None: |
| 56 |
break |
| 57 |
expected = convert_file_linesep_with_pty_linesep(b"""\ |
| 58 |
out 1 |
| 59 |
err 1 |
| 60 |
out 2 |
| 61 |
""") |
| 62 |
self.assertEqual(expected, result) |
| 63 |
|
| 64 |
def test__execute_process_pty_combined(self): |
| 65 |
from osrf_pycommon.process_utils import execute_process_pty |
| 66 |
exc_pty = execute_process_pty._execute_process_pty |
| 67 |
|
| 68 |
# Test ordering with stdout and stderr combined |
| 69 |
cmd = [python, test_script] |
| 70 |
result = b"" |
| 71 |
for out, err, ret in exc_pty(cmd, None, None, False, True): |
| 72 |
if out is not None: |
| 73 |
result += out |
| 74 |
if err is not None: |
| 75 |
result += err |
| 76 |
if ret is not None: |
| 77 |
break |
| 78 |
expected = convert_file_linesep_with_pty_linesep(b"""\ |
| 79 |
out 1 |
| 80 |
err 1 |
| 81 |
out 2 |
| 82 |
""") |
| 83 |
self.assertEqual(expected, result) |
| 84 |
|
| 85 |
def test__execute_process_pty(self): |
| 86 |
from osrf_pycommon.process_utils import execute_process_pty |
| 87 |
exc_pty = execute_process_pty._execute_process_pty |
| 88 |
|
| 89 |
# Test ordering with stdout and stderr separate |
| 90 |
cmd = [python, test_script] |
| 91 |
result = b"" |
| 92 |
for out, err, ret in exc_pty(cmd, None, None, False, False): |
| 93 |
if out is not None: |
| 94 |
result += out |
| 95 |
if err is not None: |
| 96 |
result += err |
| 97 |
if ret is not None: |
| 98 |
break |
| 99 |
expected = convert_file_linesep_with_pty_linesep(b"""\ |
| 100 |
out 1 |
| 101 |
err 1 |
| 102 |
out 2 |
| 103 |
""") |
| 104 |
self.assertEqual(expected, result) |