File: test_execute_process_nopty.py

package info (click to toggle)
ros-osrf-pycommon 2.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 360 kB
  • sloc: python: 1,726; makefile: 146; xml: 16
file content (136 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import unittest

from osrf_pycommon.process_utils import execute_process_nopty

this_dir = os.path.dirname(os.path.abspath(__file__))

test_script = os.path.join(
    this_dir,
    'fixtures',
    'execute_process',
    'stdout_stderr_ordering.py')
python = sys.executable

file_nl = b"""
"""
nl = os.linesep.encode()


class TestProcessUtilsExecuteNoPty(unittest.TestCase):
    def test__process_incomming_lines(self):
        pil = execute_process_nopty._process_incoming_lines

        # Test with no left overs and no new incoming
        left_overs = b''
        incoming = b''
        self.assertEqual((None, left_overs), pil(incoming, left_overs))

        # Test with left overs, but no new incoming
        left_overs = b'something'
        incoming = b''
        self.assertEqual((b'', left_overs), pil(incoming, left_overs))

        # Test with no left overs, but new incoming
        left_overs = b''
        incoming = nl.join([b'one', b'two'])
        self.assertEqual((b'one' + nl, b'two'), pil(incoming, left_overs))

        # Test with left overs and new incoming with prefixed nl
        left_overs = b'something'
        incoming = (nl + b'else')
        expected = (b'something' + nl, b'else')
        self.assertEqual(expected, pil(incoming, left_overs))

    def test__execute_process_nopty_combined_unbuffered(self):
        exc_nopty = execute_process_nopty._execute_process_nopty

        # Test ordering with stdout and stderr combined and Python unbuffered
        cmd = [python, "-u", test_script]
        result = b""
        for out, err, ret in exc_nopty(cmd, None, None, False, True):
            if out is not None:
                result += out
            if err is not None:
                result += err
            if ret is not None:
                break
        expected = b"""\
out 1
err 1
out 2
"""
        expected = expected.replace(file_nl, nl)
        expected = sorted(expected.splitlines(True))
        result = sorted(result.splitlines(True))
        self.assertEqual(expected, result)

    def test__execute_process_nopty_unbuffered(self):
        exc_nopty = execute_process_nopty._execute_process_nopty

        # Test ordering with stdout and stderr combined and Python unbuffered
        cmd = [python, "-u", test_script]
        result = b""
        for out, err, ret in exc_nopty(cmd, None, None, False, False):
            if out is not None:
                result += out
            if err is not None:
                result += err
            if ret is not None:
                break
        expected = b"""\
out 1
err 1
out 2
"""
        expected = expected.replace(file_nl, nl)
        expected = sorted(expected.splitlines(True))
        result = sorted(result.splitlines(True))
        self.assertEqual(expected, result)

    def test__execute_process_nopty_combined(self):
        exc_nopty = execute_process_nopty._execute_process_nopty

        # Test ordering with stdout and stderr combined
        cmd = [python, test_script]
        result = b""
        for out, err, ret in exc_nopty(cmd, None, None, False, True):
            if out is not None:
                result += out
            if err is not None:
                result += err
            if ret is not None:
                break
        expected = b"""\
out 1
err 1
out 2
"""
        expected = expected.replace(file_nl, nl)
        expected = sorted(expected.splitlines(True))
        result = sorted(result.splitlines(True))
        self.assertEqual(expected, result)

    def test__execute_process_nopty(self):
        exc_nopty = execute_process_nopty._execute_process_nopty

        # Test ordering with stdout and stderr separate
        cmd = [python, test_script]
        result = b""
        for out, err, ret in exc_nopty(cmd, None, None, False, False):
            if out is not None:
                result += out
            if err is not None:
                result += err
            if ret is not None:
                break
        expected = b"""\
out 1
err 1
out 2
"""
        expected = expected.replace(file_nl, nl)
        expected = sorted(expected.splitlines(True))
        result = sorted(result.splitlines(True))
        self.assertEqual(expected, result)