Skip to content

Commit 49fa75f

Browse files
authored
Merge pull request #118 from naylor-b/sys_path_timing
added current directory to sys.path during try_import to avoid any relative import failures in test files
2 parents a9571cf + 46dbd85 commit 49fa75f

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
@@ -19,7 +19,7 @@
1919
from testflo.cover import start_coverage, stop_coverage
2020

2121
from testflo.util import get_module, ismethod, get_memory_usage, \
22-
get_testpath, _options2args
22+
get_testpath, _options2args, _testing_path
2323
from testflo.utresult import UnitTestResult
2424
from testflo.devnull import DevNull
2525

@@ -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
@@ -18,6 +18,12 @@
1818
from argparse import ArgumentParser, _AppendAction
1919

2020

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

2329

@@ -336,7 +342,11 @@ def find_module(name):
336342

337343

338344
def try_import(fname, modpath):
345+
global _testing_path
339346
try:
347+
_testing_path[0] = os.path.dirname(fname)
348+
old_sys_path = sys.path
349+
sys.path = _testing_path
340350
mod = import_module(modpath)
341351
except ImportError:
342352
# this might be a module that's not in the same
@@ -355,6 +365,8 @@ def try_import(fname, modpath):
355365
del sys.modules[modpath]
356366
finally:
357367
sys.path = oldpath
368+
finally:
369+
sys.path = old_sys_path
358370

359371
return mod
360372

0 commit comments

Comments
 (0)