Merge "Fixes import_modules_recursively for Windows"

This commit is contained in:
Jenkins 2017-06-19 13:56:28 +00:00 committed by Gerrit Code Review
commit c9a9b5d41a
2 changed files with 13 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import importlib
import os
import os.path
import random
import re
import signal
import sys
import threading
@ -57,6 +58,8 @@ SYNCHRONIZED_PREFIX = 'neutron-'
DEFAULT_THROTTLER_VALUE = 2
_SEPARATOR_REGEX = re.compile(r'[/\\]+')
synchronized = lockutils.synchronized_with_prefix(SYNCHRONIZED_PREFIX)
@ -750,6 +753,7 @@ def extract_exc_details(e):
def import_modules_recursively(topdir):
'''Import and return all modules below the topdir directory.'''
topdir = _SEPARATOR_REGEX.sub('/', topdir)
modules = []
for root, dirs, files in os.walk(topdir):
for file_ in files:
@ -760,7 +764,7 @@ def import_modules_recursively(topdir):
if module == '__init__':
continue
import_base = root.replace('/', '.')
import_base = _SEPARATOR_REGEX.sub('.', root)
# NOTE(ihrachys): in Python3, or when we are not located in the
# directory containing neutron code, __file__ is absolute, so we

View File

@ -14,8 +14,10 @@
import os.path
import random
import re
import sys
import ddt
import eventlet
import mock
import netaddr
@ -689,17 +691,20 @@ class TestExcDetails(base.BaseTestCase):
utils.extract_exc_details(Exception()), six.text_type)
@ddt.ddt
class ImportModulesRecursivelyTestCase(base.BaseTestCase):
def test_recursion(self):
@ddt.data('/', r'\\')
def test_recursion(self, separator):
expected_modules = (
'neutron.tests.unit.tests.example.dir.example_module',
'neutron.tests.unit.tests.example.dir.subdir.example_module',
)
for module in expected_modules:
sys.modules.pop(module, None)
modules = utils.import_modules_recursively(
os.path.dirname(tests.__file__))
topdir = re.sub(r'[/\\]+', separator, os.path.dirname(tests.__file__))
modules = utils.import_modules_recursively(topdir)
for module in expected_modules:
self.assertIn(module, modules)
self.assertIn(module, sys.modules)