Skip to content

Commit 46dbd85

Browse files
committed
added current directory to sys.path during try_import to avoid any relative import failures in test files
1 parent a185e69 commit 46dbd85

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

testflo/test.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from testflo.cover import start_coverage, stop_coverage
1919

2020
from testflo.util import get_module, ismethod, get_memory_usage, \
21-
get_testpath, _options2args
21+
get_testpath, _options2args, _testing_path
2222
from testflo.utresult import UnitTestResult
2323
from testflo.devnull import DevNull
2424

@@ -46,12 +46,6 @@ def __init__(self):
4646
self.size = 1
4747

4848

49-
# create a copy of sys.path with an extra entry at the beginning so that
50-
# we can quickly replace the first entry with the curent test's dir rather
51-
# than constantly copying the whole sys.path
52-
_testing_path = ['.'] + sys.path
53-
54-
5549
@contextmanager
5650
def testcontext(test):
5751
global _testing_path

testflo/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020
from testflo.cover import start_coverage, stop_coverage
2121

22+
# create a copy of sys.path with an extra entry at the beginning so that
23+
# we can quickly replace the first entry with the curent test's dir rather
24+
# than constantly copying the whole sys.path
25+
_testing_path = ['.'] + sys.path
26+
27+
2228
_store = {}
2329

2430

@@ -337,7 +343,11 @@ def find_module(name):
337343

338344

339345
def try_import(fname, modpath):
346+
global _testing_path
340347
try:
348+
_testing_path[0] = os.path.dirname(fname)
349+
old_sys_path = sys.path
350+
sys.path = _testing_path
341351
mod = import_module(modpath)
342352
except ImportError:
343353
# this might be a module that's not in the same
@@ -356,6 +366,8 @@ def try_import(fname, modpath):
356366
del sys.modules[modpath]
357367
finally:
358368
sys.path = oldpath
369+
finally:
370+
sys.path = old_sys_path
359371

360372
return mod
361373

0 commit comments

Comments
 (0)