From 6d2e11b7d1481a7bf3209f65ba94d1ca20f8bd39 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:18:41 -0400 Subject: [PATCH 1/6] Extract method for _mark_plugins_for_rewrite --- _pytest/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/_pytest/config.py b/_pytest/config.py index c73416f0a..5abb13d5a 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -995,6 +995,10 @@ class Config(object): except SystemError: mode = 'plain' else: + self._mark_plugins_for_rewrite(hook) + self._warn_about_missing_assertion(mode) + + def _mark_plugins_for_rewrite(self, hook): import pkg_resources self.pluginmanager.rewrite_hook = hook for entrypoint in pkg_resources.iter_entry_points('pytest11'): @@ -1013,7 +1017,6 @@ class Config(object): elif is_package: package_name = os.path.dirname(fn) hook.mark_rewrite(package_name) - self._warn_about_missing_assertion(mode) def _warn_about_missing_assertion(self, mode): try: From ce0ff0040f2c05f90270e84e519df68292268bf4 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:21:08 -0400 Subject: [PATCH 2/6] Reindent and add docstring --- _pytest/config.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 5abb13d5a..0485d5381 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -999,24 +999,29 @@ class Config(object): self._warn_about_missing_assertion(mode) def _mark_plugins_for_rewrite(self, hook): - import pkg_resources - self.pluginmanager.rewrite_hook = hook - for entrypoint in pkg_resources.iter_entry_points('pytest11'): - # 'RECORD' available for plugins installed normally (pip install) - # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) - # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa - # so it shouldn't be an issue - for metadata in ('RECORD', 'SOURCES.txt'): - for entry in entrypoint.dist._get_metadata(metadata): - fn = entry.split(',')[0] - is_simple_module = os.sep not in fn and fn.endswith('.py') - is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') - if is_simple_module: - module_name, ext = os.path.splitext(fn) - hook.mark_rewrite(module_name) - elif is_package: - package_name = os.path.dirname(fn) - hook.mark_rewrite(package_name) + """ + Given an importhook, mark for rewrite any top-level + modules or packages in the distribution package for + all pytest plugins. + """ + import pkg_resources + self.pluginmanager.rewrite_hook = hook + for entrypoint in pkg_resources.iter_entry_points('pytest11'): + # 'RECORD' available for plugins installed normally (pip install) + # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) + # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa + # so it shouldn't be an issue + for metadata in ('RECORD', 'SOURCES.txt'): + for entry in entrypoint.dist._get_metadata(metadata): + fn = entry.split(',')[0] + is_simple_module = os.sep not in fn and fn.endswith('.py') + is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') + if is_simple_module: + module_name, ext = os.path.splitext(fn) + hook.mark_rewrite(module_name) + elif is_package: + package_name = os.path.dirname(fn) + hook.mark_rewrite(package_name) def _warn_about_missing_assertion(self, mode): try: From 2abf2070f2a486dab8e0592b0a774d588afcfc78 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:26:42 -0400 Subject: [PATCH 3/6] Collapse nested for loops into a generator expression --- _pytest/config.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 0485d5381..24b05e4ae 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -1006,13 +1006,21 @@ class Config(object): """ import pkg_resources self.pluginmanager.rewrite_hook = hook - for entrypoint in pkg_resources.iter_entry_points('pytest11'): - # 'RECORD' available for plugins installed normally (pip install) - # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) - # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa - # so it shouldn't be an issue - for metadata in ('RECORD', 'SOURCES.txt'): - for entry in entrypoint.dist._get_metadata(metadata): + + # 'RECORD' available for plugins installed normally (pip install) + # 'SOURCES.txt' available for plugins installed in dev mode (pip install -e) + # for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa + # so it shouldn't be an issue + metadata_files = 'RECORD', 'SOURCES.txt' + + metadata_entries = ( + entry + for entrypoint in pkg_resources.iter_entry_points('pytest11') + for metadata in metadata_files + for entry in entrypoint.dist._get_metadata(metadata) + ) + + for entry in metadata_entries: fn = entry.split(',')[0] is_simple_module = os.sep not in fn and fn.endswith('.py') is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') From 43aa037ebd9324a628cb0e4e52e52b3fa01c7761 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:26:57 -0400 Subject: [PATCH 4/6] Reindent --- _pytest/config.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 24b05e4ae..9fc2ed5fe 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -1021,15 +1021,15 @@ class Config(object): ) for entry in metadata_entries: - fn = entry.split(',')[0] - is_simple_module = os.sep not in fn and fn.endswith('.py') - is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') - if is_simple_module: - module_name, ext = os.path.splitext(fn) - hook.mark_rewrite(module_name) - elif is_package: - package_name = os.path.dirname(fn) - hook.mark_rewrite(package_name) + fn = entry.split(',')[0] + is_simple_module = os.sep not in fn and fn.endswith('.py') + is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') + if is_simple_module: + module_name, ext = os.path.splitext(fn) + hook.mark_rewrite(module_name) + elif is_package: + package_name = os.path.dirname(fn) + hook.mark_rewrite(package_name) def _warn_about_missing_assertion(self, mode): try: From 9beeef970e17fa5bb717681b1d7e89414d8eb21c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:36:33 -0400 Subject: [PATCH 5/6] Parse the filename in the generator expression --- _pytest/config.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 9fc2ed5fe..420ae5910 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -1013,15 +1013,14 @@ class Config(object): # so it shouldn't be an issue metadata_files = 'RECORD', 'SOURCES.txt' - metadata_entries = ( - entry + package_files = ( + entry.split(',')[0] for entrypoint in pkg_resources.iter_entry_points('pytest11') for metadata in metadata_files for entry in entrypoint.dist._get_metadata(metadata) ) - for entry in metadata_entries: - fn = entry.split(',')[0] + for fn in package_files: is_simple_module = os.sep not in fn and fn.endswith('.py') is_package = fn.count(os.sep) == 1 and fn.endswith('__init__.py') if is_simple_module: From c9c2c34b44c47dd00f0917bf9e7d351f00e13697 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 20 May 2017 04:39:45 -0400 Subject: [PATCH 6/6] Remove unused parameter --- _pytest/config.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 420ae5910..36288170c 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -980,7 +980,7 @@ class Config(object): self._parser.addini('minversion', 'minimally required pytest version') self._override_ini = ns.override_ini or () - def _consider_importhook(self, args, entrypoint_name): + def _consider_importhook(self, args): """Install the PEP 302 import hook if using assertion re-writing. Needs to parse the --assert= option from the commandline @@ -1053,10 +1053,9 @@ class Config(object): args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args args[:] = self.getini("addopts") + args self._checkversion() - entrypoint_name = 'pytest11' - self._consider_importhook(args, entrypoint_name) + self._consider_importhook(args) self.pluginmanager.consider_preparse(args) - self.pluginmanager.load_setuptools_entrypoints(entrypoint_name) + self.pluginmanager.load_setuptools_entrypoints('pytest11') self.pluginmanager.consider_env() self.known_args_namespace = ns = self._parser.parse_known_args(args, namespace=self.option.copy()) confcutdir = self.known_args_namespace.confcutdir