test: Refactor TFW load_source() method to only take the file path

Fixes issues where example tests for persistent_sockets and advanced_tests both
loaded a module named "client", causing a race condition.
This commit is contained in:
Angus Gratton
2019-11-28 19:25:52 +08:00
committed by He Yin Ling
parent c8f483034d
commit da4cb76f5a
6 changed files with 25 additions and 12 deletions

View File

@@ -40,8 +40,9 @@ import Utility
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
# Import client module # Import client module
# TODO: replace with import
expath = os.path.dirname(os.path.realpath(__file__)) expath = os.path.dirname(os.path.realpath(__file__))
client = Utility.load_source("client", expath + "/scripts/test.py") client = Utility.load_source(expath + "/scripts/test.py")
# Due to connectivity issues (between runner host and DUT) in the runner environment, # Due to connectivity issues (between runner host and DUT) in the runner environment,

View File

@@ -42,8 +42,9 @@ import Utility
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
# Import client module # Import client module
# TODO: replace with import
expath = os.path.dirname(os.path.realpath(__file__)) expath = os.path.dirname(os.path.realpath(__file__))
client = Utility.load_source("client", expath + "/scripts/adder.py") client = Utility.load_source(expath + "/scripts/adder.py")
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")

View File

@@ -44,7 +44,7 @@ import Utility
# Import client module # Import client module
expath = os.path.dirname(os.path.realpath(__file__)) expath = os.path.dirname(os.path.realpath(__file__))
client = Utility.load_source("client", expath + "/scripts/client.py") client = Utility.load_source(expath + "/scripts/client.py")
@IDF.idf_example_test(env_tag="Example_WIFI") @IDF.idf_example_test(env_tag="Example_WIFI")

View File

@@ -172,9 +172,9 @@ class Parser(object):
""" """
output = dict() output = dict()
for key in overwrite: for key in overwrite:
_path = overwrite[key]["path"] path = overwrite[key]["path"]
_module = load_source(str(hash(_path)), overwrite[key]["path"]) module = load_source(path)
output[key] = _module.__getattribute__(overwrite[key]["class"]) output[key] = module.__getattribute__(overwrite[key]["class"])
return output return output
@classmethod @classmethod

View File

@@ -30,7 +30,7 @@ class Search(object):
print("Try to get cases from: " + file_name) print("Try to get cases from: " + file_name)
test_functions = [] test_functions = []
try: try:
mod = load_source(str(hash(file_name)), file_name) mod = load_source(file_name)
for func in [mod.__getattribute__(x) for x in dir(mod) for func in [mod.__getattribute__(x) for x in dir(mod)
if isinstance(mod.__getattribute__(x), types.FunctionType)]: if isinstance(mod.__getattribute__(x), types.FunctionType)]:
try: try:

View File

@@ -1,4 +1,5 @@
from __future__ import print_function from __future__ import print_function
import os.path
import sys import sys
@@ -45,16 +46,26 @@ __LOADED_MODULES = dict()
# it will lead to strange errors like `isinstance(object, type_of_this_object)` return False # it will lead to strange errors like `isinstance(object, type_of_this_object)` return False
def load_source(name, path): def load_source(path):
"""
Dynamic loading python file. Note that this function SHOULD NOT be used to replace ``import``.
It should only be used when the package path is only available in runtime.
:param path: The path of python file
:return: Loaded object
"""
path = os.path.realpath(path)
# load name need to be unique, otherwise it will update the already loaded module
load_name = str(len(__LOADED_MODULES))
try: try:
return __LOADED_MODULES[name] return __LOADED_MODULES[path]
except KeyError: except KeyError:
try: try:
from importlib.machinery import SourceFileLoader from importlib.machinery import SourceFileLoader
ret = SourceFileLoader(name, path).load_module() ret = SourceFileLoader(load_name, path).load_module()
except ImportError: except ImportError:
# importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3) # importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
import imp import imp
ret = imp.load_source(name, path) ret = imp.load_source(load_name, path)
__LOADED_MODULES[name] = ret __LOADED_MODULES[path] = ret
return ret return ret