[Python-checkins] [3.12] gh-106368: Increase Argument Clinic CLI test coverage (GH-107277) (#107282)

erlend-aasland webhook-mailer at python.org
Wed Jul 26 03:13:26 EDT 2023


https://github.com/python/cpython/commit/ef808517f499b25f9f847c813067f24d73af051f
commit: ef808517f499b25f9f847c813067f24d73af051f
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-07-26T07:13:22Z
summary:

[3.12] gh-106368: Increase Argument Clinic CLI test coverage (GH-107277) (#107282)

(cherry picked from commit 579100f6d75a27429e7f8de74935d7bc3a3e44e6)

Co-authored-by: Erlend E. Aasland <erlend at python.org>

files:
M Lib/test/test_clinic.py

diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py
index f527120336226..d1c61b1d401b9 100644
--- a/Lib/test/test_clinic.py
+++ b/Lib/test/test_clinic.py
@@ -1348,18 +1348,18 @@ def test_scaffolding(self):
 
 class ClinicExternalTest(TestCase):
     maxDiff = None
+    clinic_py = os.path.join(test_tools.toolsdir, "clinic", "clinic.py")
 
     def _do_test(self, *args, expect_success=True):
-        clinic_py = os.path.join(test_tools.toolsdir, "clinic", "clinic.py")
         with subprocess.Popen(
-            [sys.executable, "-Xutf8", clinic_py, *args],
+            [sys.executable, "-Xutf8", self.clinic_py, *args],
             encoding="utf-8",
             bufsize=0,
             stdout=subprocess.PIPE,
             stderr=subprocess.PIPE,
         ) as proc:
             proc.wait()
-            if expect_success == bool(proc.returncode):
+            if expect_success and proc.returncode:
                 self.fail("".join(proc.stderr))
             stdout = proc.stdout.read()
             stderr = proc.stderr.read()
@@ -1449,6 +1449,49 @@ def test_cli_force(self):
                 generated = f.read()
             self.assertTrue(generated.endswith(checksum))
 
+    def test_cli_make(self):
+        c_code = dedent("""
+            /*[clinic input]
+            [clinic start generated code]*/
+        """)
+        py_code = "pass"
+        c_files = "file1.c", "file2.c"
+        py_files = "file1.py", "file2.py"
+
+        def create_files(files, srcdir, code):
+            for fn in files:
+                path = os.path.join(srcdir, fn)
+                with open(path, "w", encoding="utf-8") as f:
+                    f.write(code)
+
+        with os_helper.temp_dir() as tmp_dir:
+            # add some folders, some C files and a Python file
+            create_files(c_files, tmp_dir, c_code)
+            create_files(py_files, tmp_dir, py_code)
+
+            # create C files in externals/ dir
+            ext_path = os.path.join(tmp_dir, "externals")
+            with os_helper.temp_dir(path=ext_path) as externals:
+                create_files(c_files, externals, c_code)
+
+                # run clinic in verbose mode with --make on tmpdir
+                out = self.expect_success("-v", "--make", "--srcdir", tmp_dir)
+
+            # expect verbose mode to only mention the C files in tmp_dir
+            for filename in c_files:
+                with self.subTest(filename=filename):
+                    path = os.path.join(tmp_dir, filename)
+                    self.assertIn(path, out)
+            for filename in py_files:
+                with self.subTest(filename=filename):
+                    path = os.path.join(tmp_dir, filename)
+                    self.assertNotIn(path, out)
+            # don't expect C files from the externals dir
+            for filename in c_files:
+                with self.subTest(filename=filename):
+                    path = os.path.join(ext_path, filename)
+                    self.assertNotIn(path, out)
+
     def test_cli_verbose(self):
         with os_helper.temp_dir() as tmp_dir:
             fn = os.path.join(tmp_dir, "test.c")
@@ -1534,6 +1577,35 @@ def test_cli_converters(self):
                     f"expected converter {converter!r}, got {line!r}"
                 )
 
+    def test_cli_fail_converters_and_filename(self):
+        out = self.expect_failure("--converters", "test.c")
+        msg = (
+            "Usage error: can't specify --converters "
+            "and a filename at the same time"
+        )
+        self.assertIn(msg, out)
+
+    def test_cli_fail_no_filename(self):
+        out = self.expect_failure()
+        self.assertIn("usage: clinic.py", out)
+
+    def test_cli_fail_output_and_multiple_files(self):
+        out = self.expect_failure("-o", "out.c", "input.c", "moreinput.c")
+        msg = "Usage error: can't use -o with multiple filenames"
+        self.assertIn(msg, out)
+
+    def test_cli_fail_filename_or_output_and_make(self):
+        for opts in ("-o", "out.c"), ("filename.c",):
+            with self.subTest(opts=opts):
+                out = self.expect_failure("--make", *opts)
+                msg = "Usage error: can't use -o or filenames with --make"
+                self.assertIn(msg, out)
+
+    def test_cli_fail_make_without_srcdir(self):
+        out = self.expect_failure("--make", "--srcdir", "")
+        msg = "Usage error: --srcdir must not be empty with --make"
+        self.assertIn(msg, out)
+
 
 try:
     import _testclinic as ac_tester



More information about the Python-checkins mailing list