Skip to content

Commit 9edeeb2

Browse files
[3.11] gh-106368: Increase Argument Clinic CLI test coverage (#107277) (#107326)
(cherry picked from commit 579100f)
1 parent 6bf2c29 commit 9edeeb2

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

Lib/test/test_clinic.py

+75-3
Original file line numberDiff line numberDiff line change
@@ -1294,18 +1294,18 @@ def test_scaffolding(self):
12941294

12951295
class ClinicExternalTest(TestCase):
12961296
maxDiff = None
1297+
clinic_py = os.path.join(test_tools.toolsdir, "clinic", "clinic.py")
12971298

12981299
def _do_test(self, *args, expect_success=True):
1299-
clinic_py = os.path.join(test_tools.toolsdir, "clinic", "clinic.py")
13001300
with subprocess.Popen(
1301-
[sys.executable, "-Xutf8", clinic_py, *args],
1301+
[sys.executable, "-Xutf8", self.clinic_py, *args],
13021302
encoding="utf-8",
13031303
bufsize=0,
13041304
stdout=subprocess.PIPE,
13051305
stderr=subprocess.PIPE,
13061306
) as proc:
13071307
proc.wait()
1308-
if expect_success == bool(proc.returncode):
1308+
if expect_success and proc.returncode:
13091309
self.fail("".join(proc.stderr))
13101310
stdout = proc.stdout.read()
13111311
stderr = proc.stderr.read()
@@ -1395,6 +1395,49 @@ def test_cli_force(self):
13951395
generated = f.read()
13961396
self.assertTrue(generated.endswith(checksum))
13971397

1398+
def test_cli_make(self):
1399+
c_code = dedent("""
1400+
/*[clinic input]
1401+
[clinic start generated code]*/
1402+
""")
1403+
py_code = "pass"
1404+
c_files = "file1.c", "file2.c"
1405+
py_files = "file1.py", "file2.py"
1406+
1407+
def create_files(files, srcdir, code):
1408+
for fn in files:
1409+
path = os.path.join(srcdir, fn)
1410+
with open(path, "w", encoding="utf-8") as f:
1411+
f.write(code)
1412+
1413+
with os_helper.temp_dir() as tmp_dir:
1414+
# add some folders, some C files and a Python file
1415+
create_files(c_files, tmp_dir, c_code)
1416+
create_files(py_files, tmp_dir, py_code)
1417+
1418+
# create C files in externals/ dir
1419+
ext_path = os.path.join(tmp_dir, "externals")
1420+
with os_helper.temp_dir(path=ext_path) as externals:
1421+
create_files(c_files, externals, c_code)
1422+
1423+
# run clinic in verbose mode with --make on tmpdir
1424+
out = self.expect_success("-v", "--make", "--srcdir", tmp_dir)
1425+
1426+
# expect verbose mode to only mention the C files in tmp_dir
1427+
for filename in c_files:
1428+
with self.subTest(filename=filename):
1429+
path = os.path.join(tmp_dir, filename)
1430+
self.assertIn(path, out)
1431+
for filename in py_files:
1432+
with self.subTest(filename=filename):
1433+
path = os.path.join(tmp_dir, filename)
1434+
self.assertNotIn(path, out)
1435+
# don't expect C files from the externals dir
1436+
for filename in c_files:
1437+
with self.subTest(filename=filename):
1438+
path = os.path.join(ext_path, filename)
1439+
self.assertNotIn(path, out)
1440+
13981441
def test_cli_verbose(self):
13991442
with os_helper.temp_dir() as tmp_dir:
14001443
fn = os.path.join(tmp_dir, "test.c")
@@ -1481,6 +1524,35 @@ def test_cli_converters(self):
14811524
f"expected converter {converter!r}, got {line!r}"
14821525
)
14831526

1527+
def test_cli_fail_converters_and_filename(self):
1528+
out = self.expect_failure("--converters", "test.c")
1529+
msg = (
1530+
"Usage error: can't specify --converters "
1531+
"and a filename at the same time"
1532+
)
1533+
self.assertIn(msg, out)
1534+
1535+
def test_cli_fail_no_filename(self):
1536+
out = self.expect_failure()
1537+
self.assertIn("usage: clinic.py", out)
1538+
1539+
def test_cli_fail_output_and_multiple_files(self):
1540+
out = self.expect_failure("-o", "out.c", "input.c", "moreinput.c")
1541+
msg = "Usage error: can't use -o with multiple filenames"
1542+
self.assertIn(msg, out)
1543+
1544+
def test_cli_fail_filename_or_output_and_make(self):
1545+
for opts in ("-o", "out.c"), ("filename.c",):
1546+
with self.subTest(opts=opts):
1547+
out = self.expect_failure("--make", *opts)
1548+
msg = "Usage error: can't use -o or filenames with --make"
1549+
self.assertIn(msg, out)
1550+
1551+
def test_cli_fail_make_without_srcdir(self):
1552+
out = self.expect_failure("--make", "--srcdir", "")
1553+
msg = "Usage error: --srcdir must not be empty with --make"
1554+
self.assertIn(msg, out)
1555+
14841556

14851557
try:
14861558
import _testclinic as ac_tester

0 commit comments

Comments
 (0)