mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-09 17:59:36 +08:00
refactor load debugtalk.py module
This commit is contained in:
@@ -170,43 +170,49 @@ def load_dot_env_file(path):
|
|||||||
## debugtalk.py module loader
|
## debugtalk.py module loader
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
def locate_debugtalk_py(start_dir_path):
|
def locate_debugtalk_py(start_path):
|
||||||
""" locate debugtalk.py module and return module name.
|
""" locate debugtalk.py module and return module name.
|
||||||
|
searching will be recursive upward until current working directory.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
start_dir_path (str): start locating directory path
|
start_path (str): start locating path, maybe file path or directory path
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: located module name. None if module not found.
|
str: located module name. None if module not found.
|
||||||
|
|
||||||
Examples:
|
Raises:
|
||||||
# CWD/debugtalk.py
|
exceptions.FileNotFound: If failed to locate debugtalk.py module.
|
||||||
>>> locate_debugtalk_py("/path/to/CWD")
|
|
||||||
debugtalk
|
|
||||||
|
|
||||||
# CWD/tests/debugtalk.py
|
|
||||||
>>> locate_debugtalk_py("/path/to/CWD")
|
|
||||||
tests.debugtalk
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if os.path.isfile(start_path):
|
||||||
|
start_dir_path = os.path.dirname(start_path)
|
||||||
|
elif os.path.isdir(start_path):
|
||||||
|
start_dir_path = start_path
|
||||||
|
else:
|
||||||
|
raise exceptions.FileNotFound("invalid path: {}".format(start_path))
|
||||||
|
|
||||||
module_path = os.path.join(start_dir_path, "debugtalk.py")
|
module_path = os.path.join(start_dir_path, "debugtalk.py")
|
||||||
if os.path.isfile(module_path):
|
if os.path.isfile(module_path):
|
||||||
return "debugtalk"
|
if os.path.isabs(module_path):
|
||||||
|
module_path = module_path[len(os.getcwd())+1:]
|
||||||
|
|
||||||
# make compatible with former version
|
module_name = module_path.replace("/", ".").rstrip(".py")
|
||||||
# TODO: remove this compatiblity
|
return module_name
|
||||||
module_path = os.path.join(start_dir_path, "tests", "debugtalk.py")
|
|
||||||
if os.path.isfile(module_path):
|
|
||||||
return "tests.debugtalk"
|
|
||||||
|
|
||||||
return None
|
# current working directory
|
||||||
|
if os.path.abspath(start_dir_path) == os.getcwd():
|
||||||
|
raise exceptions.FileNotFound("debugtalk.py module not found: {}".format(start_path))
|
||||||
|
|
||||||
|
# locate recursive upward
|
||||||
|
return locate_debugtalk_py(os.path.dirname(start_dir_path))
|
||||||
|
|
||||||
|
|
||||||
def load_debugtalk_module(module_name=None):
|
def load_debugtalk_module(start_path=None):
|
||||||
""" load debugtalk.py module.
|
""" load debugtalk.py module.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
module_name (str, optional): module name for debugtalk.py. Defaults to None.
|
start_path (str, optional): start locating path, maybe file path or directory path.
|
||||||
|
Defaults to current working directory.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: variables and functions mapping for debugtalk.py
|
dict: variables and functions mapping for debugtalk.py
|
||||||
@@ -216,34 +222,22 @@ def load_debugtalk_module(module_name=None):
|
|||||||
"functions": {}
|
"functions": {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Examples:
|
|
||||||
# debugtalk.py
|
|
||||||
>>> load_debugtalk_module()
|
|
||||||
debugtalk
|
|
||||||
|
|
||||||
# tests/debugtalk.py
|
|
||||||
>>> load_debugtalk_module()
|
|
||||||
tests.debugtalk
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
exceptions.ParamsError: If failed to import specified module.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
module_name = module_name or locate_debugtalk_py(os.getcwd())
|
start_path = start_path or os.getcwd()
|
||||||
|
|
||||||
if not module_name:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
imported_module = importlib.import_module(module_name)
|
module_name = locate_debugtalk_py(start_path)
|
||||||
except ImportError:
|
except exceptions.FileNotFound:
|
||||||
raise exceptions.ParamsError("module name error: {}".format(module_name))
|
return {}
|
||||||
|
|
||||||
return {
|
imported_module = importlib.import_module(module_name)
|
||||||
|
debugtalk_module = {
|
||||||
"variables": utils.filter_module(imported_module, "variable"),
|
"variables": utils.filter_module(imported_module, "variable"),
|
||||||
"functions": utils.filter_module(imported_module, "function")
|
"functions": utils.filter_module(imported_module, "function")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return debugtalk_module
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
## suite loader
|
## suite loader
|
||||||
|
|||||||
@@ -144,23 +144,39 @@ class TestFileLoader(unittest.TestCase):
|
|||||||
class TestModuleLoader(unittest.TestCase):
|
class TestModuleLoader(unittest.TestCase):
|
||||||
|
|
||||||
def test_locate_debugtalk_py(self):
|
def test_locate_debugtalk_py(self):
|
||||||
self.assertEqual(loader.locate_debugtalk_py(os.getcwd()), "tests.debugtalk")
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
|
loader.locate_debugtalk_py(os.getcwd())
|
||||||
|
|
||||||
start_dir_path = os.path.join(os.getcwd(), "tests")
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
|
loader.locate_debugtalk_py("")
|
||||||
|
|
||||||
|
start_path = os.path.join(os.getcwd(), "tests")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
loader.locate_debugtalk_py(start_dir_path),
|
loader.locate_debugtalk_py(start_path),
|
||||||
"debugtalk"
|
"tests.debugtalk"
|
||||||
)
|
)
|
||||||
|
|
||||||
start_dir_path = os.path.join(os.getcwd(), "not_exist")
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
loader.locate_debugtalk_py(start_dir_path),
|
loader.locate_debugtalk_py("tests/"),
|
||||||
None
|
"tests.debugtalk"
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
loader.locate_debugtalk_py("tests"),
|
||||||
|
"tests.debugtalk"
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
loader.locate_debugtalk_py("tests/base.py"),
|
||||||
|
"tests.debugtalk"
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
loader.locate_debugtalk_py("tests/data/test.env"),
|
||||||
|
"tests.debugtalk"
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_load_debugtalk_module(self):
|
def test_load_debugtalk_module(self):
|
||||||
imported_module_items = loader.load_debugtalk_module("tests.debugtalk")
|
imported_module_items = loader.load_debugtalk_module()
|
||||||
print(imported_module_items)
|
self.assertEqual(imported_module_items, {})
|
||||||
|
|
||||||
|
imported_module_items = loader.load_debugtalk_module("tests")
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
imported_module_items["variables"]["SECRET_KEY"],
|
imported_module_items["variables"]["SECRET_KEY"],
|
||||||
"DebugTalk"
|
"DebugTalk"
|
||||||
@@ -171,9 +187,6 @@ class TestModuleLoader(unittest.TestCase):
|
|||||||
self.assertTrue(is_status_code_200(200))
|
self.assertTrue(is_status_code_200(200))
|
||||||
self.assertFalse(is_status_code_200(500))
|
self.assertFalse(is_status_code_200(500))
|
||||||
|
|
||||||
with self.assertRaises(exceptions.ParamsError):
|
|
||||||
loader.load_debugtalk_module("debugtalk")
|
|
||||||
|
|
||||||
|
|
||||||
class TestSuiteLoader(unittest.TestCase):
|
class TestSuiteLoader(unittest.TestCase):
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user