mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-15 04:19:28 +08:00
search expected function recursive upward
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
#coding: utf-8
|
#coding: utf-8
|
||||||
|
try:
|
||||||
|
FileNotFoundError = FileNotFoundError
|
||||||
|
except NameError:
|
||||||
|
FileNotFoundError = IOError
|
||||||
|
|
||||||
class MyBaseError(BaseException):
|
class MyBaseError(BaseException):
|
||||||
pass
|
pass
|
||||||
@@ -14,3 +18,6 @@ class ParseResponseError(MyBaseError):
|
|||||||
|
|
||||||
class ValidationError(MyBaseError):
|
class ValidationError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class FunctionNotFound(NameError):
|
||||||
|
pass
|
||||||
|
|||||||
35
ate/utils.py
35
ate/utils.py
@@ -2,6 +2,7 @@ import codecs
|
|||||||
import fnmatch
|
import fnmatch
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
|
import imp
|
||||||
import importlib
|
import importlib
|
||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
@@ -260,8 +261,42 @@ def get_imported_module(module_name):
|
|||||||
"""
|
"""
|
||||||
return importlib.import_module(module_name)
|
return importlib.import_module(module_name)
|
||||||
|
|
||||||
|
def get_imported_module_from_file(file_path):
|
||||||
|
""" import module from python file path and return imported module
|
||||||
|
"""
|
||||||
|
|
||||||
|
if PYTHON_VERSION == 3:
|
||||||
|
imported_module = importlib.machinery.SourceFileLoader(
|
||||||
|
'module_name', file_path).load_module()
|
||||||
|
else:
|
||||||
|
# Python 2.7
|
||||||
|
imported_module = imp.load_source('module_name', file_path)
|
||||||
|
|
||||||
|
return imported_module
|
||||||
|
|
||||||
def filter_module_functions(module):
|
def filter_module_functions(module):
|
||||||
""" filter functions from import module
|
""" filter functions from import module
|
||||||
"""
|
"""
|
||||||
module_functions_dict = dict(filter(is_function, vars(module).items()))
|
module_functions_dict = dict(filter(is_function, vars(module).items()))
|
||||||
return module_functions_dict
|
return module_functions_dict
|
||||||
|
|
||||||
|
def search_conf_function(start_path, func):
|
||||||
|
""" search expected function recursive upward
|
||||||
|
"""
|
||||||
|
dir_path = os.path.dirname(os.path.abspath(start_path))
|
||||||
|
target_file = os.path.join(dir_path, "debugtalk.py")
|
||||||
|
|
||||||
|
if os.path.isfile(target_file):
|
||||||
|
imported_module = get_imported_module_from_file(target_file)
|
||||||
|
functions_dict = filter_module_functions(imported_module)
|
||||||
|
if func in functions_dict:
|
||||||
|
return functions_dict[func]
|
||||||
|
else:
|
||||||
|
return search_conf_function(dir_path, func)
|
||||||
|
|
||||||
|
if dir_path == start_path:
|
||||||
|
# system root path
|
||||||
|
err_msg = "{} not found in recursive upward path!".format(func)
|
||||||
|
raise exception.FunctionNotFound(err_msg)
|
||||||
|
|
||||||
|
return search_conf_function(dir_path, func)
|
||||||
|
|||||||
@@ -225,3 +225,41 @@ class TestUtils(ApiServerUnittest):
|
|||||||
updated_dict,
|
updated_dict,
|
||||||
{'a': 2, 'b': {'c': 33, 'd': 4, 'e': 5}, 'f': 6, 'g': 7}
|
{'a': 2, 'b': {'c': 33, 'd': 4, 'e': 5}, 'f': 6, 'g': 7}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_get_imported_module(self):
|
||||||
|
imported_module = utils.get_imported_module("os")
|
||||||
|
self.assertIn("walk", dir(imported_module))
|
||||||
|
|
||||||
|
def test_filter_module_functions(self):
|
||||||
|
imported_module = utils.get_imported_module("ate.utils")
|
||||||
|
self.assertIn("PYTHON_VERSION", dir(imported_module))
|
||||||
|
|
||||||
|
functions_dict = utils.filter_module_functions(imported_module)
|
||||||
|
self.assertIn("filter_module_functions", functions_dict)
|
||||||
|
self.assertNotIn("PYTHON_VERSION", functions_dict)
|
||||||
|
|
||||||
|
def test_get_imported_module_from_file(self):
|
||||||
|
imported_module = utils.get_imported_module_from_file("tests/data/debugtalk.py")
|
||||||
|
self.assertIn("gen_md5", dir(imported_module))
|
||||||
|
|
||||||
|
functions_dict = utils.filter_module_functions(imported_module)
|
||||||
|
self.assertIn("gen_md5", functions_dict)
|
||||||
|
self.assertNotIn("PYTHON_VERSION", functions_dict)
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileNotFoundError):
|
||||||
|
utils.get_imported_module_from_file("tests/data/debugtalk2.py")
|
||||||
|
|
||||||
|
def test_search_conf_function(self):
|
||||||
|
gen_md5 = utils.search_conf_function("tests/data/demo_binds.yml", "gen_md5")
|
||||||
|
self.assertTrue(utils.is_function(("gen_md5", gen_md5)))
|
||||||
|
self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72")
|
||||||
|
|
||||||
|
gen_md5 = utils.search_conf_function("tests/data/subfolder/test.yml", "gen_md5")
|
||||||
|
self.assertTrue(utils.is_function(("_", gen_md5)))
|
||||||
|
self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FunctionNotFound):
|
||||||
|
utils.search_conf_function("tests/data/subfolder/test.yml", "func_not_exist")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FunctionNotFound):
|
||||||
|
utils.search_conf_function("/user/local/bin", "gen_md5")
|
||||||
|
|||||||
Reference in New Issue
Block a user