|
20 | 20 | import gc
|
21 | 21 | import textwrap
|
22 | 22 | import json
|
| 23 | +import pathlib |
23 | 24 | from test.support import FakePath
|
24 | 25 |
|
25 | 26 | try:
|
@@ -1373,28 +1374,23 @@ def test_communicate_epipe(self):
|
1373 | 1374 | p.communicate(b"x" * 2**20)
|
1374 | 1375 |
|
1375 | 1376 | def test_repr(self):
|
1376 |
| - # Run a command that waits for user input, to check the repr() of |
1377 |
| - # a Proc object while and after the sub-process runs. |
1378 |
| - code = 'import sys; input(); sys.exit(57)' |
1379 |
| - cmd = [sys.executable, '-c', code] |
1380 |
| - result = "<Popen: returncode: {}" |
1381 |
| - |
1382 |
| - with subprocess.Popen( |
1383 |
| - cmd, stdin=subprocess.PIPE, universal_newlines=True) as proc: |
1384 |
| - self.assertIsNone(proc.returncode) |
1385 |
| - self.assertTrue( |
1386 |
| - repr(proc).startswith(result.format(proc.returncode)) and |
1387 |
| - repr(proc).endswith('>') |
1388 |
| - ) |
1389 |
| - |
1390 |
| - proc.communicate(input='exit...\n') |
1391 |
| - proc.wait() |
1392 |
| - |
1393 |
| - self.assertIsNotNone(proc.returncode) |
1394 |
| - self.assertTrue( |
1395 |
| - repr(proc).startswith(result.format(proc.returncode)) and |
1396 |
| - repr(proc).endswith('>') |
1397 |
| - ) |
| 1377 | + path_cmd = pathlib.Path("my-tool.py") |
| 1378 | + pathlib_cls = path_cmd.__class__.__name__ |
| 1379 | + |
| 1380 | + cases = [ |
| 1381 | + ("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"), |
| 1382 | + ('a' * 100, True, 0, |
| 1383 | + "<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"), |
| 1384 | + (["ls"], False, None, "<Popen: returncode: None args: ['ls']>"), |
| 1385 | + (["ls", '--my-opts', 'a' * 100], False, None, |
| 1386 | + "<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"), |
| 1387 | + (path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>") |
| 1388 | + ] |
| 1389 | + with unittest.mock.patch.object(subprocess.Popen, '_execute_child'): |
| 1390 | + for cmd, shell, code, sx in cases: |
| 1391 | + p = subprocess.Popen(cmd, shell=shell) |
| 1392 | + p.returncode = code |
| 1393 | + self.assertEqual(repr(p), sx) |
1398 | 1394 |
|
1399 | 1395 | def test_communicate_epipe_only_stdin(self):
|
1400 | 1396 | # Issue 10963: communicate() should hide EPIPE
|
|
0 commit comments