]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
mypy: load extra test files from environment variable
authorMaico Timmerman <maico.timmerman@gmail.com>
Fri, 30 Apr 2021 14:36:23 +0000 (16:36 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 4 May 2021 21:41:33 +0000 (17:41 -0400)
Add --mypy-extra-test-path parameter to pytest execution to list extra
directories to load test files from.
This enables the stubs repo to load this plugin and specify it's
own test directory to run mypy tests. Supports both single file tests
and incremental tests based on patch files.

Change-Id: Id6424ff15b2f527183b9713384df3d625a8e6eb8

lib/sqlalchemy/testing/plugin/plugin_base.py
test/ext/mypy/test_mypy_plugin_py3k.py

index 6370d7ce95c5f9d3171210c55960868105ee9eb7..492a3224d84a6222c3ef15cb95766c6ffd65a9e1 100644 (file)
@@ -226,6 +226,15 @@ def setup_options(make_option):
         dest="dump_pyannotate",
         help="Run pyannotate and dump json info to given file",
     )
+    make_option(
+        "--mypy-extra-test-path",
+        type=str,
+        action="append",
+        default=[],
+        dest="mypy_extra_test_paths",
+        help="Additional test directories to add to the mypy tests. "
+        "This is used only when running mypy tests. Multiple OK",
+    )
 
 
 def configure_follower(follower_ident):
index c853f7be5d76c4ed06c0eb0e4fb7b57aee822100..430a484e3a05588e74e1356d8163d34a28b0f6d6 100644 (file)
@@ -78,31 +78,40 @@ class MypyPluginTest(fixtures.TestBase):
 
     def _incremental_dirs():
         path = os.path.join(os.path.dirname(__file__), "incremental")
-        return [
-            d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))
-        ]
+        files = []
+        for d in os.listdir(path):
+            if os.path.isdir(os.path.join(path, d)):
+                files.append(
+                    os.path.join(os.path.dirname(__file__), "incremental", d)
+                )
+
+        for extra_dir in testing.config.options.mypy_extra_test_paths:
+            if extra_dir and os.path.isdir(extra_dir):
+                for d in os.listdir(os.path.join(extra_dir, "incremental")):
+                    if os.path.isdir(os.path.join(path, d)):
+                        files.append(os.path.join(extra_dir, "incremental", d))
+        return files
 
     @testing.combinations(
-        *[(dirname,) for dirname in _incremental_dirs()], argnames="dirname"
+        *[(pathname,) for pathname in _incremental_dirs()], argnames="pathname"
     )
     @testing.requires.patch_library
-    def test_incremental(self, mypy_runner, per_func_cachedir, dirname):
+    def test_incremental(self, mypy_runner, per_func_cachedir, pathname):
         import patch
 
         cachedir = per_func_cachedir
 
-        path = os.path.join(os.path.dirname(__file__), "incremental", dirname)
         dest = os.path.join(cachedir, "mymodel")
         os.mkdir(dest)
 
         patches = set()
 
-        print("incremental test: %s" % dirname)
+        print("incremental test: %s" % pathname)
 
-        for fname in os.listdir(path):
+        for fname in os.listdir(pathname):
             if fname.endswith(".py"):
                 shutil.copy(
-                    os.path.join(path, fname), os.path.join(dest, fname)
+                    os.path.join(pathname, fname), os.path.join(dest, fname)
                 )
                 print("copying to: %s" % os.path.join(dest, fname))
             elif fname.endswith(".testpatch"):
@@ -111,7 +120,7 @@ class MypyPluginTest(fixtures.TestBase):
         for patchfile in [None] + sorted(patches):
             if patchfile is not None:
                 print("Applying patchfile %s" % patchfile)
-                patch_obj = patch.fromfile(os.path.join(path, patchfile))
+                patch_obj = patch.fromfile(os.path.join(pathname, patchfile))
                 assert patch_obj.apply(1, dest), (
                     "pathfile %s failed" % patchfile
                 )
@@ -131,15 +140,29 @@ class MypyPluginTest(fixtures.TestBase):
 
     def _file_combinations():
         path = os.path.join(os.path.dirname(__file__), "files")
-        return [f for f in os.listdir(path) if f.endswith(".py")]
+        files = []
+        for f in os.listdir(path):
+            if f.endswith(".py"):
+                files.append(
+                    os.path.join(os.path.dirname(__file__), "files", f)
+                )
+
+        for extra_dir in testing.config.options.mypy_extra_test_paths:
+            if extra_dir and os.path.isdir(extra_dir):
+                for f in os.listdir(os.path.join(extra_dir, "files")):
+                    if f.endswith(".py"):
+                        files.append(
+                            os.path.join(
+                                os.path.dirname(extra_dir), "files", f
+                            )
+                        )
+        return files
 
     @testing.combinations(
-        *[(filename,) for filename in _file_combinations()],
-        argnames="filename"
+        *[(filename,) for filename in _file_combinations()], argnames="path"
     )
-    def test_mypy(self, mypy_runner, filename):
-        path = os.path.join(os.path.dirname(__file__), "files", filename)
-
+    def test_mypy(self, mypy_runner, path):
+        filename = os.path.basename(path)
         use_plugin = True
 
         expected_errors = []