@@ -1391,30 +1391,24 @@ def test_scaffolding(self):
1391
1391
1392
1392
class ClinicExternalTest (TestCase ):
1393
1393
maxDiff = None
1394
- clinic_py = os .path .join (test_tools .toolsdir , "clinic" , "clinic.py" )
1395
-
1396
- def _do_test (self , * args , expect_success = True ):
1397
- with subprocess .Popen (
1398
- [sys .executable , "-Xutf8" , self .clinic_py , * args ],
1399
- encoding = "utf-8" ,
1400
- bufsize = 0 ,
1401
- stdout = subprocess .PIPE ,
1402
- stderr = subprocess .PIPE ,
1403
- ) as proc :
1404
- proc .wait ()
1405
- if expect_success and proc .returncode :
1406
- self .fail ("" .join ([* proc .stdout , * proc .stderr ]))
1407
- stdout = proc .stdout .read ()
1408
- stderr = proc .stderr .read ()
1409
- # Clinic never writes to stderr.
1410
- self .assertEqual (stderr , "" )
1411
- return stdout
1412
1394
1413
1395
def expect_success (self , * args ):
1414
- return self ._do_test (* args )
1396
+ with support .captured_stdout () as out , support .captured_stderr () as err :
1397
+ try :
1398
+ clinic .main (args )
1399
+ except SystemExit as exc :
1400
+ if exc .code != 0 :
1401
+ self .fail (f"unexpected failure: { args = } " )
1402
+ self .assertEqual (err .getvalue (), "" )
1403
+ return out .getvalue ()
1415
1404
1416
1405
def expect_failure (self , * args ):
1417
- return self ._do_test (* args , expect_success = False )
1406
+ with support .captured_stdout () as out , support .captured_stderr () as err :
1407
+ with self .assertRaises (SystemExit ) as cm :
1408
+ clinic .main (args )
1409
+ if cm .exception .code == 0 :
1410
+ self .fail (f"unexpected success: { args = } " )
1411
+ return out .getvalue (), err .getvalue ()
1418
1412
1419
1413
def test_external (self ):
1420
1414
CLINIC_TEST = 'clinic.test.c'
@@ -1478,8 +1472,9 @@ def test_cli_force(self):
1478
1472
# First, run the CLI without -f and expect failure.
1479
1473
# Note, we cannot check the entire fail msg, because the path to
1480
1474
# the tmp file will change for every run.
1481
- out = self .expect_failure (fn )
1482
- self .assertTrue (out .endswith (fail_msg ))
1475
+ out , _ = self .expect_failure (fn )
1476
+ self .assertTrue (out .endswith (fail_msg ),
1477
+ f"{ out !r} does not end with { fail_msg !r} " )
1483
1478
# Then, force regeneration; success expected.
1484
1479
out = self .expect_success ("-f" , fn )
1485
1480
self .assertEqual (out , "" )
@@ -1621,33 +1616,30 @@ def test_cli_converters(self):
1621
1616
)
1622
1617
1623
1618
def test_cli_fail_converters_and_filename (self ):
1624
- out = self .expect_failure ("--converters" , "test.c" )
1625
- msg = (
1626
- "Usage error: can't specify --converters "
1627
- "and a filename at the same time"
1628
- )
1629
- self .assertIn (msg , out )
1619
+ _ , err = self .expect_failure ("--converters" , "test.c" )
1620
+ msg = "can't specify --converters and a filename at the same time"
1621
+ self .assertIn (msg , err )
1630
1622
1631
1623
def test_cli_fail_no_filename (self ):
1632
- out = self .expect_failure ()
1633
- self .assertIn ("usage: clinic.py " , out )
1624
+ _ , err = self .expect_failure ()
1625
+ self .assertIn ("no input files " , err )
1634
1626
1635
1627
def test_cli_fail_output_and_multiple_files (self ):
1636
- out = self .expect_failure ("-o" , "out.c" , "input.c" , "moreinput.c" )
1628
+ _ , err = self .expect_failure ("-o" , "out.c" , "input.c" , "moreinput.c" )
1637
1629
msg = "Usage error: can't use -o with multiple filenames"
1638
- self .assertIn (msg , out )
1630
+ self .assertIn (msg , err )
1639
1631
1640
1632
def test_cli_fail_filename_or_output_and_make (self ):
1633
+ msg = "can't use -o or filenames with --make"
1641
1634
for opts in ("-o" , "out.c" ), ("filename.c" ,):
1642
1635
with self .subTest (opts = opts ):
1643
- out = self .expect_failure ("--make" , * opts )
1644
- msg = "Usage error: can't use -o or filenames with --make"
1645
- self .assertIn (msg , out )
1636
+ _ , err = self .expect_failure ("--make" , * opts )
1637
+ self .assertIn (msg , err )
1646
1638
1647
1639
def test_cli_fail_make_without_srcdir (self ):
1648
- out = self .expect_failure ("--make" , "--srcdir" , "" )
1640
+ _ , err = self .expect_failure ("--make" , "--srcdir" , "" )
1649
1641
msg = "Usage error: --srcdir must not be empty with --make"
1650
- self .assertIn (msg , out )
1642
+ self .assertIn (msg , err )
1651
1643
1652
1644
1653
1645
try :
0 commit comments