Explorar o código

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.
Angus Gratton %!s(int64=6) %!d(string=hai) anos
pai
achega
da4cb76f5a

+ 2 - 1
examples/protocols/http_server/advanced_tests/http_server_advanced_test.py

@@ -40,8 +40,9 @@ import Utility
 # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
 
 # Import client module
+# TODO: replace with import
 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,

+ 2 - 1
examples/protocols/http_server/persistent_sockets/http_server_persistence_test.py

@@ -42,8 +42,9 @@ import Utility
 # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
 
 # Import client module
+# TODO: replace with import
 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")

+ 1 - 1
examples/protocols/http_server/simple/http_server_simple_test.py

@@ -44,7 +44,7 @@ import Utility
 
 # Import client module
 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")

+ 3 - 3
tools/tiny-test-fw/Utility/CaseConfig.py

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

+ 1 - 1
tools/tiny-test-fw/Utility/SearchCases.py

@@ -30,7 +30,7 @@ class Search(object):
         print("Try to get cases from: " + file_name)
         test_functions = []
         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)
                          if isinstance(mod.__getattribute__(x), types.FunctionType)]:
                 try:

+ 16 - 5
tools/tiny-test-fw/Utility/__init__.py

@@ -1,4 +1,5 @@
 from __future__ import print_function
+import os.path
 import sys
 
 
@@ -45,16 +46,26 @@ __LOADED_MODULES = dict()
 # 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:
-        return __LOADED_MODULES[name]
+        return __LOADED_MODULES[path]
     except KeyError:
         try:
             from importlib.machinery import SourceFileLoader
-            ret = SourceFileLoader(name, path).load_module()
+            ret = SourceFileLoader(load_name, path).load_module()
         except ImportError:
             # importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
             import imp
-            ret = imp.load_source(name, path)
-        __LOADED_MODULES[name] = ret
+            ret = imp.load_source(load_name, path)
+        __LOADED_MODULES[path] = ret
         return ret