| 1 |
import os |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
|
| 5 |
from osrf_pycommon.process_utils import execute_process_nopty |
| 6 |
|
| 7 |
this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 8 |
|
| 9 |
test_script = os.path.join( |
| 10 |
this_dir, |
| 11 |
'fixtures', |
| 12 |
'execute_process', |
| 13 |
'stdout_stderr_ordering.py') |
| 14 |
python = sys.executable |
| 15 |
|
| 16 |
file_nl = b""" |
| 17 |
""" |
| 18 |
nl = os.linesep.encode() |
| 19 |
|
| 20 |
|
| 21 |
class TestProcessUtilsExecuteNoPty(unittest.TestCase): |
| 22 |
def test__process_incomming_lines(self): |
| 23 |
pil = execute_process_nopty._process_incoming_lines |
| 24 |
|
| 25 |
# Test with no left overs and no new incoming |
| 26 |
left_overs = b'' |
| 27 |
incoming = b'' |
| 28 |
self.assertEqual((None, left_overs), pil(incoming, left_overs)) |
| 29 |
|
| 30 |
# Test with left overs, but no new incoming |
| 31 |
left_overs = b'something' |
| 32 |
incoming = b'' |
| 33 |
self.assertEqual((b'', left_overs), pil(incoming, left_overs)) |
| 34 |
|
| 35 |
# Test with no left overs, but new incoming |
| 36 |
left_overs = b'' |
| 37 |
incoming = nl.join([b'one', b'two']) |
| 38 |
self.assertEqual((b'one' + nl, b'two'), pil(incoming, left_overs)) |
| 39 |
|
| 40 |
# Test with left overs and new incoming with prefixed nl |
| 41 |
left_overs = b'something' |
| 42 |
incoming = (nl + b'else') |
| 43 |
expected = (b'something' + nl, b'else') |
| 44 |
self.assertEqual(expected, pil(incoming, left_overs)) |
| 45 |
|
| 46 |
def test__execute_process_nopty_combined_unbuffered(self): |
| 47 |
exc_nopty = execute_process_nopty._execute_process_nopty |
| 48 |
|
| 49 |
# Test ordering with stdout and stderr combined and Python unbuffered |
| 50 |
cmd = [python, "-u", test_script] |
| 51 |
result = b"" |
| 52 |
for out, err, ret in exc_nopty(cmd, None, None, False, True): |
| 53 |
if out is not None: |
| 54 |
result += out |
| 55 |
if err is not None: |
| 56 |
result += err |
| 57 |
if ret is not None: |
| 58 |
break |
| 59 |
expected = b"""\ |
| 60 |
out 1 |
| 61 |
err 1 |
| 62 |
out 2 |
| 63 |
""" |
| 64 |
expected = expected.replace(file_nl, nl) |
| 65 |
expected = sorted(expected.splitlines(True)) |
| 66 |
result = sorted(result.splitlines(True)) |
| 67 |
self.assertEqual(expected, result) |
| 68 |
|
| 69 |
def test__execute_process_nopty_unbuffered(self): |
| 70 |
exc_nopty = execute_process_nopty._execute_process_nopty |
| 71 |
|
| 72 |
# Test ordering with stdout and stderr combined and Python unbuffered |
| 73 |
cmd = [python, "-u", test_script] |
| 74 |
result = b"" |
| 75 |
for out, err, ret in exc_nopty(cmd, None, None, False, False): |
| 76 |
if out is not None: |
| 77 |
result += out |
| 78 |
if err is not None: |
| 79 |
result += err |
| 80 |
if ret is not None: |
| 81 |
break |
| 82 |
expected = b"""\ |
| 83 |
out 1 |
| 84 |
err 1 |
| 85 |
out 2 |
| 86 |
""" |
| 87 |
expected = expected.replace(file_nl, nl) |
| 88 |
expected = sorted(expected.splitlines(True)) |
| 89 |
result = sorted(result.splitlines(True)) |
| 90 |
self.assertEqual(expected, result) |
| 91 |
|
| 92 |
def test__execute_process_nopty_combined(self): |
| 93 |
exc_nopty = execute_process_nopty._execute_process_nopty |
| 94 |
|
| 95 |
# Test ordering with stdout and stderr combined |
| 96 |
cmd = [python, test_script] |
| 97 |
result = b"" |
| 98 |
for out, err, ret in exc_nopty(cmd, None, None, False, True): |
| 99 |
if out is not None: |
| 100 |
result += out |
| 101 |
if err is not None: |
| 102 |
result += err |
| 103 |
if ret is not None: |
| 104 |
break |
| 105 |
expected = b"""\ |
| 106 |
out 1 |
| 107 |
err 1 |
| 108 |
out 2 |
| 109 |
""" |
| 110 |
expected = expected.replace(file_nl, nl) |
| 111 |
expected = sorted(expected.splitlines(True)) |
| 112 |
result = sorted(result.splitlines(True)) |
| 113 |
self.assertEqual(expected, result) |
| 114 |
|
| 115 |
def test__execute_process_nopty(self): |
| 116 |
exc_nopty = execute_process_nopty._execute_process_nopty |
| 117 |
|
| 118 |
# Test ordering with stdout and stderr separate |
| 119 |
cmd = [python, test_script] |
| 120 |
result = b"" |
| 121 |
for out, err, ret in exc_nopty(cmd, None, None, False, False): |
| 122 |
if out is not None: |
| 123 |
result += out |
| 124 |
if err is not None: |
| 125 |
result += err |
| 126 |
if ret is not None: |
| 127 |
break |
| 128 |
expected = b"""\ |
| 129 |
out 1 |
| 130 |
err 1 |
| 131 |
out 2 |
| 132 |
""" |
| 133 |
expected = expected.replace(file_nl, nl) |
| 134 |
expected = sorted(expected.splitlines(True)) |
| 135 |
result = sorted(result.splitlines(True)) |
| 136 |
self.assertEqual(expected, result) |