Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added current directory to sys.path during try_import to avoid any relative import failures in test files #118

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions testflo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 Down Expand Up @@ -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 @@ -19,6 +19,12 @@

from testflo.cover import start_coverage, stop_coverage

# 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 @@ -337,7 +343,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 @@ -356,6 +366,8 @@ def try_import(fname, modpath):
del sys.modules[modpath]
finally:
sys.path = oldpath
finally:
sys.path = old_sys_path

return mod

Expand Down
Loading