From: Maico Timmerman Date: Fri, 30 Apr 2021 14:36:23 +0000 (+0200) Subject: mypy: load extra test files from environment variable X-Git-Tag: rel_1_4_14~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=36b627612483331f96b0f7490d9b3d6322c5dd6a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git mypy: load extra test files from environment variable 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 --- diff --git a/lib/sqlalchemy/testing/plugin/plugin_base.py b/lib/sqlalchemy/testing/plugin/plugin_base.py index 6370d7ce95..492a3224d8 100644 --- a/lib/sqlalchemy/testing/plugin/plugin_base.py +++ b/lib/sqlalchemy/testing/plugin/plugin_base.py @@ -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): diff --git a/test/ext/mypy/test_mypy_plugin_py3k.py b/test/ext/mypy/test_mypy_plugin_py3k.py index c853f7be5d..430a484e3a 100644 --- a/test/ext/mypy/test_mypy_plugin_py3k.py +++ b/test/ext/mypy/test_mypy_plugin_py3k.py @@ -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 = []