##// END OF EJS Templates
tests: generalize common test case code in test-stdio.py
Manuel Jacob -
r45589:35988468 default
parent child Browse files
Show More
@@ -20,10 +20,10 b' from mercurial import dispatch'
20 from mercurial.utils import procutil
20 from mercurial.utils import procutil
21
21
22 dispatch.initstdio()
22 dispatch.initstdio()
23 procutil.stdout.write(b'aaa')
23 procutil.{stream}.write(b'aaa')
24 os.write(procutil.stdout.fileno(), b'[written aaa]')
24 os.write(procutil.{stream}.fileno(), b'[written aaa]')
25 procutil.stdout.write(b'bbb\n')
25 procutil.{stream}.write(b'bbb\n')
26 os.write(procutil.stdout.fileno(), b'[written bbb\\n]')
26 os.write(procutil.{stream}.fileno(), b'[written bbb\\n]')
27 '''
27 '''
28 UNBUFFERED = b'aaa[written aaa]bbb\n[written bbb\\n]'
28 UNBUFFERED = b'aaa[written aaa]bbb\n[written bbb\\n]'
29 LINE_BUFFERED = b'[written aaa]aaabbb\n[written bbb\\n]'
29 LINE_BUFFERED = b'[written aaa]aaabbb\n[written bbb\\n]'
@@ -62,32 +62,35 b' def _ptys():'
62 yield rwpair
62 yield rwpair
63
63
64
64
65 class TestStdout(unittest.TestCase):
65 class TestStdio(unittest.TestCase):
66 def _test(self, rwpair_generator, expected_output, python_args=[]):
66 def _test(self, stream, rwpair_generator, expected_output, python_args=[]):
67 with rwpair_generator() as (stdout_receiver, child_stdout), open(
67 assert stream in ('stdout', 'stderr')
68 with rwpair_generator() as (stream_receiver, child_stream), open(
68 os.devnull, 'rb'
69 os.devnull, 'rb'
69 ) as child_stdin:
70 ) as child_stdin:
70 proc = subprocess.Popen(
71 proc = subprocess.Popen(
71 [sys.executable] + python_args + ['-c', CHILD_PROCESS],
72 [sys.executable]
73 + python_args
74 + ['-c', CHILD_PROCESS.format(stream=stream)],
72 stdin=child_stdin,
75 stdin=child_stdin,
73 stdout=child_stdout,
76 stdout=child_stream if stream == 'stdout' else None,
74 stderr=None,
77 stderr=child_stream if stream == 'stderr' else None,
75 )
78 )
76 retcode = proc.wait()
79 retcode = proc.wait()
77 self.assertEqual(retcode, 0)
80 self.assertEqual(retcode, 0)
78 self.assertEqual(os.read(stdout_receiver, 1024), expected_output)
81 self.assertEqual(os.read(stream_receiver, 1024), expected_output)
79
82
80 def test_stdout_pipes(self):
83 def test_stdout_pipes(self):
81 self._test(_pipes, FULLY_BUFFERED)
84 self._test('stdout', _pipes, FULLY_BUFFERED)
82
85
83 def test_stdout_ptys(self):
86 def test_stdout_ptys(self):
84 self._test(_ptys, LINE_BUFFERED)
87 self._test('stdout', _ptys, LINE_BUFFERED)
85
88
86 def test_stdout_pipes_unbuffered(self):
89 def test_stdout_pipes_unbuffered(self):
87 self._test(_pipes, UNBUFFERED, python_args=['-u'])
90 self._test('stdout', _pipes, UNBUFFERED, python_args=['-u'])
88
91
89 def test_stdout_ptys_unbuffered(self):
92 def test_stdout_ptys_unbuffered(self):
90 self._test(_ptys, UNBUFFERED, python_args=['-u'])
93 self._test('stdout', _ptys, UNBUFFERED, python_args=['-u'])
91
94
92 if not pycompat.ispy3 and not pycompat.iswindows:
95 if not pycompat.ispy3 and not pycompat.iswindows:
93 # On Python 2 on non-Windows, we manually open stdout in line-buffered
96 # On Python 2 on non-Windows, we manually open stdout in line-buffered
General Comments 0
You need to be logged in to leave comments. Login now