Skip to content

Commit

Permalink
Merge pull request #118 from naylor-b/sys_path_timing
Browse files Browse the repository at this point in the history
added current directory to sys.path during try_import to avoid any relative import failures in test files
  • Loading branch information
swryan authored Jul 24, 2024
2 parents a9571cf + 46dbd85 commit 49fa75f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 1 addition & 7 deletions testflo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from testflo.cover import start_coverage, stop_coverage

from testflo.util import get_module, ismethod, get_memory_usage, \
get_testpath, _options2args
get_testpath, _options2args, _testing_path
from testflo.utresult import UnitTestResult
from testflo.devnull import DevNull

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


# create a copy of sys.path with an extra entry at the beginning so that
# we can quickly replace the first entry with the curent test's dir rather
# than constantly copying the whole sys.path
_testing_path = ['.'] + sys.path


@contextmanager
def testcontext(test):
global _testing_path
Expand Down
12 changes: 12 additions & 0 deletions testflo/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
from argparse import ArgumentParser, _AppendAction


# create a copy of sys.path with an extra entry at the beginning so that
# we can quickly replace the first entry with the curent test's dir rather
# than constantly copying the whole sys.path
_testing_path = ['.'] + sys.path


_store = {}


Expand Down Expand Up @@ -336,7 +342,11 @@ def find_module(name):


def try_import(fname, modpath):
global _testing_path
try:
_testing_path[0] = os.path.dirname(fname)
old_sys_path = sys.path
sys.path = _testing_path
mod = import_module(modpath)
except ImportError:
# this might be a module that's not in the same
Expand All @@ -355,6 +365,8 @@ def try_import(fname, modpath):
del sys.modules[modpath]
finally:
sys.path = oldpath
finally:
sys.path = old_sys_path

return mod

Expand Down

0 comments on commit 49fa75f

Please sign in to comment.