|
4 | 4 |
|
5 | 5 | from test import support, test_tools
|
6 | 6 | from test.support import os_helper
|
7 |
| -from test.support import SHORT_TIMEOUT, requires_subprocess |
8 | 7 | from test.support.os_helper import TESTFN, unlink
|
9 | 8 | from textwrap import dedent
|
10 | 9 | from unittest import TestCase
|
11 | 10 | import collections
|
12 | 11 | import inspect
|
13 | 12 | import os.path
|
14 |
| -import subprocess |
15 | 13 | import sys
|
16 | 14 | import unittest
|
17 | 15 |
|
@@ -1411,30 +1409,26 @@ def test_scaffolding(self):
|
1411 | 1409 |
|
1412 | 1410 | class ClinicExternalTest(TestCase):
|
1413 | 1411 | maxDiff = None
|
1414 |
| - clinic_py = os.path.join(test_tools.toolsdir, "clinic", "clinic.py") |
1415 |
| - |
1416 |
| - def _do_test(self, *args, expect_success=True): |
1417 |
| - with subprocess.Popen( |
1418 |
| - [sys.executable, "-Xutf8", self.clinic_py, *args], |
1419 |
| - encoding="utf-8", |
1420 |
| - bufsize=0, |
1421 |
| - stdout=subprocess.PIPE, |
1422 |
| - stderr=subprocess.PIPE, |
1423 |
| - ) as proc: |
1424 |
| - proc.wait() |
1425 |
| - if expect_success and proc.returncode: |
1426 |
| - self.fail("".join([*proc.stdout, *proc.stderr])) |
1427 |
| - stdout = proc.stdout.read() |
1428 |
| - stderr = proc.stderr.read() |
1429 |
| - # Clinic never writes to stderr. |
1430 |
| - self.assertEqual(stderr, "") |
1431 |
| - return stdout |
| 1412 | + |
| 1413 | + def run_clinic(self, *args): |
| 1414 | + with ( |
| 1415 | + support.captured_stdout() as out, |
| 1416 | + support.captured_stderr() as err, |
| 1417 | + self.assertRaises(SystemExit) as cm |
| 1418 | + ): |
| 1419 | + clinic.main(args) |
| 1420 | + return out.getvalue(), err.getvalue(), cm.exception.code |
1432 | 1421 |
|
1433 | 1422 | def expect_success(self, *args):
|
1434 |
| - return self._do_test(*args) |
| 1423 | + out, err, code = self.run_clinic(*args) |
| 1424 | + self.assertEqual(code, 0, f"Unexpected failure: {args=}") |
| 1425 | + self.assertEqual(err, "") |
| 1426 | + return out |
1435 | 1427 |
|
1436 | 1428 | def expect_failure(self, *args):
|
1437 |
| - return self._do_test(*args, expect_success=False) |
| 1429 | + out, err, code = self.run_clinic(*args) |
| 1430 | + self.assertNotEqual(code, 0, f"Unexpected success: {args=}") |
| 1431 | + return out, err |
1438 | 1432 |
|
1439 | 1433 | def test_external(self):
|
1440 | 1434 | CLINIC_TEST = 'clinic.test.c'
|
@@ -1498,8 +1492,9 @@ def test_cli_force(self):
|
1498 | 1492 | # First, run the CLI without -f and expect failure.
|
1499 | 1493 | # Note, we cannot check the entire fail msg, because the path to
|
1500 | 1494 | # the tmp file will change for every run.
|
1501 |
| - out = self.expect_failure(fn) |
1502 |
| - self.assertTrue(out.endswith(fail_msg)) |
| 1495 | + out, _ = self.expect_failure(fn) |
| 1496 | + self.assertTrue(out.endswith(fail_msg), |
| 1497 | + f"{out!r} does not end with {fail_msg!r}") |
1503 | 1498 | # Then, force regeneration; success expected.
|
1504 | 1499 | out = self.expect_success("-f", fn)
|
1505 | 1500 | self.assertEqual(out, "")
|
@@ -1641,33 +1636,30 @@ def test_cli_converters(self):
|
1641 | 1636 | )
|
1642 | 1637 |
|
1643 | 1638 | def test_cli_fail_converters_and_filename(self):
|
1644 |
| - out = self.expect_failure("--converters", "test.c") |
1645 |
| - msg = ( |
1646 |
| - "Usage error: can't specify --converters " |
1647 |
| - "and a filename at the same time" |
1648 |
| - ) |
1649 |
| - self.assertIn(msg, out) |
| 1639 | + _, err = self.expect_failure("--converters", "test.c") |
| 1640 | + msg = "can't specify --converters and a filename at the same time" |
| 1641 | + self.assertIn(msg, err) |
1650 | 1642 |
|
1651 | 1643 | def test_cli_fail_no_filename(self):
|
1652 |
| - out = self.expect_failure() |
1653 |
| - self.assertIn("usage: clinic.py", out) |
| 1644 | + _, err = self.expect_failure() |
| 1645 | + self.assertIn("no input files", err) |
1654 | 1646 |
|
1655 | 1647 | def test_cli_fail_output_and_multiple_files(self):
|
1656 |
| - out = self.expect_failure("-o", "out.c", "input.c", "moreinput.c") |
1657 |
| - msg = "Usage error: can't use -o with multiple filenames" |
1658 |
| - self.assertIn(msg, out) |
| 1648 | + _, err = self.expect_failure("-o", "out.c", "input.c", "moreinput.c") |
| 1649 | + msg = "error: can't use -o with multiple filenames" |
| 1650 | + self.assertIn(msg, err) |
1659 | 1651 |
|
1660 | 1652 | def test_cli_fail_filename_or_output_and_make(self):
|
| 1653 | + msg = "can't use -o or filenames with --make" |
1661 | 1654 | for opts in ("-o", "out.c"), ("filename.c",):
|
1662 | 1655 | with self.subTest(opts=opts):
|
1663 |
| - out = self.expect_failure("--make", *opts) |
1664 |
| - msg = "Usage error: can't use -o or filenames with --make" |
1665 |
| - self.assertIn(msg, out) |
| 1656 | + _, err = self.expect_failure("--make", *opts) |
| 1657 | + self.assertIn(msg, err) |
1666 | 1658 |
|
1667 | 1659 | def test_cli_fail_make_without_srcdir(self):
|
1668 |
| - out = self.expect_failure("--make", "--srcdir", "") |
1669 |
| - msg = "Usage error: --srcdir must not be empty with --make" |
1670 |
| - self.assertIn(msg, out) |
| 1660 | + _, err = self.expect_failure("--make", "--srcdir", "") |
| 1661 | + msg = "error: --srcdir must not be empty with --make" |
| 1662 | + self.assertIn(msg, err) |
1671 | 1663 |
|
1672 | 1664 |
|
1673 | 1665 | try:
|
|
0 commit comments