8000 bpo-35617: Fix for unittest discover not working with implicit namespace packages by simonfagerholm · Pull Request #11364 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-35617: Fix for unittest discover not working with implicit namespace packages #11364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Lib/unittest/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,29 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
sys.path.insert(0, top_level_dir)
self._top_level_dir = top_level_dir

is_not_importable = False
is_namespace = False
tests = []
if os.path.isdir(os.path.abspath(start_dir)):
start_dir = os.path.abspath(start_dir)
if start_dir != top_level_dir:
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
if os.path.abspath(start_dir) != top_level_dir:
try:
# Convert to dot import
dot_import = '.'.join(
os.path.relpath(start_dir, top_level_dir).split(os.sep)
)
__import__(dot_import)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis angry here :(

except ImportError:
raise ImportError('Start directory is not importable: %r' % start_dir)

if not os.path.isfile(os.path.join(start_dir, '__init__.py')):
is_namespace = True

tests = list(self._find_tests(start_dir, pattern, is_namespace))
else:
# support for discovery from dotted module names
try:
__import__(start_dir)
except ImportError:
is_not_importable = True
raise ImportError('Start directory is not importable: %r' % start_dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if is convenient raise a Exception here. This can break other tests

else:
the_module = sys.modules[start_dir]
top_part = start_dir.split('.')[0]
Expand Down Expand Up @@ -341,12 +351,9 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
sys.path.remove(top_level_dir)
else:
sys.path.remove(top_level_dir)
if not is_namespace:
tests = list(self._find_tests(start_dir, pattern))

if is_not_importable:
raise ImportError('Start directory is not importable: %r' % start_dir)

if not is_namespace:
tests = list(self._find_tests(start_dir, pattern))
return self.suiteClass(tests)

def _get_directory_containing_module(self, module_name):
Expand Down
0