test: try to make test_import work better without assuming cwd

Allows it to run properly on Windows.
This commit is contained in:
Eli Schwartz 2019-03-26 16:46:44 -04:00
parent a75b0730a7
commit 02be4edee2
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6

View File

@ -82,7 +82,9 @@ class BuildTest(unittest.TestCase):
exclude.append('osx') exclude.append('osx')
if not islinux: if not islinux:
exclude.extend(['dbus', 'linux']) exclude.extend(['dbus', 'linux'])
for root, dirs, files in os.walk('src/calibre'): base = os.path.dirname(__file__)
trimpath = len(os.path.dirname(base)) + 1
for root, dirs, files in os.walk(base):
for dir in dirs: for dir in dirs:
if not os.path.isfile(os.path.join(root, dir, '__init__.py')): if not os.path.isfile(os.path.join(root, dir, '__init__.py')):
dirs.remove(dir) dirs.remove(dir)
@ -90,7 +92,7 @@ class BuildTest(unittest.TestCase):
file, ext = os.path.splitext(file) file, ext = os.path.splitext(file)
if ext != '.py': if ext != '.py':
continue continue
name = '.'.join(root.split(os.path.sep)[1:] + [file]) name = '.'.join(root[trimpath:].split(os.path.sep) + [file])
if not any(x for x in exclude if x in name): if not any(x for x in exclude if x in name):
importlib.import_module(name) importlib.import_module(name)