|
23 | 23 | import gc
|
24 | 24 | import textwrap
|
25 | 25 | import json
|
| 26 | +import pathlib |
26 | 27 | from test.support.os_helper import FakePath
|
27 | 28 |
|
28 | 29 | try:
|
@@ -1442,28 +1443,23 @@ def test_communicate_epipe(self):
|
1442 | 1443 | p.communicate(b"x" * 2**20)
|
1443 | 1444 |
|
1444 | 1445 | def test_repr(self):
|
1445 |
| - # Run a command that waits for user input, to check the repr() of |
1446 |
| - # a Proc object while and after the sub-process runs. |
1447 |
| - code = 'import sys; input(); sys.exit(57)' |
1448 |
| - cmd = [sys.executable, '-c', code] |
1449 |
| - result = "<Popen: returncode: {}" |
1450 |
| - |
1451 |
| - with subprocess.Popen( |
1452 |
| - cmd, stdin=subprocess.PIPE, universal_newlines=True) as proc: |
1453 |
| - self.assertIsNone(proc.returncode) |
1454 |
| - self.assertTrue( |
1455 |
| - repr(proc).startswith(result.format(proc.returncode)) and |
1456 |
| - repr(proc).endswith('>') |
1457 |
| - ) |
1458 |
| - |
1459 |
| - proc.communicate(input='exit...\n') |
1460 |
| - proc.wait() |
1461 |
| - |
1462 |
| - self.assertIsNotNone(proc.returncode) |
1463 |
| - self.assertTrue( |
1464 |
| - repr(proc).startswith(result.format(proc.returncode)) and |
1465 |
| - repr(proc).endswith('>') |
1466 |
| - ) |
| 1446 | + path_cmd = pathlib.Path("my-tool.py") |
| 1447 | + pathlib_cls = path_cmd.__class__.__name__ |
| 1448 | + |
| 1449 | + cases = [ |
| 1450 | + ("ls", True, 123, "<Popen: returncode: 123 args: 'ls'>"), |
| 1451 | + ('a' * 100, True, 0, |
| 1452 | + "<Popen: returncode: 0 args: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...>"), |
| 1453 | + (["ls"], False, None, "<Popen: returncode: None args: ['ls']>"), |
| 1454 | + (["ls", '--my-opts', 'a' * 100], False, None, |
| 1455 | + "<Popen: returncode: None args: ['ls', '--my-opts', 'aaaaaaaaaaaaaaaaaaaaaaaa...>"), |
| 1456 | + (path_cmd, False, 7, f"<Popen: returncode: 7 args: {pathlib_cls}('my-tool.py')>") |
| 1457 | + ] |
| 1458 | + with unittest.mock.patch.object(subprocess.Popen, '_execute_child'): |
| 1459 | + for cmd, shell, code, sx in cases: |
| 1460 | + p = subprocess.Popen(cmd, shell=shell) |
| 1461 | + p.returncode = code |
| 1462 | + self.assertEqual(repr(p), sx) |
1467 | 1463 |
|
1468 | 1464 | def test_communicate_epipe_only_stdin(self):
|
1469 | 1465 | # Issue 10963: communicate() should hide EPIPE
|
|
0 commit comments